<template>
	<div class="statistics-center-detail-seat-satisfaction-list-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<vxe-grid v-bind="gridOptions" v-on="gridEvents">
				<template #form>
					<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline :disabled="gridOptions.loading">
						<el-form-item prop="crTime" v-if="showTimePicker" label="退回时间">
							<statistical-time v-model="state.queryParams.crTime" @change="handleQuery" ref="statisticalTimeRef" :disabled="state.loading" />
						</el-form-item>
						<el-form-item label="工单标题" prop="Title">
							<el-input v-model="state.queryParams.Title" placeholder="工单标题" clearable @keyup.enter="handleQuery" class="keyword-input" />
						</el-form-item>
						<el-form-item label="工单编码" prop="No">
							<el-input v-model="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>
				</template>
				<template #order_detail="{ row }">
					<order-detail :order="{ ...row.order }" @updateList="queryList">{{ row.order?.title }}</order-detail>
				</template>
				<template #pager>
					<pagination
						@pagination="queryList"
						:total="state.total"
						v-model:current-page="state.queryParams.PageIndex"
						v-model:page-size="state.queryParams.PageSize"
						:disabled="state.loading"
					/>
				</template>
			</vxe-grid>
		</div>
		<!--	更多查询	-->
		<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="Channel">
					<el-select v-model="state.queryParams.Channel" placeholder="请选择来源渠道" clearable @change="handleQuery">
						<el-option v-for="item in state.channelOptions" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
					</el-select>
				</el-form-item>
				<el-form-item label="受理类型" prop="AcceptType">
					<el-select v-model="state.queryParams.AcceptType" placeholder="请选择受理类型" clearable @change="handleQuery">
						<el-option v-for="item in state.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="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 label="申请部门" prop="ApplyOrgName">
					<el-input v-model="state.queryParams.ApplyOrgName" placeholder="申请部门" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="申请人" prop="ApplyUserName">
					<el-input v-model="state.queryParams.ApplyUserName" placeholder="申请人" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="退回审批状态" prop="State">
					<el-select v-model="state.queryParams.State" placeholder="请选择退回审批状态" clearable @change="handleQuery">
						<el-option v-for="item in state.sendBackAuditState" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>
				</el-form-item>
				<el-form-item label="审批人" prop="AuditUser">
					<el-input v-model="state.queryParams.AuditUser" placeholder="审批人" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="审批时间" prop="spTime">
					<el-date-picker
						v-model="state.queryParams.spTime"
						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="AuditContent">
					<el-input v-model="state.queryParams.AuditContent" 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="statisticsCenterDetailOverdueReturn">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { useRoute } from 'vue-router';
import { centerOverdueReturnDetail, centerOverdueReturnDetailBasic, centerOverdueReturnDetailExport } from '@/api/statistics/center';
import Other from '@/utils/other';
import { FormInstance } from 'element-plus';
import { defaultDate, defaultTimeStartEnd, shortcuts } from '@/utils/constants';

// 引入组件
const StatisticalTime = defineAsyncComponent(() => import('@/components/StatisticalTime/index.vue')); // 日期类型选择组件
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
// 定义变量内容
const state = reactive<any>({
	queryParams: {
		// 查询条件
		crTime: defaultDate, //
		StartTime: null,
		EndTime: null,
		Title: null, // 标题
		No: null, // 工单编号
		PageIndex: 1, // 当前页码
		PageSize: 20, // 每页条数
		Channels: null, // 来源渠道
		AcceptType: null, // 受理类型
		Hotspot: null, // 热点分类
		slTime: [], // 受理时间
		CreationTimeStart: null,
		CreationTimeEnd: null,
		ApplyOrgName: null, // 申请部门
		ApplyUserName: null, // 申请人
		State: null, // 退回审批状态
		AuditUser: null, // 审批人
		spTime: [], // 审批时间
		AuditTimeStart: null,
		AuditTimeEnd: null,
		AuditContent: null, // 审批意见
	},
	loading: false, // 加载
	total: 0,
	channelOptions: [], // 来源渠道
	acceptTypeOptions: [], // 受理类型
	sendBackAuditState: [], // 退回审批状态
});

const requestParams = ref<EmptyObjectType>({});
const gridOptions = reactive<any>({
	loading: false,
	border: true,
	showOverflow: true,
	columnConfig: {
		resizable: true,
	},
	scrollY: {
		enabled: true,
		gt: 100,
	},
	toolbarConfig: {
		zoom: true,
		custom: true,
		refresh: {
			queryMethod: () => {
				handleQuery();
			},
		},
		tools: [{ toolRender: { name: 'exportCurrent' } }, { toolRender: { name: 'exportAll' } }],
	},
	customConfig: {
		storage: true,
	},
	id: 'statisticsCenterDetailOverdueReturn',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	columns: [
		{ field: 'order.no', title: '工单编码', minWidth: 140 },
		{ field: 'order.isProvinceText', title: '省/市工单', width: 90 },
		{
			field: 'order.title',
			title: '工单标题',
			minWidth: 200,
			slots: { default: 'order_detail' },
		},
		{ field: 'order.sourceChannel', title: '来源渠道', width: 110 },
		{ field: 'order.acceptType', title: '受理类型', width: 110 },
		{ field: 'order.hotspotName', title: '热点分类', width: 150 },
		{
			field: 'order.startTime',
			title: '受理时间',
			width: 160,
			sortable: true,
			formatter: 'formatDate',
		},
		{
			field: 'creationTime',
			title: '退回时间',
			width: 160,
			sortable: true,
			formatter: 'formatDate',
		},
		{ field: 'applyOrgName', title: '申请部门', width: 140 },
		{ field: 'creatorName', title: '申请人', width: 140 },
		{ field: 'stateText', title: '退回审批状态', width: 140 },
		{ field: 'auditUser', title: '审批人', width: 140 },
		{
			field: 'auditTime',
			title: '审批时间',
			width: 160,
			sortable: true,
			formatter: 'formatDate',
		},
		{ field: 'auditContent', title: '审批意见', minWidth: 200 },
	],
	data: [],
	params: {
		exportMethod: centerOverdueReturnDetailExport,
		exportParams: requestParams,
	},
	sortConfig: {
		remote: true,
	},
	showFooter: true,
	footerData: [],
});
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 重置按钮操作 */
const statisticalTimeRef = ref<RefType>();
const drawerRuleFormRef = ref();
const ruleFormRef = ref<RefType>(); // 表单ref
const drawer = ref(false);
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	statisticalTimeRef.value.reset();
	ruleFormRef.value?.resetFields();
	queryList();
};
// 是否展示时间选择
const showTimePicker = ref<boolean>(true);
/** 获取列表 */
const route = useRoute();
const routeQueryParams = route.query;
const queryList = async () => {
	try {
		state.loading = true;
		gridOptions.loading = true;
		requestParams.value = Other.deepClone(state.queryParams);
		if (routeQueryParams.mode === 'detail') {
			showTimePicker.value = false;
			requestParams.value.StartTime = routeQueryParams.StartTime;
			requestParams.value.EndTime = routeQueryParams.EndTime;
			requestParams.value.Type = Number(routeQueryParams.Type);
			requestParams.value.OrgId = routeQueryParams.OrgId;
		} else {
			requestParams.value.StartTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[0];
			requestParams.value.EndTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
		}
		Reflect.deleteProperty(requestParams.value, 'crTime');
		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.AuditTimeStart = state.queryParams.spTime === null ? null : state.queryParams.spTime[0]; // 审批时间
		requestParams.value.AuditTimeEnd = state.queryParams.spTime === null ? null : state.queryParams.spTime[1];
		Reflect.deleteProperty(requestParams.value, 'spTime'); // 删除无用的参数
		const { result } = await centerOverdueReturnDetail(requestParams.value);
		gridOptions.data = result.items ?? [];
		state.total = result.total ?? 0;
		state.loading = false;
		gridOptions.loading = false;
	} catch (e) {
		console.log(e);
		state.loading = false;
		gridOptions.loading = false;
	}
};
// 排序
const gridEvents = {
	sortChange(val: any) {
		state.queryParams.SortField = val.order ? val.field : null;
		// 0 升序 1 降序
		state.queryParams.SortRule = val.order ? (val.order == 'desc' ? 1 : 0) : null;
		handleQuery();
	},
};
// 获取基础数据
const getBaseData = async () => {
	try {
		const { result } = await centerOverdueReturnDetailBasic();
		const mappings: any = {
			acceptTypeOptions: 'acceptTypeOptions',
			channelOptions: 'channelOptions',
			sendBackAuditState: 'sendBackAuditState',
		};
		for (const key in mappings) {
			state[key] = result?.[mappings[key]] ?? [];
		}
	} catch (error) {
		console.log(error);
	}
};
onMounted(() => {
	queryList();
	getBaseData();
});
</script>
