<template>
	<div class="statistics-order-special-table-container layout-pd">
		<!-- 搜索  -->
		<el-card shadow="never">
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline>
				<el-form-item label="关键字" prop="Keyword">
					<el-input v-model="state.queryParams.Keyword" placeholder="工单编码/标题" clearable @keyup.enter="queryList" class="keyword-input" />
				</el-form-item>
				<el-form-item label="特提状态" prop="State">
					<el-select v-model="state.queryParams.State" placeholder="请选择特提状态" clearable @change="queryList">
						<el-option label="待审核" value="0"></el-option>
						<el-option label="审核通过" value="1"></el-option>
						<el-option label="审核不通过" value="2"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="queryList" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
					<el-button @click="resetQuery(ruleFormRef)" v-waves 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">
			<!-- 表格 -->
			<el-table :data="state.tableData" v-loading="state.loading">
				<el-table-column type="selection" width="55" />
				<el-table-column prop="order.no" label="工单编码" show-overflow-tooltip width="150"></el-table-column>
				<el-table-column width="100" label="省/市工单" prop="isProvince">
					<template #default="{ row }">
						<span>{{ row.order?.isProvince ? '省工单' : '市工单' }}</span>
					</template>
				</el-table-column>
				<el-table-column label="工单标题" show-overflow-tooltip width="300">
					<template #default="{ row }">
						<span class="color-primary">{{ row.order?.title }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="order.actualHandleStepName" label="办理节点" show-overflow-tooltip width="120"></el-table-column>
				<el-table-column prop="stateText" label="特提审批状态" show-overflow-tooltip width="120"></el-table-column>
				<el-table-column label="受理时间" show-overflow-tooltip width="170">
					<template #default="{ row }">
						<span>{{ formatDate(row.order?.startTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="order.acceptType" label="受理类型" show-overflow-tooltip width="120"></el-table-column>
				<el-table-column prop="order.sourceChannel" label="来源方式" show-overflow-tooltip></el-table-column>
				<el-table-column prop="order.hotspotName" label="热点分类" show-overflow-tooltip width="200"></el-table-column>
				<el-table-column label="操作" width="170" fixed="right" align="center">
					<template #default="{ row }">
						<el-button link type="primary" @click="visitDetail(row)" title="查看特提详情"> 特提详情 </el-button>
						<order-detail :order="row.order" @updateList="queryList" />
					</template>
				</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>
		<!-- 审批详情 -->
		<special-audit-detail ref="specialAuditDetailRef" />
	</div>
</template>

<script setup lang="ts" name="statisticsOrderSpecialTable">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { ElButton, ElMessage, FormInstance } from 'element-plus';
import { throttle } from '@/utils/tools';
import { formatDate } from '@/utils/formatTime';
import { useRoute, useRouter } from 'vue-router';
import { departmentSpecialDetail } from '@/api/statistics/order';

// 引入组件
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const SpecialAuditDetail = defineAsyncComponent(() => import('@/views/business/special/components/Special-audit-detail.vue')); // 审批详情

// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const router = useRouter(); // 路由
const state = reactive(<any>{
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 10,
		State: null, // 特提状态
		Keyword: null, // 关键字
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
});
/** 获取列表 */
const route = useRoute();
const queryList = throttle(() => {
	state.loading = true;
	const request = {
		PageIndex: state.queryParams.PageIndex,
		PageSize: state.queryParams.PageSize,
		Keyword: state.queryParams.Keyword,
		Cause: route.params.cause,
		State: state.queryParams.State,
		StartTime: route.params.startTime,
		EndTime: route.params.endTime,
	};
	departmentSpecialDetail(request)
		.then((res: any) => {
			state.tableData = res.result?.items ?? [];
			state.total = res.result?.total ?? 0;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
}, 300);

/** 重置按钮操作 */
const resetQuery = throttle((formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
}, 300);
// 特提审批详情
const specialAuditDetailRef = ref<RefType>();
const visitDetail = (row: any) => {
	specialAuditDetailRef.value.openDialog(row);
};
onMounted(() => {
	if (route.params.cause && route.params.startTime && route.params.endTime) queryList();
	else {
		ElMessage.warning('参数错误');
	}
});
</script>
