<template>
	<div class="judicial-order-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="IsTheClueTrueText">
					<el-segmented
						:options="[
							{
								value: '0',
								label: '全部',
							},
							{
								value: '1',
								label: '待核实',
							},
							{
								value: '2',
								label: '线索属实',
							},
							{
								value: '3',
								label: '线索不属实',
							},
							{
								value: '4',
								label: '推诿工单',
							},
							{
								value: '5',
								label: '行政执法类工单',
							},
						]"
						v-model="state.queryParams.IsTheClueTrueText"
						@change="handleQuery"
						: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="">
					<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' } }]"
			>
				<template #buttons>
					<el-button type="primary" @click="onVerify(null)" v-auth="'judicial:order:verify:multiple'" :disabled="isChecked" :loading="state.loading"
						>线索核实
					</el-button>
				</template>
			</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="judicialOrder"
					:custom-config="{ storage: true }"
					:params="{ exportMethod: judicialOrderListExport, exportParams: requestParams }"
					showHeaderOverflow
					@checkbox-all="selectAllChangeEvent"
					@checkbox-change="selectChangeEvent"
					:checkbox-config="checkboxConfig"
				>
					<vxe-column type="checkbox" width="50" align="center"></vxe-column>
					<vxe-column field="no" title="工单编码" width="160"></vxe-column>
					<vxe-column field="title" title="工单标题" min-width="200">
						<template #default="{ row }">
							<order-detail :order="row" @updateList="queryList">{{ row.title }}</order-detail>
						</template>
					</vxe-column>
					<vxe-column field="sourceChannel" title="来源渠道" width="110"></vxe-column>
					<vxe-column field="acceptType" title="受理类型" width="100"></vxe-column>
					<vxe-column field="hotspotName" title="热点分类" width="150"></vxe-column>
					<vxe-column field="enforcementOrdersHandlerText" title="执法部门" min-width="150"></vxe-column>
					<vxe-column field="incidentAddressText" title="事发地址" min-width="150"></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="acceptorName" title="热点分类" width="120"></vxe-column>
					<vxe-column field="isPassTheBuckOrderText" title="是否推诿工单" width="120"></vxe-column>
					<vxe-column field="isTheClueTrueText" title="线索是否属实" width="120"></vxe-column>
					<vxe-column field="isEnforcementOrderText" title="是否行政执法类工单" width="150"></vxe-column>
					<vxe-column field="eventTypeName" title="事项类型" width="150"></vxe-column>
					<vxe-column title="操作" fixed="right" width="140" align="center" :show-overflow="false">
						<template #default="{ row }">
							<el-button
								link
								type="primary"
								@click="onVerify(row)"
								title="线索核实"
								v-if="row.isTheClueTrue === null"
								v-auth="'judicial:order:verify'"
							>
								线索核实
							</el-button>
							<el-button link type="primary" @click="onEdit(row)" title="修改" v-if="row.isShowUpdateButton"> 修改 </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>
		<!-- 线索核实  -->
		<Verify ref="verifyRef" @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="No">
					<el-input v-model="state.queryParams.No" placeholder="工单编码" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="受理类型" prop="AcceptTypeCode">
					<el-select v-model="state.queryParams.AcceptTypeCode" placeholder="请选择受理类型" clearable class="w100" @change="handleQuery">
						<el-option v-for="item in state.acceptTypeOptions" :value="item.value" :key="item.value" :label="item.key" />
					</el-select>
				</el-form-item>
				<el-form-item label="热点分类" prop="HotspotId">
					<el-tree-select
						v-model="state.queryParams.HotspotId"
						:data="state.enforcementHotspot"
						:render-after-expand="false"
						class="w100"
						node-key="id"
						:props="{
							isLeaf: 'hasChild',
							children: 'children',
							label: 'hotSpotFullName',
						}"
						placeholder="请选择热点分类"
						filterable
						expand-on-click-node
						@change="handleQuery"
					/>
				</el-form-item>
				<el-form-item label="受理人" prop="NameOrNo">
					<el-input v-model="state.queryParams.NameOrNo" placeholder="受理人" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="执法部门" prop="OrgCode">
					<el-select v-model="state.queryParams.OrgCode" placeholder="请选择执法部门" class="w100" @change="handleQuery">
						<el-option v-for="item in state.orgsOptions" :key="item.id" :label="item.name" :value="item.id" />
					</el-select>
				</el-form-item>
				<el-form-item label="事发地址" prop="AreaCode">
					<el-cascader
						:options="state.areaOptions"
						filterable
						:props="{ checkStrictly: true, value: 'id', label: 'areaName', emitPath: false }"
						placeholder="请选择事发地址"
						clearable
						class="w100"
						v-model="state.queryParams.AreaCode"
						@change="handleQuery"
					>
					</el-cascader>
				</el-form-item>
				<el-form-item label="来源渠道" prop="ChannelCode">
					<el-select
						v-model="state.queryParams.ChannelCode"
						placeholder="请选择来源渠道"
						clearable
						class="w100"
						@change="handleQuery"
					>
						<el-option v-for="item in state.channelOptions" :value="item.value" :key="item.value" :label="item.key" />
					</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-item label="事项分类" prop="EventTypeId">
					<el-tree-select
						v-model="state.queryParams.EventTypeId"
						:data="treeList"
						:props="{
							label: 'eventTypeName',
							value: 'id',
							children: 'children',
						}"
						class="w100"
						ref="treeRef"
						@change="handleQuery"
						filterable
					/>
				</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="judicialOrder">
import { computed, defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import type { FormInstance } from 'element-plus';
import { defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import { formatDate } from '@/utils/formatTime';
import { getCluesBaseData, getWorkList, judicialOrderListExport, listBaseData } from '@/api/judicial';
import Other from '@/utils/other';
import { treeArea } from '@/api/auxiliary/area';

// 引入组件
const OrderDetail = defineAsyncComponent(() => import('@/views/judicial/order/components/orderDetail.vue')); // 工单详情
const Verify = defineAsyncComponent(() => import('@/views/judicial/order/components/verify.vue')); // 线索核实
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页

// 定义变量内容
const state = reactive<any>({
	queryParams: {
		PageIndex: 1, // 当前页
		PageSize: 20, // 每页条数
		// 查询条件
		No: null, // 工单编码
		Title: null,
		HotspotId: null,
		AcceptTypeCode: null,
		OrgCode: null,
		AreaCode: null,
		crTime: [],
		CreationTimeStart: null,
		CreationTimeEnd: null,
		NameOrNo: null,
		IsTheClueTrueText: '0', // 快捷查询
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
	acceptTypeOptions: [], //受理类型
	channelOptions: [], // 来源频道
	orgsOptions: [], // 部门
	enforcementHotspot: [], // 热点类型
	areaOptions: [], // 省市区数据
});
const checkboxConfig = reactive<any>({
	// labelField: 'id',
	checkMethod: ({ row }) => {
		return row.isTheClueTrue === null;
	},
});

// 获取查询条件基础信息
const treeList = ref<any[]>([]);
const getBaseData = async () => {
	try {
		const res: any = await listBaseData();
		const mappings: any = {
			acceptTypeOptions: 'acceptTypeOptions',
			channelOptions: 'channelOptions',
			orgsOptions: 'orgsOptions',
			enforcementHotspot: 'enforcementHotspot',
		};
		for (const key in mappings) {
			state[key] = res.result?.[mappings[key]] ?? [];
		}
		const area = await treeArea();
		state.areaOptions = area?.result ?? []; //省市区数据
		const baseRes = await getCluesBaseData(); // 事项分类
		treeList.value = baseRes.result ?? [];
	} catch (error) {
		console.log(error);
	}
};
/** 搜索按钮操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
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;
	getWorkList(requestParams.value)
		.then((response: any) => {
			state.tableData = response?.result.items ?? [];
			state.total = response?.result.total;
			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 verifyRef = ref<RefType>();
const onVerify = (row: any) => {
	const data = row ? row : checkTable.value;
	verifyRef.value.openDialog(data);
};
// 修改线索
const onEdit = (row: any) => {
	verifyRef.value.openDialog(row);
};
const tableRef = ref<RefType>();
const checkTable = ref<EmptyArrayType>([]);
const selectAllChangeEvent = ({ checked }) => {
	if (tableRef.value) {
		const records = tableRef.value.getCheckboxRecords();
		checkTable.value = records;
		console.log(checked ? '所有勾选事件' : '所有取消事件', records);
	}
};

const selectChangeEvent = ({ checked }) => {
	if (tableRef.value) {
		const records = tableRef.value.getCheckboxRecords();
		checkTable.value = records;
		console.log(checked ? '勾选事件' : '取消事件', records);
	}
};
const isChecked = computed(() => {
	return !Boolean(checkTable.value.length);
});
const toolbarRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
	getBaseData();
});
</script>
