<template>
	<div class="business-urge-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="No">
					<el-input v-model.trim="state.queryParams.No" placeholder="工单编码" clearable @keyup.enter="handleQuery" class="keyword-input" />
				</el-form-item>
				<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>
					<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="businessUrgeTodo"
					: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.sourceChannel" title="来源渠道" width="110"></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="order.creationTime" title="受理时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.order?.creationTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="order.statusText" title="工单状态" width="120">
						<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.title" title="工单标题" width="200">
						<template #default="{ row }">
							<order-detail :order="row.order" @updateList="refreshList">{{ row.order?.title }}</order-detail>
						</template>
					</vxe-column>
					<vxe-column field="order.actualHandleOrgName" title="接办部门" width="140"></vxe-column>
					<vxe-column field="order.actualHandleTime" title="接办时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.order?.actualHandleTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="order.hotspotName" title="热点分类" width="150"></vxe-column>
					<vxe-column field="order.acceptType" title="受理类型" width="110"></vxe-column>
					<vxe-column field="crUser" title="催办人" width="120"></vxe-column>
					<vxe-column field="creatorOrgName" title="催办部门" width="140"></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="orgName" title="被催办部门" width="140"></vxe-column>
					<vxe-column field="applyContent" title="催办内容" width="150"></vxe-column>
					<vxe-column title="操作" fixed="right" width="90" align="center">
						<template #default="{ row }">
							<el-button link type="primary" @click="onDetail(row)" title="查看工单详情"> 催办详情 </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-urge-detail ref="orderUrgeDetailRef" @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 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="slTime">
					<el-date-picker
						v-model="state.queryParams.slTime"
						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 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 label="接办部门" prop="CurrentHandleOrgName">
					<el-input v-model.trim="state.queryParams.CurrentHandleOrgName" placeholder="接办部门" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="接办时间" prop="jbTime">
					<el-date-picker
						v-model="state.queryParams.jbTime"
						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="Hotspot">
					<el-input v-model.trim="state.queryParams.Hotspot" placeholder="热点分类" clearable @keyup.enter="handleQuery" />
				</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="CrUser">
					<el-input v-model.trim="state.queryParams.CrUser" placeholder="催办人" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="催办部门" prop="CreatorOrgName">
					<el-input v-model.trim="state.queryParams.CreatorOrgName" placeholder="催办部门" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="催办时间" prop="cbTime">
					<el-date-picker
						v-model="state.queryParams.cbTime"
						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="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="businessUrgeTodo">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { urgeList, urgeToDo } from '@/api/business/urge';
import { orderBaseData } from '@/api/business/order';
import { defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import Other from '@/utils/other';
// 引入组件
const OrderUrgeDetail = defineAsyncComponent(() => import('@/views/business/urge/components/Order-urge-detail.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,
		UrgeState: '0', // 回复状态
		No:null, // 工单编码
		Title:null, // 工单标题
		IsProvince:null, // 是否省工单
		Channel:null, // 来源渠道
		qmTime:[], // 期满时间
		ExpiredTimeStart:null,
		ExpiredTimeEnd:null,
		slTime:[], // 受理时间
		CreationTimeStart:null,
		CreationTimeEnd:null,
		Status:null, // 工单状态
		CurrentHandleOrgName:null, // 接办部门
		jbTime:[], // 接办时间
		CurrentHandleTimeStart:null,
		CurrentHandleTimeEnd:null,
		Hotspot:null, // 热点分类
		AcceptTypeCode:null, // 受理类型
		CrUser:null, // 催办人
		CreatorOrgName:null, // 催办部门
		cbTime:[], // 催办时间
		CrTimeStart:null, //
		CrTimeEnd: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.CreationTimeStart  = state.queryParams.slTime === null ? null : state.queryParams.slTime[0]; // 受理时间
	requestParams.value.CreationTimeEnd = state.queryParams.slTime === null ? null : state.queryParams.slTime[1];
	Reflect.deleteProperty(requestParams.value, 'slTime'); // 删除无用的参数
	requestParams.value.CurrentHandleTimeStart  = state.queryParams.jbTime === null ? null : state.queryParams.jbTime[0]; //接办时间
	requestParams.value.CurrentHandleTimeEnd = state.queryParams.jbTime === null ? null : state.queryParams.jbTime[1];
	Reflect.deleteProperty(requestParams.value, 'jbTime'); // 删除无用的参数
	requestParams.value.CrTimeStart  = state.queryParams.cbTime === null ? null : state.queryParams.cbTime[0]; // 催办时间
	requestParams.value.CrTimeEnd = state.queryParams.cbTime === null ? null : state.queryParams.cbTime[1];
	Reflect.deleteProperty(requestParams.value, 'cbTime'); // 删除无用的参数
	urgeToDo(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 orderUrgeDetailRef = ref<RefType>(); // 催办详情
const onDetail = (row: any) => {
	orderUrgeDetailRef.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>
