<template>
	<div class="business-second-handle-index-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline>
				<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 label="">
					<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,
				}"
				:tools="[{ toolRender: { name: 'exportCurrent' } }, { toolRender: { name: 'exportAll' } }]"
			>
			</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
					:scrollY="{ enabled: true, gt: 100 }"
					id="businessSecondHandleIndex"
					:custom-config="{ storage: true }"
					showHeaderOverflow
					:params="{ exportMethod: secondHandleAuditExport, exportParams: requestParams }"
				>
					<vxe-column field="order.expiredStatusText" title="超期状态" width="90" align="center">
						<template #default="{ row }">
							<span :class="'overdue-status-' + row.order?.expiredStatus" :title="row.order?.expiredStatusText"></span>
						</template>
					</vxe-column>
					<vxe-column field="stateText" title="办理状态" width="100"></vxe-column>
					<vxe-column field="order.no" title="工单编码" width="140"></vxe-column>
					<vxe-column field="order.title" title="工单标题" min-width="200">
						<template #default="{ row }">
							<order-detail :order="row.order" @updateList="queryList">{{ row.order?.title }}</order-detail>
						</template>
					</vxe-column>
					<vxe-column field="creatorName" 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="order.acceptType" title="受理类型" width="120"></vxe-column>
					<vxe-column field="order.hotspotName" title="热点分类" width="150"></vxe-column>
					<vxe-column title="操作" fixed="right" width="100" 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>
		<!--  办理明细  -->
		<second-handle-detail ref="secondHandleDetailRef" @updateList="queryList" />
		<!--	更多查询	-->
		<el-drawer v-model="drawer" title="更多查询" size="500px">
			<el-form :model="state.queryParams" ref="drawerRuleFormRef" @submit.native.prevent label-width="100px">
				<el-form-item label="办理状态" prop="Status">
					<el-select v-model="state.queryParams.Status" placeholder="请选择办理状态" clearable @change="handleQuery">
						<el-option v-for="item in state.statusOptions" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>
				</el-form-item>
				<el-form-item label="申请时间" prop="crTime">
					<el-date-picker
						v-model="state.queryParams.crTime"
						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>
			<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="businessSecondHandleIndex">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { secondHandleAuditExport, secondHandleAuditList, secondHandleBase } from '@/api/business/secondHandle';
import { defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import other from '@/utils/other';
// 引入组件
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const SecondHandleDetail = defineAsyncComponent(() => import('@/views/business/secondHandle/components/Second-handle-detail.vue')); // 办理明细
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页

const state = reactive<any>({
	queryParams: {
		PageIndex: 1,
		PageSize: 20,
		Status: null, //
		crTime: [], // 申请时间
		CreationTimeStart: null, // 受理时间开始
		CreationTimeEnd: null, // 受理时间结束
		No: null,
		Title: null,
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
	statusOptions: [],
	screenStatus: [], // 甄别状态
	screenType: [], // 甄别类型
	counterSignTypeOptions: [], // 会签类型
	acceptTypeOptions: [], // 受理类型
	sourceChannelOptions: [], // 来源渠道
	dissatisfiedReason: [], // 不满意原因
	visitSatisfaction: [], // 满意度
});
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
// 获取基础数据
const getBaseData = async () => {
	try {
		const { result } = await secondHandleBase();
		state.statusOptions = result?.status ?? [];
	} catch (e) {
		console.log(e);
	}
};
/** 获取列表 */
const requestParams = ref<EmptyObjectType>({});
const queryList = () => {
	requestParams.value = other.deepClone(state.queryParams);
	requestParams.value.CreationTimeStart = state.queryParams.crTime === null ? null : state.queryParams.crTime[0];
	requestParams.value.CreationTimeEnd = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
	Reflect.deleteProperty(requestParams.value, 'crTime'); // 受理时间
	state.loading = true;
	secondHandleAuditList(requestParams.value)
		.then((res) => {
			state.tableData = res?.result.items ?? [];
			state.total = res?.result.total;
			state.loading = false;
		})
		.finally(() => {
			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 secondHandleApplyRef = ref<RefType>();
const onApply = (row: any) => {
	if (row.secondaryHandling) {
		// 如果有值表示修改
		secondHandleApplyRef.value.openDialog(row, true);
	} else {
		// 没有表示新增
		secondHandleApplyRef.value.openDialog(row);
	}
};
// 办理明细
const secondHandleDetailRef = ref<RefType>();
const onDetail = (row: any) => {
	secondHandleDetailRef.value.openDialog(row.id);
};
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	getBaseData();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
	queryList();
});
</script>
