<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"
						@change="
							(val) => {
								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"> <SvgIcon name="ele-Refresh" class="mr5" />重置 </el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card shadow="never">
			<div class="flex-center-between mb20">
				<p class="table-title">信息列表</p>
				<div></div>
			</div>
			<!-- 表格 -->
			<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="审批记录"> 审批记录 </el-button>
						<el-button
							link
							type="primary"
							@click="onSubmit(row, '提交', 'next')"
							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 ref="processRef" @submitProcessSuccess="queryList" @submitProcessFailed="queryList" />
		<!-- 流转记录 -->
		<process-record ref="ProcessRecordRef" />
	</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, commonEnum } from '/@/utils/tools';
import other from '/@/utils/other';
// 引入组件
const ProcessRecord = defineAsyncComponent(() => import('/@/views/tels/restApply/components/Process-record.vue'));
const Process = defineAsyncComponent(() => import('/@/views/business/order/components/Order-process.vue'));

// 定义变量内容
const state = reactive<any>({
	queryParams: {
		PageIndex: 1,
		PageSize: 10,
		time: [],
	},
	loading: false,
	total: 0,
	tableData: [],
	restApplyStatus: [],
	restReason: [],
	multipleSelection: [],
});
const ruleFormRef = ref<FormInstance>();
const processRef = ref<RefType>();
const ProcessRecordRef = ref<RefType>();
/** 搜索按钮操作 节流操作 */
const handleQuery = debounce(() => {
	state.queryParams.PageIndex = 1;
	queryList();
}, 500);
/** 获取用户列表 */
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;
	formEl.resetFields();
	handleQuery();
}, 500);
// 办理 下一步
const onSubmit = debounce((row: any, title: string, type?: string) => {
	processRef.value.openDialog({ id: row.workflowId, title, processType: type, commonEnum: commonEnum.OrderCirculation });
}, 1000);
// 审核记录
const onRecord = (row: any) => {
	const params = {
		title: '小休申请',
		...row,
	};
	ProcessRecordRef.value.openDialog(params);
};
// 获取基础数据
const baseDataFn = () => {
  baseData().then((res: any) => {
		state.restApplyStatus = res.result?.restApplyStatus;
		state.queryParams.Status = res.result?.restApplyStatus[0].key;
		state.restReason = res.result?.restReason;
		queryList();
	});
};
onMounted(() => {
	baseDataFn();
});
</script>
