<template>
	<div class="statistics-call-telephone-container layout-pd">
		<!-- 搜索  -->
		<el-card shadow="never">
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent class="mt15" label-width="100px">
				<el-row :gutter="10">
					<el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="8">
						<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-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="8">
						<el-form-item label="坐席人员" prop="Keyword">
							<el-input v-model="state.queryParams.Keyword" placeholder="坐席人员" clearable @keyup.enter="queryList" />
						</el-form-item>
					</el-col>
					<transition name="el-zoom-in-top">
						<el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="8" v-show="!searchCol">
							<el-form-item label="工号" prop="Keyword">
								<el-input v-model="state.queryParams.Keyword" placeholder="工号" clearable @keyup.enter="queryList" />
							</el-form-item>
						</el-col>
					</transition>
					<transition name="el-zoom-in-top">
						<el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="8" v-show="!searchCol">
							<el-form-item label="工作组" prop="Keyword">
								<el-input v-model="state.queryParams.Keyword" placeholder="工作组" clearable @keyup.enter="queryList" />
							</el-form-item>
						</el-col>
					</transition>
					<transition name="el-zoom-in-top">
						<el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="8" v-show="!searchCol">
							<el-form-item label="动作类型" prop="DelayState">
								<el-select v-model="state.queryParams.DelayState" placeholder="动作类型" class="w100">
									<el-option v-for="item in delayStateOptions" :value="item.key" :key="item.key" :label="item.value" />
								</el-select>
							</el-form-item>
						</el-col>
					</transition>
					<el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="8">
						<el-form-item label=" ">
							<div class="flex-end w100">
								<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-button link type="primary" @click="closeSearch" :loading="state.loading">
									{{ searchCol ? '展开' : '收起' }}
									<SvgIcon :class="{ 'is-reverse': searchCol }" name="ele-ArrowUp" class="mr5 arrow" size="18px" />
								</el-button>
							</div>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
		</el-card>
		<el-card shadow="never">
			<!-- 表格 -->
			<el-table
				:data="state.tableData"
				v-loading="state.loading"
				show-summary
				row-key="id"
				ref="multipleTableRef"
				@selection-change="handleSelectionChange"
				@sort-change="sortChange"
        border
			>
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column prop="orgName" label="坐席人员" show-overflow-tooltip align="center"></el-table-column>
				<el-table-column prop="addNum" label="工号" show-overflow-tooltip align="center" sortable="custom"></el-table-column>
				<el-table-column prop="addNum" label="工作组" show-overflow-tooltip align="center" sortable="custom"></el-table-column>
				<el-table-column prop="addNum" label="工作类型" show-overflow-tooltip align="center" sortable="custom"></el-table-column>
				<el-table-column prop="addNum" label="开始时间" show-overflow-tooltip align="center" sortable="custom">
					<template #default="{ row }">
						<span>{{ formatDate(row.expiredTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="deleteNum" label="结束时间" show-overflow-tooltip align="center" sortable="custom">
					<template #default="{ row }">
						<span>{{ formatDate(row.expiredTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="deleteNum" label="使用时间(秒)" show-overflow-tooltip align="center" sortable="custom"></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>
	</div>
</template>
<script setup lang="ts" name="statisticsCallTel">
import { onMounted, reactive, ref } from 'vue';
import { ElButton, ElMessage, FormInstance } from 'element-plus';
import { auth } from '/@/utils/authFunction';
import { throttle } from '/@/utils/tools';
import { knowledgeList } from '/@/api/statistics/knowledge';
import { shortcuts } from '/@/utils/constants';
import { formatDate } from '/@/utils/formatTime';
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive(<any>{
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 10,
		Keyword: null, // 关键词
		crTime: [], // 时间默认今天开始到今天结束
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
});
const delayStateOptions = ref<EmptyArrayType>([]); // 延期状态
const searchCol = ref(true); // 展开/收起
// 展开/收起
const closeSearch = () => {
	searchCol.value = !searchCol.value;
};
/** 获取列表 */
const queryList = throttle(() => {
	if (!auth('business:publish:query')) ElMessage.error('抱歉，您没有权限查看工单发布列表！');
	else {
		state.loading = true;
		const request = {
			CreationTimeStart: state.queryParams.crTime[0],
			CreationTimeEnd: state.queryParams.crTime[1],
			DelayState: state.queryParams.DelayState,
			PageIndex: state.queryParams.PageIndex,
			PageSize: state.queryParams.PageSize,
			Keyword: state.queryParams.Keyword,
			SortField: state.queryParams.SortField,
			SortRule: state.queryParams.SortRule,
		};
		knowledgeList(request)
			.then((res: any) => {
				state.tableData = res.result?.items ?? [];
				state.total = res.result?.total ?? 0;
				state.loading = false;
			})
			.catch((err: any) => {
				state.loading = false;
			});
	}
}, 300);
// 排序
const sortChange = (val: any) => {
	state.queryParams.SortField = val.prop;
  // 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);
// 表格多选
const multipleTableRef = ref<RefType>();
const multipleSelection = ref<any>([]);
const handleSelectionChange = (val: any[]) => {
	multipleSelection.value = val;
};

onMounted(() => {
	queryList();
});
</script>
<style lang="scss" scoped>
.statistics-call-telephone-container {
	.arrow {
		transition: transform var(--el-transition-duration);
		cursor: pointer;
	}
	.arrow.is-reverse {
		transform: rotateZ(-180deg);
	}
}
</style>
