<template>
	<div class="tels-restApply-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<el-tabs v-model="state.queryParams.Type" @tab-change="handleQuery">
				<el-tab-pane :name="0" label="待审批" :disabled="state.loading"></el-tab-pane>
				<el-tab-pane :name="1" label="已审批" :disabled="state.loading"></el-tab-pane>
			</el-tabs>
			<el-form :model="state.queryParams" ref="ruleFormRef" inline @submit.native.prevent>
				<el-form-item label="申请人" prop="ApplyUserName">
					<el-input v-model="state.queryParams.ApplyUserName" placeholder="申请人" clearable @keyup.enter="handleQuery" class="keyword-input" />
				</el-form-item>
				<el-form-item label="小休原因" prop="ReasonId">
					<el-select v-model="state.queryParams.ReasonId" placeholder="请选择小休原因" @change="handleQuery" clearable>
						<el-option v-for="item in state.restReason" :key="item.dicDataValue" :label="item.dicDataName" :value="item.dicDataValue" />
					</el-select>
				</el-form-item>
				<el-form-item label="申请时间" prop="crTime">
					<el-date-picker
						v-model="state.queryParams.crTime"
						type="datetimerange"
						range-separator="至"
						start-placeholder="开始时间"
						end-placeholder="结束时间"
						value-format="YYYY-MM-DD[T]HH:mm:ss"
						:shortcuts="shortcuts"
						:default-time="defaultTimeStartEnd"
						@change="handleQuery"
					/>
				</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" :loading="state.loading">
						<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="onAuditBatch"
						:disabled="isChecked"
						:loading="state.loading"
						v-auth="'tels:restApply:audit:batch'"
						v-show="state.queryParams.Type === 0"
					>批量审批</el-button
					>
				</template>
			</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="telsRestApply"
					:custom-config="{ storage: true }"
					:params="{ exportMethod: restApplyExport, exportParams: requestParams }"
					@sort-change="sortChange"
					@checkbox-all="selectAllChangeEvent"
					@checkbox-change="selectChangeEvent"
				>
					<vxe-column type="checkbox" width="50" align="center"></vxe-column>
					<vxe-column field="reason" title="小休原因"></vxe-column>
					<vxe-column field="applyUserName" title="申请人"></vxe-column>
					<vxe-column field="applyTime" title="申请时间" sortable width="160">
						<template #default="{ row }">
							{{ formatDate(row.applyTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="auditStatusText" title="审核结果"> </vxe-column>
					<vxe-column field="auditOpinion" title="审核意见" min-width="140"></vxe-column>
					<vxe-column field="auditUserName" title="审核人" min-width="140"></vxe-column>
					<vxe-column field="auditTime" title="审核时间" sortable width="160">
						<template #default="{ row }">
							{{ formatDate(row.auditTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column title="操作" fixed="right" width="90" align="center">
						<template #default="{ row }">
							<el-button link type="primary" @click="onAudit(row)" v-auth="'tels:restApply:audit'" title="审批" v-show="[0].includes(row.auditStatus)">
								审批
							</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>
		<!-- 更多查询 -->
		<el-drawer v-model="drawer" title="更多查询" size="500px">
			<el-form :model="state.queryParams" ref="drawerRuleFormRef" @submit.native.prevent label-width="100px" :disabled="state.loading">
				<template v-if="state.queryParams.Type === 1">
					<el-form-item label="审批状态" prop="AuditStatus">
						<el-select v-model="state.queryParams.AuditStatus" placeholder="请选择审批状态" class="w100" @change="handleQuery" clearable>
							<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 label="审批人" prop="AuditUserName">
						<el-input v-model="state.queryParams.AuditUserName" placeholder="审批人" clearable @keyup.enter="handleQuery" />
					</el-form-item>
					<el-form-item label="审批时间" prop="spTime">
						<el-date-picker
							v-model="state.queryParams.spTime"
							type="datetimerange"
							range-separator="至"
							start-placeholder="开始时间"
							end-placeholder="结束时间"
							value-format="YYYY-MM-DD[T]HH:mm:ss"
							:shortcuts="shortcuts"
							:default-time="defaultTimeStartEnd"
							@change="handleQuery"
						/>
					</el-form-item>
				</template>
			</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>
		<!-- 审核 -->
		<rest-audit ref="restAuditRef" @updateList="queryList" />
	</div>
</template>

<script lang="tsx" setup name="telsRestApply">
import { computed, defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import type { FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { restBaseData, restApplyExport, restApplyPaged } from '@/api/tels/restApply';
import { debounce } from '@/utils/tools';
import { defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import Other from '@/utils/other';
// 引入组件
const RestAudit = defineAsyncComponent(() => import('@/views/tels/restApply/components/Audit.vue')); // 审批
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页

// 定义变量内容
const state = reactive<any>({
	queryParams: {
		PageIndex: 1, // 当前页码
		PageSize: 10, // 每页条数
		Type: 0, // 默认待审批
		ApplyUserName: null, // 申请人
		ReasonId: null, // 小休原因
		crTime: [], // 申请时间
		ApplyStartTime: null, // 开始时间
		ApplyEndTime: null, // 结束时间
		AuditStatus: null, // 审批状态
		AuditUserName: null, // 审批人
		spTime: [], // 审批时间
		AuditStartTime: null, // 审批开始时间
		AuditEndTime: null, // 审批结束时间
		SortRule: null, // 排序规则
		SortField: null, // 排序字段
	},
	loading: false, // 加载状态
	total: 0, // 总条数
	tableData: [], // 表格数据
	restApplyStatus: [], // 审批状态
	restReason: [], // 小休原因
});
const ruleFormRef = ref<FormInstance>(); // 表单实例
/** 搜索按钮操作 节流操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取用户列表 */
const requestParams = ref<EmptyObjectType>({});
const queryList = () => {
	state.loading = true;
	requestParams.value = Other.deepClone(state.queryParams);
	requestParams.value.ApplyStartTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[0];
	requestParams.value.ApplyEndTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
	Reflect.deleteProperty(requestParams.value, 'crTime'); // 申请时间
	requestParams.value.AuditStartTime = state.queryParams.spTime === null ? null : state.queryParams.spTime[0];
	requestParams.value.AuditEndTime = state.queryParams.spTime === null ? null : state.queryParams.spTime[1];
	Reflect.deleteProperty(requestParams.value, 'spTime'); // 审批时间
	restApplyPaged(requestParams.value)
		.then((response: any) => {
			state.tableData = response?.result.items;
			state.total = response?.result.total;
			tableRef.value?.clearCheckboxRow();
			checkTable.value = [];
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
// 排序
const sortChange = (val: any) => {
	state.queryParams.SortField = val.order ? val.field : null;
	// 0 升序 1 降序
	state.queryParams.SortRule = val.order ? (val.order == 'desc' ? 1 : 0) : null;
	handleQuery();
};
/** 重置按钮操作 */
const drawer = ref(false);
const drawerRuleFormRef = ref();
const resetQuery = debounce((formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	ruleFormRef.value?.resetFields();
	handleQuery();
}, 300);
// 审核
const restAuditRef = ref<RefType>();
const onAudit = (row: any) => {
	restAuditRef.value.openDialog([row.id]);
};
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 onAuditBatch = () => {
	const ids = checkTable.value.map((row: any) => row.id);
	restAuditRef.value.openDialog(ids);
};
// 获取基础数据
const baseDataFn = () => {
	restBaseData().then((res: any) => {
		state.restApplyStatus = res.result?.auditStatus;
		state.restApplyStatus = state.restApplyStatus.filter((item: any) => {
			return [1, 2].includes(item.key);
		});
		state.restReason = res.result?.restReason;
	});
};
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	queryList();
	baseDataFn();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
