<template>
	<div class="quality-done-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<el-tabs v-model="state.queryParams.Source" @tab-change="handleQuery">
				<el-tab-pane label="受理已质检" :name="1" :disabled="state.loading"></el-tab-pane>
				<el-tab-pane label="派单已质检" :name="2" :disabled="state.loading"></el-tab-pane>
				<el-tab-pane label="回访已质检" :name="3" :disabled="state.loading"></el-tab-pane>
			</el-tabs>
			<el-form :model="state.queryParams" ref="ruleFormRef" inline @submit.native.prevent>
				<el-form-item label="关键词" prop="Keyword">
					<el-input v-model="state.queryParams.Keyword" placeholder="关键词" clearable @keyup.enter="handleQuery" class="keyword-input"/>
				</el-form-item>
				<el-form-item label="分数区间" prop="value">
					<input-number-range v-model="state.queryParams.value" />
				</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="handleQuery"
						value-format="YYYY-MM-DD[T]HH:mm:ss"
						:default-time="defaultTimeStartEnd"
					/>
				</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)" v-waves class="default-button">
						<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"
					: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="qualityDone"
					:custom-config="{ storage: true }"
				>
					<vxe-column field="order.no" title="工单编码" width="140"></vxe-column>
					<vxe-column field="order.title" title="工单标题" min-width="200">
						<template #default="{ row }">
							<order-detail :order="row.order" @updateList="queryList">{{ row.order?.title }}</order-detail>
						</template>
					</vxe-column>
					<vxe-column field="aiQuality" title="质检方式" width="100">
						<template #default="{ row }">
							{{ row.aiQuality ? '智能质检' : '人工质检' }}
						</template>
					</vxe-column>
					<vxe-column field="order.sourceChannel" title="来源渠道" width="110"></vxe-column>
					<vxe-column field="order.startTime" title="受理时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.order?.startTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<template v-if="state.queryParams.Source === 3">
						<vxe-column field="visit.visitStateText" title="回访状态" width="110"></vxe-column>
						<vxe-column field="visit.visitTypeText" title="回访方式" width="110"></vxe-column>
						<vxe-column field="order.filedTime" title="办结时间" width="160">
							<template #default="{ row }">
								{{ formatDate(row.order?.filedTime, 'YYYY-mm-dd HH:MM:SS') }}
							</template>
						</vxe-column>
						<vxe-column field="visit.publishTime" title="发布时间" width="160">
							<template #default="{ row }">
								{{ formatDate(row.visit?.publishTime, 'YYYY-mm-dd HH:MM:SS') }}
							</template>
						</vxe-column>
						<vxe-column field="visit.creationTime" title="回访任务创建时间" width="160">
							<template #default="{ row }">
								{{ formatDate(row.visit?.creationTime, 'YYYY-mm-dd HH:MM:SS') }}
							</template>
						</vxe-column>
						<vxe-column field="visit.employeeName" title="回访人" width="120"></vxe-column>
						<vxe-column field="visit.visitTime" title="回访时间" width="160">
							<template #default="{ row }">
								{{ formatDate(row.visit?.visitTime, 'YYYY-mm-dd HH:MM:SS') }}
							</template>
						</vxe-column>
						<vxe-column field="order.counterSignTypeText" title="是否会签" width="100"></vxe-column>
					</template>
					<template v-if="state.queryParams.Source === 2">
						<vxe-column field="creationTime" title="交办时间" width="160">
							<template #default="{ row }">
								{{ formatDate(row.creationTime, 'YYYY-mm-dd HH:MM:SS') }}
							</template>
						</vxe-column>
					</template>
					<vxe-column field="order.statusText" title="工单状态" width="100">
						<template #default="{ row }">
							<el-text type="danger" tag="b" v-if="[1, 2, 3, 9, 101, 102, 103, 104, 105, 200].includes(row.order.status)">{{ row.order.statusText }}</el-text>
							<span v-else>{{ row.order.statusText }}</span>
						</template>
					</vxe-column>
					<vxe-column field="order.acceptType" title="受理类型" width="100"></vxe-column>
					<vxe-column field="order.hotspotName" title="热点分类" width="150"></vxe-column>
					<vxe-column field="order.acceptorName" title="受理人" width="120"></vxe-column>
					<vxe-column field="order.fromPhone" title="来电电话" width="140"></vxe-column>
					<vxe-column field="userName" title="质检人" width="120"></vxe-column>
					<vxe-column field="qualityTime" title="质检时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.qualityTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="grade" title="质检得分" width="100"></vxe-column>
					<vxe-column title="操作" fixed="right" width="120" align="center" :show-overflow="false">
						<template #default="{ row }">
							<el-button link type="primary" @click="onQualityView(row)" v-auth="'quality:done:view'" title="查看质检详情"> 查看 </el-button>
							<el-button link type="primary" @click="onQualityEdit(row)" v-auth="'quality:done:edit'" title="编辑质检"> 编辑 </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>
		<!-- 质检 -->
		<quality-inspection ref="qualityInspectionRef" @updateList="queryList" />
	</div>
</template>

<script lang="tsx" setup name="qualityDone">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { qualityBaseData, qualityList } from '@/api/quality';
import { defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import Other from '@/utils/other';

// 引入组件
const QualityInspection = defineAsyncComponent(() => import('@/views/quality/done/components/Quality-inspection.vue')); // 质检
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const InputNumberRange = defineAsyncComponent(() => import('@/components/NumberRange/index.vue')); // 数字区间
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页

// 定义变量内容
const state = reactive<any>({
	loading: false, // 加载状态
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 20,
		Source: 1,
		CreationTimeStart: null, // 创建时间 开始
		CreationTimeEnd: null, // 创建时间 结束
		crTime: [], // 办理期限
		State: 1, // 待质检
		Keyword: null,
		value: [0, 100],
	},
	total: 0, // 总条数
	tableData: [], // 表格数据
});
const ruleFormRef = ref<RefType>(null); // 表单ref
const qualitySourceOptions = ref<EmptyArrayType>([]); // 违禁词分类
const qualityStateOptions = ref<EmptyArrayType>([]); // 违禁词属性
const getBaseData = async () => {
	try {
		const res = await qualityBaseData();
		qualitySourceOptions.value = res.result?.qualitySource ?? [];
		qualityStateOptions.value = res.result?.qualityState ?? [];
	} catch (error) {
		console.log(error);
	}
};
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
// 查询已质检列表
const requestParams = ref<EmptyObjectType>({});
const queryList = () => {
	state.loading = true;
	requestParams.value = Other.deepClone(state.queryParams);
	requestParams.value.CreationTimeStart = state.queryParams.crTime === null ? null : state.queryParams.crTime[0]; // 生成时间
	requestParams.value.CreationTimeEnd = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
	Reflect.deleteProperty(requestParams.value, 'crTime'); // 删除无用的参数
	requestParams.value.MinGrade = state.queryParams.value === null ? null : state.queryParams.value[0]; // 分数区间
	requestParams.value.MaxGrade = state.queryParams.value === null ? null : state.queryParams.value[1];
	Reflect.deleteProperty(requestParams.value, 'value'); // 删除无用的参数
	qualityList(requestParams.value)
		.then((res) => {
			state.loading = false;
			state.tableData = res.result.items ?? [];
			state.total = res.result.total ?? 0;
		})
		.finally(() => {
			state.loading = false;
		});
};
// 重置表单
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	state.queryParams.CreationTimeStart = null;
	state.queryParams.CreationTimeEnd = null;
	queryList();
};
// 质检查看
const qualityInspectionRef = ref<RefType>();
const onQualityView = (row: any) => {
	qualityInspectionRef.value.openDialog(row, state.queryParams.Source, true);
};
// 质检编辑
const onQualityEdit = (row: any) => {
	qualityInspectionRef.value.openDialog(row, state.queryParams.Source);
};
// 页面加载时
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
	getBaseData();
});
</script>