<template>
	<div class="snapshot-statistics-gird-order-log-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<vxe-grid v-bind="gridOptions" v-on="gridEvents" ref="gridRef">
				<template #form>
					<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>
				</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="MemberName">
					<el-input v-model="state.queryParams.MemberName" placeholder="网格员姓名" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="网格员电话" prop="MemberMobile">
					<el-input v-model="state.queryParams.MemberMobile" placeholder="网格员电话" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="编号" prop="NetworkENumber">
					<el-input v-model="state.queryParams.NetworkENumber" placeholder="编号" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="状态" prop="Status">
					<el-select v-model="state.queryParams.Status" placeholder="请选择状态" clearable @change="handleQuery">
						<el-option v-for="item in state.orderStatus" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>
				</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>
			<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="snapshotStatisticsGridOrderLog">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import type { FormInstance } from 'element-plus';
import { defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import Other from '@/utils/other';
import { getSSPStatisticsGridWorkOrder, getSSPStatisticsGridWorkOrderBaseData, getSSPStatisticsGridWorkOrderExport } from '@/api/snapshot/statistics';

// 引入组件
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, // 每页条数
		// 查询条件
		No: null, // 工单编码
		Title: null, // 工单标题
		MemberName: null, // 网格员电话
		MemberMobile: null, // 网格员电话
		NetworkENumber: null, // 编号
		Status: null, // 状态
		slTime: [], // 受理时间
		BeginCreationTime: null,
		EndCreationTime: null,
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
	orderStatus: [],
});
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' } }],
	},
	params: {
		exportMethod: getSSPStatisticsGridWorkOrderExport,
		exportParams: requestParams,
	},
	customConfig: {
		storage: true,
	},
	id: 'snapshotStatisticsGridOrderLog',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	columns: [
		{ field: 'no', title: '工单编码', width: 140 },
		{
			field: 'title',
			title: '工单标题',
			minWidth: 200,
			slots: { default: 'order_detail' },
		},
		{
			field: 'creationTime',
			title: '受理时间',
			width: 160,
			sortable: true,
			formatter: 'formatDate',
		},
		{ field: 'county', title: '区域', width: 150 },
		{ field: 'replyResultTypeTxt', title: '状态', width: 140 },
		{ field: 'networkRemark', title: '回复内容', minWidth: 200 },
		{
			field: 'replyDate',
			title: '回复时间',
			width: 160,
			sortable: true,
			formatter: 'formatDate',
		},
		{ field: 'memberName', title: '网格员姓名', width: 120 },
		{ field: 'memberMobile', title: '网格员电话', width: 140 },
		{ field: 'networkENumber', title: '编号', width: 200 },
	],
	data: [],
	sortConfig: {
		remote: true,
	},
});
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();
	},
};
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
// 改变页码
const queryList = () => {
	return new Promise((resolve, reject) => {
		requestParams.value = Other.deepClone(state.queryParams);
		requestParams.value.BeginCreationTime = state.queryParams.slTime === null ? null : state.queryParams.slTime[0]; // 受理时间
		requestParams.value.EndCreationTime = state.queryParams.slTime === null ? null : state.queryParams.slTime[1];
		Reflect.deleteProperty(requestParams.value, 'slTime'); // 删除无用的参数
		state.loading = true;
		gridOptions.loading = true;
		getSSPStatisticsGridWorkOrder(requestParams.value)
			.then((response: any) => {
				gridOptions.data = response?.result.items ?? [];
				state.total = response?.result.total;
				state.loading = false;
				gridOptions.loading = false;
				gridRef.value.clearCheckboxRow();
			})
			.catch(() => {
				state.loading = false;
				gridOptions.loading = false;
				reject();
			});
	});
};
/** 重置按钮操作 */
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 gridRef = ref<RefType>();
// 获取基础数据
const getBaseData = async () => {
	// 获取基础数据
	try {
		const { result } = await getSSPStatisticsGridWorkOrderBaseData();
		state.orderStatus = result.orderStatus;
	} catch (e) {
		console.log(e);
	}
};
onMounted(() => {
	queryList();
	getBaseData();
});
</script>
