<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 :disabled="state.loading">
				<el-form-item prop="crTime">
					<statistical-time v-model="state.queryParams.crTime" @change="handleQuery" ref="statisticalTimeRef" :disabled="state.loading" />
				</el-form-item>
        <el-form-item prop="OperateState" label="动作类型">
          <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 prop="UserName" label="坐席人员">
					<el-input v-model="state.queryParams.UserName" placeholder="坐席人员" clearable @keyup.enter="handleQuery" class="keyword-input" />
				</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"> <SvgIcon name="ele-Search" 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-column field="endTime" title="结束时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.endTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="duration" title="时长(秒)" min-width="120"> </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-item label="分机号" prop="TelNo">
        <el-input v-model="state.queryParams.TelNo" placeholder="分机号" clearable @keyup.enter="handleQuery" class="keyword-input" />
      </el-form-item>
			<el-form :model="state.queryParams" ref="drawerRuleFormRef" @submit.native.prevent label-width="90px" :disabled="state.loading">
				<el-form-item prop="StaffNo" label="工号">
					<el-input v-model="state.queryParams.StaffNo" placeholder="工号" clearable @keyup.enter="handleQuery" class="keyword-input" />
				</el-form-item>
				<el-form-item prop="GroupId" label="工作组">
					<el-input v-model="state.queryParams.GroupId" placeholder="工作组" clearable @keyup.enter="handleQuery" class="keyword-input" />
				</el-form-item>
			</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>
	</div>
</template>

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

const StatisticalTime = defineAsyncComponent(() => import('@/components/StatisticalTime/index.vue')); // 日期类型选择组件
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页

// 定义变量内容
const state = reactive<any>({
	queryParams: {
		PageIndex: 1, // 当前页
		PageSize: 20, // 每页条数
		UserName: null, // 话务员名称
		StaffNo: null, // 工号
		GroupId: null, // 工作组
		OperateState: null, // 操作类型
		crTime: defaultDate,
		StartTime: null,
		EndTime: null,
		TelNo: null,
	},
	tableData: [], // 列表数据
	loading: false, // 加载
	total: 0, // 总条数
	callDirection: [],
	operations: [],
});
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
	getTotal();
};
/** 很滑稽动作列表 */
const requestParams = ref<EmptyObjectType>({});
const queryList = async () => {
	state.loading = true;
	requestParams.value = Other.deepClone(state.queryParams);
	requestParams.value.StartTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[0];
	requestParams.value.EndTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
	Reflect.deleteProperty(requestParams.value, 'crTime');
	getCallCenterOperateRecord(requestParams.value)
		.then((response: any) => {
			state.tableData = response?.result ?? [];
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
// 查询总数
const getTotal = () => {
	getCallCenterOperateRecordTotal(requestParams.value)
		.then((res) => {
			state.total = res.result ?? 0;
		})
		.catch(() => {});
};
/** 重置按钮操作 */
const statisticalTimeRef = ref<RefType>();
const drawerRuleFormRef = ref();
const ruleFormRef = ref<RefType>(); // 表单ref
const drawer = ref(false);
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	statisticalTimeRef.value.reset();
	ruleFormRef.value?.resetFields();
	queryList();
};
// 基础信息
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();
	getTotal();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
