<template>
	<div class="tels-callLog-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline>
				<el-form-item prop="UserName">
					<el-input v-model="state.queryParams.UserName" placeholder="坐席人员" clearable @keyup.enter="handleQuery" class="keyword-input" />
				</el-form-item>
				<el-form-item prop="StaffNo">
					<el-input v-model="state.queryParams.StaffNo" placeholder="工号" clearable @keyup.enter="handleQuery" class="keyword-input" />
				</el-form-item>
				<el-form-item prop="GroupId">
					<el-input v-model="state.queryParams.GroupId" placeholder="工作组" clearable @keyup.enter="handleQuery" class="keyword-input" />
				</el-form-item>
				<el-form-item prop="OperateState">
					<el-select v-model="state.queryParams.OperateState" placeholder="动作类型" clearable class="w100" @change="handleQuery">
						<el-option v-for="item in state.operations" :value="item.key" :key="item.key" :label="item.value" />
					</el-select>
				</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="resetQuery(ruleFormRef)" 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="telsPhoneAction"
					:custom-config="{ storage: true }"
				>
					<vxe-column field="userName" title="坐席人员" min-width="140"></vxe-column>
					<vxe-column field="telNo" title="分机号" min-width="140"></vxe-column>
					<vxe-column field="staffNo" title="工号" min-width="140"></vxe-column>
					<vxe-column field="groupId" title="工作组" min-width="140"></vxe-column>
					<vxe-column field="operateStateText" title="动作类型" min-width="200"></vxe-column>
					<vxe-column field="operateTime" title="开始时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.operateTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
				</vxe-table>
			</div>
			<div class="flex-end">
				<el-pagination
					layout="prev, pager, next"
					:total="state.total"
					@current-change="handleCurrentChange"
					:page-size="state.queryParams.PageSize"
					:current-page="state.queryParams.PageIndex"
				/>
			</div>
		</div>
	</div>
</template>

<script lang="tsx" setup name="telsPhoneAction">
import { onMounted, reactive, ref } from 'vue';
import type { FormInstance } from 'element-plus';
import { getNeedArr } from '@/utils/tools';
import { formatDate } from '@/utils/formatTime';
import Other from '@/utils/other';
import { getCallCenterOperateRecord, getCallCenterOperateRecordBaseData } from '@/api/callCenter';

// 定义变量内容
const state = reactive<any>({
	queryParams: {
		PageIndex: 1, // 当前页
		PageSize: 20, // 每页条数
		UserName: null, // 话务员名称
		StaffNo: null, // 工号
		GroupId: null, // 工作组
		OperateState: null, // 操作类型
	},
	tableData: [], // 列表数据
	loading: false, // 加载
	total: 0, // 总条数
	callDirection: [],
	operations: [],
});
const ruleFormRef = ref<FormInstance>(); // 表单ref
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryIndex.value = 0;
	queryList(true);
};
// 改变页码
const queryIndex = ref(0); // 数据批次
const totalTable = ref([]); // 数据总数
const handleCurrentChange = (val: number) => {
	state.queryParams.PageIndex = val;
	// 判断当前页是否是数据的最后一页
	if (val === Math.ceil(state.total / state.queryParams.PageSize)) {
		queryList();
	} else {
		state.tableData = getNeedArr(totalTable.value, state.queryParams.PageSize)[state.queryParams.PageIndex - 1]; //当前页的表格数据
	}
};
/** 通话记录列表 */
const requestParams = ref<EmptyObjectType>({});
const queryList = async (isQuery: boolean = false) => {
	state.loading = true;
	requestParams.value = Other.deepClone(state.queryParams);
	requestParams.value.QueryIndex = queryIndex.value; // 数据批次
	state.loading = true;
	getCallCenterOperateRecord(requestParams.value)
		.then((response: any) => {
			if (isQuery) {
				// 如果是查询
				totalTable.value = []; // 先清空
				totalTable.value = response?.result;
				state.total = totalTable.value.length;
				state.tableData = getNeedArr(totalTable.value, state.queryParams.PageSize)[state.queryParams.PageIndex - 1]; //当前页的表格数据
				queryIndex.value = 1;
			} else {
				if (response?.result.length) queryIndex.value++;
				totalTable.value = totalTable.value.concat(response?.result);
				state.total = totalTable.value.length;
				state.tableData = getNeedArr(totalTable.value, state.queryParams.PageSize)[state.queryParams.PageIndex - 1]; //当前页的表格数据
			}
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryIndex.value = 0;
	queryList(true);
};
// 基础信息
const getBaseData = async () => {
	try {
		const { result } = await getCallCenterOperateRecordBaseData();
		state.operations = result.operations;
	} catch (e) {
		console.log(e);
	}
};
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	getBaseData();
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
