<template>
	<div class="tels-restApply-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<el-form :model="state.queryParams" ref="ruleFormRef" inline @submit.native.prevent >
				<el-form-item label="关键词" prop="KeyWords">
					<el-input v-model="state.queryParams.KeyWords" placeholder="坐席名称/工号" clearable @keyup.enter="queryList" />
				</el-form-item>
				<el-form-item label="申请时间" prop="time">
					<el-date-picker
						v-model="state.queryParams.time"
						type="datetimerange"
						range-separator="至"
						start-placeholder="开始时间"
						end-placeholder="结束时间"
						value-format="YYYY-MM-DD[T]HH:mm:ss"
						:shortcuts="shortcuts"
						:default-time="defaultTimeStartEnd"
						@change="
							(val: any[]) => {
								state.queryParams.BeginTime = val[0];
								state.queryParams.EndTime = val[1];
							}
						"
					/>
				</el-form-item>
				<el-form-item label="小休原因" prop="Reason">
					<el-select v-model="state.queryParams.Reason" placeholder="请选择小休原因" class="w100">
						<el-option v-for="item in state.restReason" :key="item.id" :label="item.content" :value="item.content" />
					</el-select>
				</el-form-item>
				<el-form-item label="审批状态" prop="Status">
					<el-select v-model="state.queryParams.Status" placeholder="请选择审批状态" class="w100">
						<el-option v-for="item in state.restApplyStatus" :key="item.key" :label="item.value" :value="item.key" />
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleQuery" :loading="state.loading" v-waves> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
					<el-button @click="resetQuery(ruleFormRef)" v-waves class="default-button" :loading="state.loading">
						<SvgIcon name="ele-Refresh" class="mr5" />重置
					</el-button>
				</el-form-item>
			</el-form>
			<vxe-toolbar
				ref="toolbarRef"
				:loading="state.loading"
				custom
				:refresh="{
					queryMethod: handleQuery,
				}"
			>
			</vxe-toolbar>
			<div style="overflow: hidden; width: 100%; height: 100%; flex: 1">
				<vxe-table
					border
					:loading="state.loading"
					:data="state.tableData"
					:sort-config="{ remote: true }"
					: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="restApply"
					:custom-config="{ storage: true }"
				>

					<vxe-column field="userName" title="坐席" width="140"></vxe-column>
					<vxe-column field="creationTime" title="申请时间" sortable width="160">
						<template #default="{ row }">
							{{ formatDate(row.creationTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="creationTime" title="开始时间" sortable width="160">
						<template #default="{ row }">
							{{ formatDate(row.creationTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="creationTime" title="结束时间" sortable width="160">
						<template #default="{ row }">
							{{ formatDate(row.creationTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="restDuration" title="时长（分钟）" width="140">
						<template #default="{ row }">
							{{ (row.restDuration / 60).toFixed(2) }}
						</template>
					</vxe-column>
					<vxe-column field="applyStatus" title="审批状态" width="140">
						<template #default="{ row }">
							{{ state.restApplyStatus[row.applyStatus].value }}
						</template>
					</vxe-column>
					<vxe-column field="restReason" title="小休原因" min-width="140"></vxe-column>
					<vxe-column title="操作" fixed="right" width="140" align="center">
						<template #default="{ row }">
							<el-button link type="primary" @click="onRecord(row)" v-auth="'system:workflow:record'" title="审批记录" v-if="row.workflowId">
								审批记录
							</el-button>
							<el-button link type="primary" @click="onSubmit(row)" v-auth="'system:workflow:handle'" title="审批" v-if="[0].includes(row.applyStatus)">
								审批
							</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>
		<!-- 流程审配 -->
		<process-audit ref="processAuditRef" @orderProcessSuccess="orderProcessSuccess" />
		<!-- 流转记录 -->
		<audit-record ref="AuditRecordRef" />
	</div>
</template>

<script lang="tsx" setup name="restApply">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import type { FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { baseData, restApplyPaged } from '@/api/tels/restApply';
import { debounce } from '@/utils/tools';
import { commonEnum, defaultTimeStartEnd, shortcuts } from "@/utils/constants";
import other from '@/utils/other';
// 引入组件
const AuditRecord = defineAsyncComponent(() => import('@/components/AuditRecord/index.vue')); // 流转记录
const ProcessAudit = defineAsyncComponent(() => import('@/components/ProcessAudit/index.vue')); // 审批流程
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页

// 定义变量内容
const state = reactive<any>({
	queryParams: {
		PageIndex: 1, // 当前页码
		PageSize: 10, // 每页条数
		KeyWords: null, // 关键字
		Reason: null, // 小休原因
		Status: null, // 审批状态
		time: [], // 申请时间
		BeginTime: null, // 开始时间
		EndTime: null, // 结束时间
	},
	loading: false, // 加载状态
	total: 0, // 总条数
	tableData: [], // 表格数据
	restApplyStatus: [], // 审批状态
	restReason: [], // 小休原因
});
const ruleFormRef = ref<FormInstance>(); // 表单实例
/** 搜索按钮操作 节流操作 */
const handleQuery = debounce(() => {
	state.queryParams.PageIndex = 1;
	queryList();
}, 300);
const orderProcessSuccess = () => {
	console.log('审批成功');
	handleQuery();
};
/** 获取用户列表 */
const queryList = () => {
	state.loading = true;
	let request = other.deepClone(state.queryParams);
	Reflect.deleteProperty(request, 'time'); // 删除无用的参数
	restApplyPaged(request)
		.then((response: any) => {
			state.tableData = response?.result.items;
			state.total = response?.result.total;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
/** 重置按钮操作 */
const resetQuery = debounce((formEl: FormInstance | undefined) => {
	if (!formEl) return;
	state.queryParams.BeginTime = null;
	state.queryParams.EndTime = null;
	formEl.resetFields();
	handleQuery();
}, 300);
// 办理 下一步
const processAuditRef = ref<RefType>();
const onSubmit = debounce((row: any) => {
	const params = {
		id: row.workflowId,
		commonEnum: commonEnum.OrderCirculation,
		processType: '小休办理',
		extra: {
			dialogTitle: '小休办理',
			inputPlaceholder: '办理意见',
			annexName: '办理附件',
		},
	};
	processAuditRef.value.openDialog(params);
}, 1000);
// 审核记录
const AuditRecordRef = ref<RefType>();
const onRecord = (row: any) => {
	const params = {
		dialogTitle: '审核记录 （小休申请）',
		...row,
	};
	console.log(params);
	AuditRecordRef.value.openDialog(params);
};
// 获取基础数据
const baseDataFn = () => {
	baseData().then((res: any) => {
		state.restApplyStatus = res.result?.restApplyStatus;
		state.restReason = res.result?.restReason;
		queryList();
	});
};
onMounted(() => {
	baseDataFn();
});
</script>
