<template>
	<div class="business-copy-view-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<vxe-grid v-bind="gridOptions" ref="gridRef" @checkbox-all="selectAllChangeEvent" @checkbox-change="selectChangeEvent">
				<template #form>
					<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline :disabled="gridOptions.loading">
						<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="工单标题" prop="Title">
							<el-input v-model.trim="state.queryParams.Title" 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 #toolbar_buttons>
					<el-button type="primary" @click="onJbExport" :disabled="isChecked" :loading="state.loading"
						><SvgIcon name="iconfont icon-daochu" class="mr5" />交办单导出<span v-if="checkTable.length">({{ checkTable.length }})</span></el-button
					>
				</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">
				<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="exTime">
					<el-date-picker
						v-model="state.queryParams.exTime"
						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="ActualHandleOrgName">
					<el-input v-model="state.queryParams.ActualHandleOrgName" placeholder="请填写接办部门名称" clearable @keyup.enter="handleQuery" />
				</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 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="state.queryParams.Hotspot" 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="businessCopyView">
import { computed, defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import { exportAssignment } from '@/utils/tools';
import Other from '@/utils/other';
import { countersignBase } from '@/api/query/countersign';
import { copyViewBaseData, copyViewList, copyViewListCount } from '@/api/business/copyView';
// 引入组件
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive<any>({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 20,
		Keyword: null, // 关键字
		No: null,
		Title: null,
		slTime: [], // 受理时间
		StartTime: null,
		EndTime: null,
		exTime: [], // 期满时间
		ExpiredTimeStart: null,
		ExpiredTimeEnd: null,
		ActualHandleOrgName: null, // 接办部门
		AcceptType: null, //受理类型
		Hotspot: null, // 热点分类
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
});

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();
			},
		},
		slots: {
			buttons: 'toolbar_buttons',
		},
	},
	customConfig: {
		storage: true,
	},
	id: 'businessCopyView',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	columns: [
		{ type: 'checkbox', width: 50, align: 'center' },
		{
			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: 'sourceChannel', title: '来源渠道', width: 110 },
		{ field: 'currentStepName', title: '当前节点', width: 120 },
		{ field: 'no', title: '工单编码', width: 140 },
		{
			field: 'startTime',
			title: '受理时间',
			width: 160,
			formatter: 'formatDate',
		},
		{
			field: 'title',
			title: '工单标题',
			minWidth: 200,
			slots: { default: 'order_detail' },
		},

		{
			field: 'expiredTime',
			title: '工单期满时间',
			width: 160,
			formatter: 'formatDate',
		},
		{ field: 'actualHandleOrgName', title: '接办部门', width: 140 },
		{
			field: 'filedTime',
			title: '办结时间',
			width: 160,
			formatter: 'formatDate',
		},

		{ field: 'acceptType', title: '受理类型', width: 110 },
		{ field: 'hotspotName', title: '热点分类', width: 150 },
	],
	data: [],
	sortConfig: {
		remote: true,
	},
});
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
	getTotal();
};
/** 获取列表 */
const requestParams = ref<EmptyObjectType>({});
const queryList = () => {
	state.loading = true;
	gridOptions.loading = true;
	requestParams.value = Other.deepClone(state.queryParams);
	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.ExpiredTimeStart = state.queryParams.exTime === null ? null : state.queryParams.exTime[0]; // 期满时间
	requestParams.value.ExpiredTimeEnd = state.queryParams.exTime === null ? null : state.queryParams.exTime[1];
	Reflect.deleteProperty(requestParams.value, 'exTime'); // 删除无用的参数
	state.loading = false;
	copyViewList(requestParams.value)
		.then((res: any) => {
			gridOptions.data = res?.result ?? [];
			state.loading = false;
			gridOptions.loading = false;
			gridRef.value.clearCheckboxRow();
			checkTable.value = [];
		})
		.catch(() => {
			state.loading = false;
			gridOptions.loading = false;
			gridRef.value.clearCheckboxRow();
			checkTable.value = [];
		});
};

// 查询总数
const getTotal = () => {
	copyViewListCount(requestParams.value)
		.then((res) => {
			state.total = res.result ?? 0;
		})
		.catch(() => {});
};
/** 重置按钮操作 */
const drawerRuleFormRef = ref<RefType>();
const drawer = ref(false);
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	ruleFormRef.value?.resetFields();
	queryList();
	getTotal();
};
// 交办单导出
const onJbExport = () => {
	const ids = checkTable.value.map((item: any) => item.id);
	exportAssignment(ids);
};
const checkTable = ref<EmptyArrayType>([]);
const gridRef = ref<RefType>();
const selectAllChangeEvent = ({ checked }) => {
	if (gridRef.value) {
		const records = gridRef.value.getCheckboxRecords();
		checkTable.value = records;
		console.log(checked ? '所有勾选事件' : '所有取消事件', records);
	}
};

const selectChangeEvent = ({ checked }) => {
	if (gridRef.value) {
		const records = gridRef.value.getCheckboxRecords();
		checkTable.value = records;
		console.log(checked ? '勾选事件' : '取消事件', records);
	}
};
const isChecked = computed(() => {
	return !Boolean(checkTable.value.length);
});
// 获取基础信息
const acceptTypeOptions = ref<EmptyArrayType>([]);
const getBaseData = async () => {
	try {
		const { result } = await copyViewBaseData();
		acceptTypeOptions.value = result.acceptTypeOptions;
	} catch (e) {
		console.log(e);
	}
};
onMounted(() => {
	queryList();
	getTotal();
	getBaseData();
});
</script>
