<template>
	<div class="business-special-apply-container layout-padding">
    <div class="layout-padding-auto layout-padding-view pd20">
			<ProTable
				ref="proTableRef"
				:columns="columns"
				:data="state.tableData"
				@updateTable="queryList"
				:loading="state.loading"
				:total="state.total"
				v-model:page-index="state.queryParams.PageIndex"
				v-model:page-size="state.queryParams.PageSize"
			>
        <template #table-search>
          <el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline>
            <el-form-item label="关键词" prop="Keyword">
              <el-input v-model.trim="state.queryParams.Keyword" 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="resetQuery(ruleFormRef)" v-waves class="default-button" :loading="state.loading">
                <SvgIcon name="ele-Refresh" class="mr5" />重置
              </el-button>
            </el-form-item>
          </el-form>
        </template>
				<template #title="{ row }">
					<order-detail :order="row" @updateList="queryList">{{ row.title }}</order-detail>
				</template>
				<!-- 表格操作 -->
				<template #operation="{ row }">
					<el-button link type="primary" @click="onSpecialApply(row)" title="特提申请" v-if="row.workflowId" v-auth="'business:special:apply'">
						特提申请
					</el-button>
				</template>
			</ProTable>
		</div>
		<!-- 特提申请 -->
		<special-apply ref="specialApplyRef" @updateList="queryList" />
	</div>
</template>
<script setup lang="tsx" name="orderSpecial">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { useRouter } from 'vue-router';
import { orderListSpecial } from '@/api/business/special';
// 引入组件
const SpecialApply = defineAsyncComponent(() => import('@/views/business/special/components/Special-apply.vue')); // 忒提申请
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const router = useRouter(); // 路由
const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([
	{ type: 'selection',minWidth: 40,align: 'center' },
	{ prop: 'no', label: '工单编码', minWidth: 140 },
	{ prop: 'isProvinceText', label: '省/市工单', minWidth: 90 },
	{ prop: 'currentStepName', label: '当前节点', minWidth: 120 },
	{ prop: 'statusText', label: '工单状态', minWidth: 100 },
	{ prop: 'title', label: '标题', minWidth: 200 },
	{
		prop: 'startTime',
		label: '受理时间',
    minWidth: 160,
		render: (scope) => {
			return <span>{formatDate(scope.row.startTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{
		prop: 'expiredTime',
		label: '工单期满时间',
    minWidth: 160,
		render: (scope) => {
			return <span>{formatDate(scope.row.expiredTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{
		prop: 'filedTime',
		label: '办结时间',
    minWidth: 160,
		render: (scope) => {
			return <span>{formatDate(scope.row.filedTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'orgLevelOneName', label: '一级部门', minWidth: 140 },
	{ prop: 'actualHandleOrgName', label: '接办部门', minWidth: 140 },
	{ prop: 'acceptType', label: '受理类型', minWidth: 100 },
	{ prop: 'counterSignTypeText', label: '是否会签', minWidth: 90 },
	{ prop: 'hotspotName', label: '热点分类', minWidth: 150 },
	{ prop: 'tagNames', label: '工单标签', minWidth: 150 },
	{ prop: 'acceptorName', label: '受理人', minWidth: 120 },
	{ prop: 'operation', label: '操作', fixed: 'right', minWidth: 90, align: 'center' },
]);
const state = reactive({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 20,
		Keyword: null, // 关键字
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
});
// 手动查询，将页码设置为1
const handleQuery = () => {
  state.queryParams.PageIndex = 1;
  queryList();
};
/** 获取列表 */
const queryList = () => {
	state.loading = true;
	orderListSpecial(state.queryParams)
		.then((res) => {
			state.tableData = res.result?.items ?? [];
			state.total = res.result?.total ?? 0;
			state.loading = false;
		})
		.catch((err) => {
			console.log(err);
			state.loading = false;
		});
}

/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
}
// 导出
const onExport = () => {
	console.log('导出');
};
// 特提申请
const specialApplyRef = ref<RefType>();
const onSpecialApply = (row: any) => {
	if (row.status == 200) {
		// 会签工单无法进行特提
		ElMessage.warning('工单会签中，请先结束会签！');
		return;
	}
	specialApplyRef.value.openDialog(row); // 需要审核
};
onMounted(() => {
	queryList();
});
</script>
