<template>
	<div class="dataShare-editExpire-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<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="handleQuery" class="keyword-input" />
				</el-form-item>
				<el-form-item label="工单编码" prop="No">
					<el-input v-model="state.queryParams.No" placeholder="工单编码" clearable @keyup.enter="handleQuery" class="keyword-input" />
				</el-form-item>
				<el-form-item label="省本地编码" prop="ProvinceNo">
					<el-input v-model="state.queryParams.ProvinceNo" placeholder="省本地编码" clearable @keyup.enter="handleQuery" class="keyword-input" />
				</el-form-item>
				<el-form-item label-width="0">
					<el-button type="primary" @click="handleQuery" :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-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="dataShareEditExpire"
					:custom-config="{ storage: true }"
				>
					<vxe-column field="expiredStatusText" title="超期状态" width="90" align="center">
						<template #default="{ row }">
							<span :class="'overdue-status-' + row.expiredStatus" :title="row.expiredStatusText"></span>
						</template>
					</vxe-column>
					<vxe-column field="no" title="工单编码" width="140"></vxe-column>
					<vxe-column field="provinceNo" title="省本地编码" width="240"></vxe-column>
					<vxe-column field="isProvinceText" title="省/市工单" width="90"></vxe-column>
					<vxe-column field="currentStepName" title="当前节点" width="120"></vxe-column>
					<vxe-column field="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.status)">{{ row.statusText }}</el-text>
							<span v-else>{{ row.statusText }}</span>
						</template>
					</vxe-column>
					<vxe-column field="title" title="工单标题" width="200">
						<template #default="{ row }">
							<order-detail :order="row" @updateList="queryList">{{ row.title }}</order-detail>
						</template>
					</vxe-column>
					<vxe-column field="startTime" title="受理时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.startTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="expiredTime" title="工单期满时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.expiredTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="filedTime" title="办结时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.filedTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="orgLevelOneName" title="一级部门" width="140"></vxe-column>
					<vxe-column field="actualHandleOrgName" title="接办部门" width="140"></vxe-column>
					<vxe-column field="acceptType" title="受理类型" width="110"></vxe-column>
					<vxe-column field="counterSignTypeText" title="是否会签" width="110"></vxe-column>
					<vxe-column field="sourceChannel" title="来源渠道" width="100"></vxe-column>
					<vxe-column field="hotspotSpliceName" title="热点全称" width="150"></vxe-column>
					<vxe-column field="hotspotName" title="热点分类" width="150"></vxe-column>
					<vxe-column field="acceptorName" title="受理人" width="120"></vxe-column>
					<vxe-column field="content" title="受理内容" width="200" :visible="false"></vxe-column>
					<vxe-column field="fileOpinion" title="承办意见" width="200" :visible="false"></vxe-column>
					<vxe-column title="操作" width="190" fixed="right">
						<template #default="{ row }">
							<el-button link type="primary" @click="onEdit(row)" title="查看修改记录"> 修改期满时间 </el-button>
							<el-button link type="primary" @click="onRecord(row)" 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>
		<el-dialog title="修改期满时间" v-model="state.dialogVisible" width="500px" destroy-on-close append-to-body draggable>
			<el-form :model="state.form" ref="formRef" label-width="100px">
				<el-form-item label="期满时间" prop="expiredTime" :rules="[{ required: true, message: '请选择修改后的期满时间', trigger: 'change' }]">
					<el-date-picker
						v-model="state.form.expiredTime"
						type="datetime"
						placeholder="请选择修改后的期满时间"
						value-format="YYYY-MM-DD[T]HH:mm:ss"
						class="w100"
					/>
				</el-form-item>
			</el-form>
			<template #footer>
				<el-button @click="state.dialogVisible = false" class="default-button">取 消</el-button>
				<el-button type="primary" @click="onSubmit(formRef)" :loading="state.loading">确 定</el-button>
			</template>
		</el-dialog>
		<edit-record ref="editRecordRef" />
	</div>
</template>
<script setup lang="tsx" name="dataShareEditExpire">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import type { FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { orderList, orderListCount } from '@/api/business/order';
import { ElMessage } from 'element-plus';
import { editExpireTime } from '@/api/dataShare';

// 引入组件
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const EditRecord = defineAsyncComponent(() => import('@/views/dataShare/RecordDialog.vue')); // 修改记录弹窗
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页

// 定义变量内容
const state = reactive<any>({
	queryParams: {
		PageIndex: 1, // 当前页
		PageSize: 20, // 每页条数
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
	dialogVisible: false,
	form: {},
});
const ruleFormRef = ref<RefType>(); // 表单ref
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
	getTotal();
};
/** 获取列表 */
const queryList = () => {
	state.loading = true;
	orderList(state.queryParams)
		.then((response: any) => {
			state.tableData = response?.result ?? [];
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
// 查询总数
const getTotal = () => {
	orderListCount(state.queryParams)
		.then((res) => {
			state.total = res.result ?? 0;
		})
		.catch(() => {
		});
};
/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
	getTotal();
};
// 修改期满时间
const onEdit = (row: any) => {
	state.form = {
		expiredTime: row.expiredTime,
		id: row.id,
		isPush: true,
	};
	state.dialogVisible = true;
};
const formRef = ref<RefType>();
const onSubmit = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid: boolean) => {
		if (!valid) return;
		state.loading = true;
		editExpireTime(state.form)
			.then(() => {
				state.loading = false;
				state.dialogVisible = false;
				queryList();
				ElMessage.success('操作成功');
			})
			.catch(() => {
				state.loading = false;
			});
	});
};
// 查看修改记录
const editRecordRef = ref<RefType>();
const onRecord = (row: any) => {
	editRecordRef.value.openDialog(row);
};
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	queryList();
	getTotal();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
