<template>
	<div class="business-supervise-todo-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline :disabled="state.loading">
				<el-form-item label="工单标题" prop="Title">
					<el-input v-model.trim="state.queryParams.Title" placeholder="工单标题" clearable @keyup.enter="handleQuery" class="keyword-input" />
				</el-form-item>
				<el-form-item label="工单编码" prop="No">
					<el-input v-model.trim="state.queryParams.No" placeholder="工单编码" clearable @keyup.enter="handleQuery" class="keyword-input" />
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleQuery" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
					<el-button @click="drawer = true" class="default-button"> <SvgIcon name="ele-Search" class="mr5" />更多查询</el-button>
				</el-form-item>
			</el-form>
			<vxe-toolbar
				ref="toolbarRef"
				:loading="state.loading"
				custom
				:refresh="{
					queryMethod: handleQuery,
				}"
			>
			</vxe-toolbar>
			<div style="overflow: hidden; width: 100%; height: 100%; flex: 1">
				<vxe-table
					border
					:loading="state.loading"
					:data="state.tableData"
					:column-config="{ resizable: true }"
					:row-config="{ isCurrent: true, isHover: true, height: 30, useKey: true }"
					ref="tableRef"
					height="auto"
					auto-resize
					show-overflow
					:print-config="{}"
					:scrollY="{ enabled: true, gt: 100 }"
					id="businessUrge"
					:custom-config="{
						storage: true,
					}"
					showHeaderOverflow
				>
					<vxe-column field="order.expiredStatusText" title="状态" width="60" align="center">
						<template #default="{ row }">
							<span :class="'overdue-status-' + row.order?.expiredStatus" :title="row.order?.expiredStatusText"></span>
						</template>
					</vxe-column>
					<vxe-column field="order.no" title="工单编码" width="140"></vxe-column>
					<vxe-column field="order.isProvinceText" title="省/市工单" width="90"></vxe-column>
					<vxe-column field="order.title" title="工单标题" min-width="200">
						<template #default="{ row }">
							<order-detail :order="row.order" @updateList="refreshList">{{ row.order?.title }}</order-detail>
						</template>
					</vxe-column>
					<vxe-column field="order.sourceChannel" title="来源渠道" width="110"></vxe-column>
					<vxe-column field="order.statusText" title="工单状态" width="110">
						<template #default="{ row }">
							<el-text type="danger" tag="b" v-if="[1, 2, 3, 9, 101, 102, 103, 104, 105, 200].includes(row.order.status)">{{ row.order.statusText }}</el-text>
							<span v-else>{{ row.order.statusText }}</span>
						</template>
					</vxe-column>
					<vxe-column field="order.acceptType" title="受理类型" width="110"></vxe-column>
					<vxe-column field="order.hotspotName" title="热点分类" width="150"></vxe-column>
					<vxe-column field="order.acceptorName" title="受理人" width="120"></vxe-column>
					<vxe-column field="order.actualHandleOrgName" title="接办部门" width="140"></vxe-column>
					<vxe-column field="order.expiredTime" title="期满时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.order?.expiredTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="creationTime" title="督办时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.creationTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="replyLimitTime" title="督办回复时限" width="170">
						<template #default="{ row }">
							{{ formatDate(row.replyLimitTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="crUser" title="督办人" width="120"></vxe-column>
					<vxe-column field="launchOrgName" title="督办部门" width="140"></vxe-column>
					<vxe-column field="orgName" title="被督办部门" width="140"></vxe-column>
					<vxe-column field="applyContent" title="督办意见" width="140"></vxe-column>
					<vxe-column title="操作" fixed="right" width="90" align="center">
						<template #default="{ row }">
							<el-button link type="primary" @click="onReply(row)" title="回复督办" v-auth="'business:supervise:todo:reply'"> 回复 </el-button>
						</template>
					</vxe-column>
				</vxe-table>
			</div>
			<pagination
				@pagination="queryList"
				:total="state.total"
				v-model:current-page="state.queryParams.PageIndex"
				v-model:page-size="state.queryParams.PageSize"
				:disabled="state.loading"
			/>
		</div>
		<!-- 督办回复 -->
		<order-supervise-reply ref="orderSuperviseReplyRef" @updateList="refreshList" />
		<!--	更多查询	-->
		<el-drawer v-model="drawer" title="更多查询" size="500px">
			<el-form :model="state.queryParams" ref="drawerRuleFormRef" @submit.native.prevent label-width="100px" :disabled="state.loading">
				<el-form-item label="是否省工单" prop="IsProvince">
					<el-select v-model="state.queryParams.IsProvince" placeholder="请选择是否省工单" @change="handleQuery">
						<el-option label="是" value="true" />
						<el-option label="否" value="false" />
					</el-select>
				</el-form-item>
				<el-form-item prop="Channel" label="来源渠道">
					<el-select v-model="state.queryParams.Channel" placeholder="请选择来源渠道" clearable class="w100" @change="handleQuery">
						<el-option v-for="item in channelOptions" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
					</el-select>
				</el-form-item>
				<el-form-item prop="Status" label="工单状态">
					<el-select v-model="state.queryParams.Status" placeholder="请选择工单状态" clearable class="w100" @change="handleQuery">
						<el-option v-for="item in orderStatusOptions" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>
				</el-form-item>
				<el-form-item prop="AcceptTypeCode" label="受理类型">
					<el-select v-model="state.queryParams.AcceptTypeCode" placeholder="请选择受理类型" clearable class="w100" @change="handleQuery">
						<el-option v-for="item in acceptTypeOptions" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
					</el-select>
				</el-form-item>
				<el-form-item label="热点分类" prop="Hotspot">
					<el-input v-model.trim="state.queryParams.Hotspot" placeholder="热点分类" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="受理人" prop="AcceptorName">
					<el-input v-model.trim="state.queryParams.AcceptorName" placeholder="受理人" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="接办部门" prop="CurrentHandleOrgName">
					<el-input v-model.trim="state.queryParams.CurrentHandleOrgName" placeholder="接办部门" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="期满时间" prop="qmTime">
					<el-date-picker
						v-model="state.queryParams.qmTime"
						type="datetimerange"
						unlink-panels
						range-separator="至"
						start-placeholder="开始时间"
						end-placeholder="结束时间"
						:shortcuts="shortcuts"
						@change="handleQuery"
						value-format="YYYY-MM-DD[T]HH:mm:ss"
						:default-time="defaultTimeStartEnd"
					/>
				</el-form-item>
				<el-form-item label="督办时间" prop="dbTime">
					<el-date-picker
						v-model="state.queryParams.dbTime"
						type="datetimerange"
						unlink-panels
						range-separator="至"
						start-placeholder="开始时间"
						end-placeholder="结束时间"
						:shortcuts="shortcuts"
						@change="handleQuery"
						value-format="YYYY-MM-DD[T]HH:mm:ss"
						:default-time="defaultTimeStartEnd"
					/>
				</el-form-item>
				<el-form-item label="督办回复时限" prop="sxTime">
					<el-date-picker
						v-model="state.queryParams.sxTime"
						type="datetimerange"
						unlink-panels
						range-separator="至"
						start-placeholder="开始时间"
						end-placeholder="结束时间"
						:shortcuts="shortcuts"
						@change="handleQuery"
						value-format="YYYY-MM-DD[T]HH:mm:ss"
						:default-time="defaultTimeStartEnd"
					/>
				</el-form-item>
				<el-form-item label="督办人" prop="CrUser">
					<el-input v-model.trim="state.queryParams.CrUser" placeholder="督办人" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="督办部门" prop="LaunchOrgName">
					<el-input v-model.trim="state.queryParams.LaunchOrgName" placeholder="督办部门" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="被督办部门" prop="OrgName">
					<el-input v-model.trim="state.queryParams.OrgName" placeholder="被督办部门" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="督办意见" prop="ApplyContent">
					<el-input v-model.trim="state.queryParams.ApplyContent" placeholder="督办意见" clearable @keyup.enter="handleQuery" />
				</el-form-item>
			</el-form>
			<template #footer>
				<el-button type="primary" @click="handleQuery" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
				<el-button @click="resetQuery(drawerRuleFormRef)" class="default-button"> <SvgIcon name="ele-Refresh" class="mr5" />重置 </el-button>
			</template>
		</el-drawer>
	</div>
</template>
<script setup lang="tsx" name="businessSuperviseTodo">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { superviseList } from '@/api/business/supervise';
import { defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import { orderBaseData } from '@/api/business/order';
import Other from '@/utils/other';
// 引入组件
const OrderSuperviseReply = defineAsyncComponent(() => import('@/views/business/supervise/components/Order-supervise-reply.vue')); // 回复督办
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页

// 定义变量内容
const state = reactive<any>({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 20,
		SuperviseState: '0', // 未回复督办
		Title:null, // 工单标题
		No: null, // 工单编码
		IsProvince: null, // 是否省工单
		Channel: null, // 来源渠道
		Status: null, // 工单状态
		AcceptTypeCode: null, // 受理类型
		Hotspot: null, // 热点分类
		AcceptorName: null, // 受理人
		CurrentHandleOrgName: null, // 接办部门
		qmTime: [], // 期满时间
		ExpiredTimeStart: null, // 期满时间开始
		ExpiredTimeEnd: null, // 期满时间结束
		CrUser: null, // 督办人
		LaunchOrgName: null, // 督办部门
		dbTime: [], // 督办时间
		SuperviseTimeStart: null, // 督办时间开始
		SuperviseTimeEnd: null, // 督办时间结束
		sxTime: [], // 督办回复时限
		ReplyLimitTimeStart: null, // 督办回复时限开始
		ReplyLimitTimeEnd: null, // 督办回复时限结束
		OrgName: null, // 被督办部门
		ApplyContent: null, // 督办意见
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
});
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
// 刷新列表
const refreshList = () => {
  queryList();
};
/** 获取列表 */
const requestParams = ref<EmptyObjectType>({});
const queryList = () => {
	state.loading = true;
	requestParams.value = Other.deepClone(state.queryParams);
	requestParams.value.ExpiredTimeStart = state.queryParams.qmTime === null ? null : state.queryParams.qmTime[0]; // 期满时间
	requestParams.value.ExpiredTimeEnd = state.queryParams.qmTime === null ? null : state.queryParams.qmTime[1];
	Reflect.deleteProperty(requestParams.value, 'qmTime'); // 删除无用的参数
	requestParams.value.SuperviseTimeStart = state.queryParams.dbTime === null ? null : state.queryParams.dbTime[0]; // 督办时间
	requestParams.value.SuperviseTimeEnd = state.queryParams.dbTime === null ? null : state.queryParams.dbTime[1];
	Reflect.deleteProperty(requestParams.value, 'dbTime'); // 删除无用的参数
	requestParams.value.ReplyLimitTimeStart = state.queryParams.sxTime === null ? null : state.queryParams.sxTime[0]; // 督办回复时限
	requestParams.value.ReplyLimitTimeEnd = state.queryParams.sxTime === null ? null : state.queryParams.sxTime[1];
	Reflect.deleteProperty(requestParams.value, 'sxTime'); // 删除无用的参数
	superviseList(requestParams.value)
		.then((res: any) => {
			state.tableData = res.result?.items ?? [];
			state.total = res.result?.total ?? 0;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
/** 重置按钮操作 */
const drawerRuleFormRef = ref();
const ruleFormRef = ref<RefType>(); // 表单ref
const drawer = ref(false);
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	ruleFormRef.value?.resetFields();
	queryList();
};
// 回复
const orderSuperviseReplyRef = ref<RefType>();
const onReply = (row: any) => {
	orderSuperviseReplyRef.value.openDialog(row);
};
// 获取基础数据
const channelOptions = ref<EmptyArrayType>([]); // 来源渠道
const orderStatusOptions = ref<EmptyArrayType>([]); // 工单状态
const acceptTypeOptions = ref<EmptyArrayType>([]); // 受理类型
const getBaseData = async () => {
	try {
		const { result } = await orderBaseData();
		channelOptions.value = result.channelOptions ?? [];
		orderStatusOptions.value = result.orderStatusOptions ?? [];
		acceptTypeOptions.value = result.acceptTypeOptions ?? [];
	} catch (e) {
		console.log(e);
	}
};
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
	getBaseData();
});
</script>
