<template>
	<div class="tels-restApply-container layout-pd">
		<el-card shadow="never">
			<!-- 通用搜索 -->
			<el-form :model="state.queryParams" ref="ruleFormRef" :inline="true" @submit.native.prevent class="mt15">
				<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"
						@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>
		</el-card>
		<el-card shadow="never">
			<!-- 表格 -->
			<el-table :data="state.tableData" v-loading="state.loading" row-key="id">
				<el-table-column type="index" width="60" label="序号" />
				<el-table-column prop="userName" label="坐席" show-overflow-tooltip>
					<template #default="{ row }">
						<span
							>{{ row.userName }} <span v-if="row.telNo">[{{ row.telNo }}]</span></span
						>
					</template>
				</el-table-column>
				<el-table-column prop="creationTime" label="申请时间" show-overflow-tooltip>
					<template #default="{ row }">
						<span>{{ formatDate(row.creationTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="startTime" label="开始时间" show-overflow-tooltip>
					<template #default="{ row }">
						<span>{{ formatDate(row.startTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="endTime" label="结束时间" show-overflow-tooltip>
					<template #default="{ row }">
						<span>{{ formatDate(row.endTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="restDuration" label="时长（分钟）" show-overflow-tooltip>
					<template #default="{ row }">
						<span>{{ (row.restDuration / 60).toFixed(2) }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="applyStatus" label="审批状态" show-overflow-tooltip>
					<template #default="{ row }">
						<span>{{ state.restApplyStatus[row.applyStatus].value }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="reason" label="小休原因" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" width="140" fixed="right" 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>
				</el-table-column>
				<template #empty>
					<Empty />
				</template>
			</el-table>
			<!-- 分页 -->
			<pagination
				:total="state.total"
				v-model:page="state.queryParams.PageIndex"
				v-model:limit="state.queryParams.PageSize"
				@pagination="queryList"
			/>
		</el-card>

		<!-- 流程 -->
		<process-approval ref="processApprovalRef" @orderProcessSuccess="orderProcessSuccess" @orderProcessFailed="handleQuery" />

		<!-- 流转记录 -->
		<audit-record ref="AuditRecordRef" />
	</div>
</template>

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

// 定义变量内容
const state = reactive<any>({
	queryParams: {
		PageIndex: 1, // 当前页码
		PageSize: 10, // 每页条数
		KeyWords: '', // 关键字
		Reason: '', // 小休原因
		Status: '', // 审批状态
		time: [], // 申请时间
		BeginTime: '',  // 开始时间
		EndTime: '',  // 结束时间
	},
	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 = '';
	state.queryParams.EndTime = '';
	formEl.resetFields();
	handleQuery();
}, 300);
// 办理 下一步
const processApprovalRef = ref<RefType>();
const onSubmit = debounce((row: any) => {
  const params = {
    id: row.workflowId,
    commonEnum: commonEnum.OrderCirculation,
    processType: '小休办理',
    extra: {
      dialogTitle: '小休办理',
      inputPlaceholder: '办理意见',
      annexName: '办理附件',
    },
  }
  processApprovalRef.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>
