<template>
	<div class="statistics-center-detail-order-num-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" label="查询时间">
							<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"
								:clearable="false"
							/>
						</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 #statusText="{ row }">
					<el-text type="danger" tag="b" v-if="[1, 2, 3, 9, 101, 102, 103, 104, 105, 200].includes(row.status)">{{ row.statusText }}</el-text>
					<span v-else>{{ row.statusText }}</span>
				</template>
				<template #order_detail="{ row }">
					<order-detail :order="{ ...row }" @updateList="queryList">{{ row?.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" :disabled="gridOptions.loading">
				<el-form-item label="事发地址" prop="AreaCode">
					<el-cascader
						:options="state.areaOptions"
						filterable
						:props="{ checkStrictly: true, value: 'id', label: 'areaName', emitPath: false }"
						placeholder="请选择事发地址"
						clearable
						v-model="state.queryParams.AreaCode"
						@change="handleQuery"
						class="w100"
					>
					</el-cascader>
				</el-form-item>
				<el-form-item label="行业" prop="IndustrialID">
					<el-select v-model="state.queryParams.IndustrialID" placeholder="请选择行业" clearable @change="handleQuery">
						<el-option v-for="item in state.industryOptions" :value="item.id" :key="item.id" :label="item.name" />
					</el-select>
				</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="statisticsCenterDetailOrderNum">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { useRoute } from 'vue-router';
import {
	centerOrderBusinessDetail,
	centerOrderBusinessDetailExport,
	centerOrderBusinessDetailTotal,
	centerOverdueReturnDetail,
	centerOverdueReturnDetailBasic,
	centerOverdueReturnDetailExport,
} from '@/api/statistics/center';
import Other from '@/utils/other';
import { FormInstance } from 'element-plus';
import { defaultDate, defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import { treeArea } from '@/api/auxiliary/area';

// 引入组件
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, // 每页条数
		IndustrialID: null, // 行业
		AreaCode: null, // 事发地址
	},
	loading: false, // 加载
	total: 0,
	areaOptions: [], // 区域信息
	industryOptions: [], // 行业
});

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: 'statisticsCenterDetailOrderNum',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	columns: [
		{
			field: 'expiredStatusText',
			title: '状态',
			width: 60,
			align: 'center',
			slots: {
				default: ({ row }) => {
					return <span class={'overdue-status-' + row.expiredStatus} title={row.expiredStatusText}></span>;
				},
			},
		},
		{
			field: 'statusText',
			title: '工单状态',
			width: 110,
			slots: {
				default: 'statusText',
			},
		},
		{ field: 'currentStepName', title: '当前节点', width: 120 },
		{ field: 'reTransactNum', title: '重办次数', width: 130 },
		{ field: 'no', title: '工单编码', minWidth: 140 },
		{
			field: 'creationTime',
			title: '受理时间',
			width: 160,
			formatter: 'formatDate',
		},
		{
			field: 'title',
			title: '工单标题',
			minWidth: 200,
			slots: { default: 'order_detail' },
		},
		{ field: 'address', title: '事发地址', width: 110 },
		{
			field: 'expiredTime',
			title: '期满时间',
			width: 160,
			formatter: 'formatDate',
		},
		{ field: 'actualHandleOrgName', title: '接办部门', width: 140 },
		{
			field: 'actualHandleTime',
			title: '接办时间',
			width: 160,
			formatter: 'formatDate',
		},
		{
			field: 'filedTime',
			title: '办结时间',
			width: 160,
			formatter: 'formatDate',
		},
		{ field: 'acceptType', title: '受理类型', width: 110 },
		{ field: 'hotspotName', title: '热点分类', width: 150 },
	],
	data: [],
	params: {
		exportMethod: centerOrderBusinessDetailExport,
		exportParams: requestParams,
	},
	sortConfig: {
		remote: true,
	},
	showFooter: true,
	footerData: [],
});
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
	getTotal();
};
/** 重置按钮操作 */
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();
	getTotal();
};
// 是否展示时间选择
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);
		showTimePicker.value = false;
		requestParams.value.StartTime = routeQueryParams.StartTime;
		requestParams.value.EndTime = routeQueryParams.EndTime;
		requestParams.value.UserID = routeQueryParams.UserID;
		requestParams.value.FieldType = routeQueryParams.FieldType;
		Reflect.deleteProperty(requestParams.value, 'crTime'); // 删除无用的参数
		const { result } = await centerOrderBusinessDetail(requestParams.value);
		gridOptions.data = result ?? [];
		state.loading = false;
		gridOptions.loading = false;
	} catch (e) {
		console.log(e);
		state.loading = false;
		gridOptions.loading = false;
	}
};
// 查询总数
const getTotal = () => {
	centerOrderBusinessDetailTotal(requestParams.value)
		.then((res) => {
			state.total = res.result ?? 0;
		})
		.catch(() => {});
};
// 排序
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 area = await treeArea();
		state.areaOptions = area?.result ?? []; //省市区数据
	} catch (error) {
		console.log(error);
	}
};
onMounted(() => {
	queryList();
	getBaseData();
	state.queryParams.crTime = [routeQueryParams.StartTime, routeQueryParams.EndTime];
	getTotal();
});
</script>
