<template>
	<div class="business-second-handle-audit-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<el-tabs v-model="state.queryParams.Status" @tab-change="handleQuery" :disabled="state.loading">
				<el-tab-pane :name="1" label="待审批" :disabled="state.loading"></el-tab-pane>
				<el-tab-pane :name="4" label="已审批" :disabled="state.loading"></el-tab-pane>
			</el-tabs>
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline :disabled="state.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>
			<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="onAuditMultiple"
						v-auth="'business:secondHandle:audit:multiple'"
						:disabled="isChecked"
						v-if="state.queryParams.Status === 1"
						>批量审批<span v-if="checkTable.length">({{checkTable.length}})</span>
					</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"
					@checkbox-all="selectAllChangeEvent"
					@checkbox-change="selectChangeEvent"
					height="auto"
					auto-resize
					show-overflow
					:scrollY="{ enabled: true, gt: 100 }"
					id="businessSecondHandleAudit"
					:custom-config="{ storage: true }"
					showHeaderOverflow
					:params="{ exportMethod: secondHandleAuditExport, exportParams: requestParams }"
				>
					<vxe-column type="checkbox" width="60" align="center" :visible="state.queryParams.Status === 1"></vxe-column>
					<vxe-column field="order.expiredStatusText" title="状态" width="60" align="center">
						<template #default="{ row }">
							<span :class="'overdue-status-' + row.order?.expiredStatus" :title="row.order?.expiredStatusText"></span>
						</template>
					</vxe-column>
					<vxe-column field="order.no" title="工单编码" width="140"></vxe-column>
					<vxe-column field="stateText" title="办理状态" width="100"></vxe-column>
					<vxe-column field="order.sourceChannel" title="来源渠道" width="110"></vxe-column>
					<vxe-column field="sendBack" title="重提办理" width="90"></vxe-column>
					<vxe-column field="order.title" title="工单标题" min-width="200">
						<template #default="{ row }">
							<order-detail :order="row.order" @updateList="refreshList">{{ row.order?.title }}</order-detail>
						</template>
					</vxe-column>
					<vxe-column field="creatorName" title="申请人" width="120"></vxe-column>
					<vxe-column field="creatorOrgName" title="申请部门" width="140"></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="order.acceptType" title="受理类型" width="100"></vxe-column>
					<vxe-column field="order.hotspotName" title="热点分类" width="140"></vxe-column>
					<vxe-column title="操作" fixed="right" width="120" align="center">
						<template #default="{ row }">
							<el-button link type="primary" @click="onView(row)" title="查看办理明细" v-show="![1].includes(row.state)"> 办理明细 </el-button>
							<el-button
								link
								type="primary"
								@click="onReturn(row)"
								title="退回"
								v-auth="'business:secondHandle:return'"
								v-show="[1].includes(row.state)"
							>
								退回
							</el-button>
							<el-button link type="primary" @click="onAudit(row)" title="审批" v-auth="'business:secondHandle:audit'" v-show="[1].includes(row.state)">
								审批
							</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>
		<second-handle-return ref="secondHandleReturnRef" @updateList="refreshList" />
		<second-handle-audit ref="secondHandleAuditRef" @updateList="refreshList" />
		<second-handle-audit-multiple ref="secondHandleAuditMultipleRef" @updateList="refreshList" />
		<!--  办理明细  -->
		<second-handle-detail ref="secondHandleDetailRef" @updateList="refreshList" />
		<!--	更多查询	-->
		<el-drawer v-model="drawer" title="更多查询" size="500px">
			<el-form :model="state.queryParams" ref="drawerRuleFormRef" @submit.native.prevent label-width="100px" :disabled="state.loading">
				<el-form-item prop="Channel" label="来源渠道">
					<el-select v-model="state.queryParams.Channel" placeholder="请选择来源渠道" clearable class="w100" @change="handleQuery">
						<el-option v-for="item in channelOptions" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
					</el-select>
				</el-form-item>
				<el-form-item prop="SendBack" label="重提办理">
					<el-select v-model="state.queryParams.SendBack" placeholder="请选择重提办理" clearable class="w100" @change="handleQuery">
						<el-option :value="1"  label="是" />
						<el-option :value="0"  label="否" />
					</el-select>
				</el-form-item>
				<el-form-item label="申请人" prop="ApplyName">
					<el-input v-model.trim="state.queryParams.ApplyName" placeholder="申请人" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="申请部门" prop="ApplyOrgName">
					<el-input v-model.trim="state.queryParams.ApplyOrgName" placeholder="申请部门" clearable @keyup.enter="handleQuery" />
				</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 prop="AcceptTypeCode" label="受理类型">
					<el-select v-model="state.queryParams.AcceptTypeCode" placeholder="请选择受理类型" clearable class="w100" @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.trim="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="businessSecondHandleAudit">
import { computed, defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { secondHandleAuditExport, secondHandleAuditList, secondHandleBase } from '@/api/business/secondHandle';
import { defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import other from '@/utils/other';
import { orderBaseData } from '@/api/business/order';
// 引入组件
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const SecondHandleReturn = defineAsyncComponent(() => import('@/views/business/secondHandle/components/Second-handle-return.vue')); // 二次办理退回
const SecondHandleAudit = defineAsyncComponent(() => import('@/views/business/secondHandle/components/Second-handle-audit.vue')); // 二次办理审批
const SecondHandleAuditMultiple = defineAsyncComponent(() => import('@/views/business/secondHandle/components/Second-handle-audit-multiple.vue')); // 二次办理审批批量
const SecondHandleDetail = defineAsyncComponent(() => import('@/views/business/secondHandle/components/Second-handle-detail.vue')); // 办理明细
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
// 定义变量内容
const state = reactive<any>({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 20,
		Status: 1, // 状态
		crTime: [], // 申请时间
		CreationTimeStart: null, // 申请时间开始
		CreationTimeEnd: null, // 申请时间结束
		No:null, // 工单编码
		Title: null, // 工单标题
		Channel: null, // 来源渠道
		SendBack: null, // 重提办理
		ApplyName: null, // 申请人
		ApplyOrgName: null, // 申请部门
		AcceptTypeCode: null, // 受理类型
		Hotspot: null, // 热点分类
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
	statusOptions: [],
});
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
// 刷新列表
const refreshList = () => {
  queryList();
};
/** 获取列表 */
const requestParams = ref<EmptyObjectType>({});
const queryList = () => {
	state.loading = true;
	requestParams.value = other.deepClone(state.queryParams);
	requestParams.value.StCreationTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[0];
	requestParams.value.EndCreationTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
	Reflect.deleteProperty(requestParams.value, 'crTime');
	secondHandleAuditList(requestParams.value)
		.then((res) => {
			state.tableData = res?.result.items ?? [];
			state.total = res?.result.total;
			state.loading = false;
			tableRef.value.clearCheckboxRow();
			checkTable.value = [];
		})
		.finally(() => {
			state.loading = false;
			tableRef.value.clearCheckboxRow();
			checkTable.value = [];
		});
};

/** 重置按钮操作 */
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 secondHandleAuditMultipleRef = ref<RefType>();
const onAuditMultiple = () => {
	const ids = checkTable.value.map((item: any) => item.id);
	secondHandleAuditMultipleRef.value.openDialog(ids);
};
// 查看详情
const secondHandleDetailRef = ref<RefType>();
const onView = (row: any) => {
	secondHandleDetailRef.value.openDialog(row.id);
};
// 退回
const secondHandleReturnRef = ref<RefType>();
const onReturn = (row: any) => {
	secondHandleReturnRef.value.openDialog(row);
};
// 审批
const secondHandleAuditRef = ref<RefType>();
const onAudit = (row: any) => {
	secondHandleAuditRef.value.openDialog(row);
};
// 获取基础数据
const channelOptions = ref<EmptyArrayType>([]); // 来源渠道
const acceptTypeOptions = ref<EmptyArrayType>([]); // 受理类型
const getBaseData = async () => {
	try {
		const res = await secondHandleBase();
		state.statusOptions = res.result?.status ?? [];

		const { result } = await orderBaseData();
		channelOptions.value = result.channelOptions ?? [];
		acceptTypeOptions.value = result.acceptTypeOptions ?? [];
	} catch (e) {
		console.log(e);
	}
};
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 !checkTable.value.length;
});
const toolbarRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
	getBaseData();
});
</script>
