<template>
	<div class="statistics-call-seats-rest-container layout-pd">
		<!-- 搜索  -->
		<el-card shadow="never">
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline>
				<el-form-item label="工号" prop="StaffNo">
					<el-input v-model="state.queryParams.StaffNo" placeholder="工号" clearable @keyup.enter="queryList" class="keyword-input" />
				</el-form-item>
				<el-form-item label="坐席名称" prop="UserName">
					<el-input v-model="state.queryParams.UserName" placeholder="坐席名称" clearable @keyup.enter="queryList" class="keyword-input" />
				</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="queryList"
						value-format="YYYY-MM-DD[T]HH:mm:ss"
					/>
				</el-form-item>
				<el-form-item label-width="0">
					<el-button type="primary" @click="queryList" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
					<el-button @click="resetQuery(ruleFormRef)" 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">
			<ProTable
				ref="proTableRef"
				:columns="columns"
				:data="state.tableData"
				@updateTable="queryList"
				:loading="state.loading"
				:pagination="false"
				border
				@sort-change="sortChange"
			>
			</ProTable>
		</el-card>
	</div>
</template>
<script setup lang="tsx" name="statisticsCallSeatsRest">
import { onMounted, reactive, ref } from 'vue';
import { ElButton, FormInstance } from 'element-plus';
import { throttle } from '@/utils/tools';
import { callRest } from '@/api/statistics/call';
import { shortcuts } from '@/utils/constants';

import dayjs from 'dayjs';

const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([
	{ type: 'index', fixed: 'left', width: 55, label: '序号', align: 'center' },
	{ prop: 'staffNo', label: '工号', align: 'center' },
	{ prop: 'userName', label: '坐席名称', align: 'center' },
	{ prop: 'restCount', label: '小休次数', align: 'center', sortable: 'custom' },
	{
		prop: 'restDuration',
		label: '小休平均时长(分)',
		align: 'center',
		sortable: 'custom',
		render: (scope) => <span>{Number(scope.row.restDuration).toFixed(2)}</span>,
	},
]);
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive({
	queryParams: {
		// 查询条件
		StaffNo: null, // 工号
		UserName: null, // 坐席名称
		SortField: 'restDuration', // 排序字段
		SortRule: 1, // 排序规则
		crTime: [dayjs().startOf('day').format('YYYY-MM-DD HH:mm:ss'), dayjs().endOf('day').format('YYYY-MM-DD HH:mm:ss')],
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
});

/** 获取列表 */
const queryList = throttle(() => {
	state.loading = true;
	let StartTime = null;
	let EndTime = null;
	if (state.queryParams?.crTime) {
		StartTime = state.queryParams?.crTime[0];
		EndTime = state.queryParams?.crTime[1];
	}
	const request = {
		StartTime,
		EndTime,
		StaffNo: state.queryParams.StaffNo,
		UserName: state.queryParams.UserName,
		Keyword: state.queryParams.Keyword,
		SortField: state.queryParams.SortField,
		SortRule: state.queryParams.SortRule,
	};
	callRest(request)
		.then((res: any) => {
			state.tableData = res.result ?? [];
			state.total = res.result?.total ?? 0;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
}, 300);
// 排序
const sortChange = (val: any) => {
	state.queryParams.SortField = val.order ? val.prop : null;
	// 0 升序 1 降序
	state.queryParams.SortRule = val.order ? (val.order == 'descending' ? 1 : 0) : null;
	queryList();
};
/** 重置按钮操作 */
const resetQuery = throttle((formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
}, 300);
onMounted(() => {
	queryList();
});
</script>
