<template>
	<div class="business-observations-Reply-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<el-tabs v-model="state.queryParams.Type" @tab-change="handleQuery">
				<el-tab-pane name="0" label="待回复" :disabled="state.loading"></el-tab-pane>
				<el-tab-pane name="1" label="已回复" :disabled="state.loading"></el-tab-pane>
			</el-tabs>
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline :disabled="state.loading">
				<el-form-item label="数据范围" prop="DataSoure">
					<el-segmented
						:options="[
							{
								value: '0',
								label: '全部',
							},
							{
								value: '1',
								label: '本级',
							},
						]"
						v-model="state.queryParams.DataSoure"
						@change="handleQuery"
						:disabled="state.loading"
					/>
				</el-form-item>
				<el-form-item label="工单编码" prop="No">
					<el-input v-model.trim="state.queryParams.No" placeholder="工单编码" clearable @keyup.enter="handleQuery" class="keyword-input" />
				</el-form-item>
				<el-form-item label="工单标题" prop="Title">
					<el-input v-model.trim="state.queryParams.Title" 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="drawer = true" class="default-button"> <SvgIcon name="ele-Search" class="mr5" />更多查询</el-button>
				</el-form-item>
			</el-form>
			<vxe-toolbar
				ref="toolbarRef"
				:loading="state.loading"
				custom
				:refresh="{
					queryMethod: handleQuery,
				}"
				:tools="[{ toolRender: { name: 'exportCurrent' } }, { toolRender: { name: 'exportAll' } }]"
			>
			</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="businessObservationsReply"
					:custom-config="{ storage: true }"
					showHeaderOverflow
					:params="{ exportMethod: exportObservationsReplyList, exportParams: requestParams }"
					@sort-change="sortChange"
				>
					<vxe-column field="no" title="工单编码" width="140"></vxe-column>
					<vxe-column field="title" title="工单标题" min-width="200">
						<template #default="{ row }">
							<order-detail :order="{ id: row.orderId }" @updateList="queryList">{{ row.title }}</order-detail>
						</template>
					</vxe-column>
					<vxe-column field="acceptType" title="受理类型" width="110"></vxe-column>
					<vxe-column field="hotspotName" title="热点分类" width="150"></vxe-column>
					<vxe-column field="acceptanceTime" title="受理时间" width="160" sortable>
						<template #default="{ row }">
							{{ formatDate(row.acceptanceTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="expiredTime" title="期满时间" width="160" sortable>
						<template #default="{ row }">
							{{ formatDate(row.expiredTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="actualHandleOrgName" title="接办部门" width="140"></vxe-column>
					<vxe-column field="filedTime" title="办结时间" width="160" sortable>
						<template #default="{ row }">
							{{ formatDate(row.filedTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<template v-if="state.queryParams.Type === '1'">
						<vxe-column field="replyUserName" title="回复人" width="140"></vxe-column>
						<vxe-column field="replyTime" title="回复时间" width="160" sortable>
							<template #default="{ row }">
								{{ formatDate(row.replyTime, 'YYYY-mm-dd HH:MM:SS') }}
							</template>
						</vxe-column>
					</template>
					<vxe-column title="操作" fixed="right" width="90" align="center" :show-overflow="false">
						<template #default="{ row }">
							<el-button
								link
								type="primary"
								@click="onReply(row)"
								title="回复"
								v-auth="'business:observations:reply'"
								v-if="state.queryParams.Type === '0'"
							>
								回复
							</el-button>
							<el-button link type="primary" @click="onDetail(row)" title="查看回复" v-else> 查看回复 </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-drawer v-model="drawer" title="更多查询" size="500px">
			<el-form :model="state.queryParams" ref="drawerRuleFormRef" @submit.native.prevent label-width="100px" :disabled="state.loading">
				<el-form-item label="受理类型" prop="AcceptType">
					<el-select v-model="state.queryParams.AcceptType" placeholder="请选择受理类型" clearable @change="handleQuery">
						<el-option v-for="item in acceptTypeOptions" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
					</el-select>
				</el-form-item>
				<el-form-item label="受理时间" prop="slTime">
					<el-date-picker
						v-model="state.queryParams.slTime"
						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 label="期满时间" prop="exTime">
					<el-date-picker
						v-model="state.queryParams.exTime"
						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 label="接办部门" prop="ActualHandleOrgName">
					<el-input v-model="state.queryParams.ActualHandleOrgName" placeholder="请填写接办部门名称" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="热点分类" prop="Hotspot">
					<el-input v-model="state.queryParams.Hotspot" placeholder="热点分类名称" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="办结时间" prop="bjTime">
					<el-date-picker
						v-model="state.queryParams.bjTime"
						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>
				<template v-if="state.queryParams.Type === '1'">
					<el-form-item label="回复人" prop="ReplyUserName">
						<el-input v-model="state.queryParams.ReplyUserName" placeholder="回复人" clearable @keyup.enter="handleQuery" />
					</el-form-item>
					<el-form-item label="回复时间" prop="hfTime">
						<el-date-picker
							v-model="state.queryParams.hfTime"
							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>
				</template>
			</el-form>
			<template #footer>
				<el-button type="primary" @click="handleQuery" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
				<el-button @click="resetQuery(drawerRuleFormRef)" class="default-button"> <SvgIcon name="ele-Refresh" class="mr5" />重置 </el-button>
			</template>
		</el-drawer>
		<!--	回复	-->
		<observe-reply ref="observeReplyRef" @updateList="queryList" />
		<!--	回复详情	-->
		<observe-detail ref="observeDetailRef" @updateList="queryList" />
	</div>
</template>
<script setup lang="tsx" name="businessObservationsReply">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { observeBaseData } from '@/api/query/observe';
import { defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import Other from '@/utils/other';
import { exportObservationsReplyList, getObservationsReplyList } from '@/api/business/observations';
// 引入组件
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
const ObserveReply = defineAsyncComponent(() => import('@/views/business/observations/components/Observe-reply.vue')); // 回复
const ObserveDetail = defineAsyncComponent(() => import('@/views/business/observations/components/Observe-detail.vue')); // 回复详情
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive<any>({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 20,
		Keyword: null, // 关键字
		No: null,
		Title: null,
		slTime: [], // 受理时间
		AcceptanceStartTime: null,
		AcceptanceEndTime: null,
		exTime: [], // 期满时间
		ExpiredStartTime: null,
		ExpiredEndTime: null,
		ActualHandleOrgName: null, // 接办部门
		AcceptType: null, //受理类型
		Hotspot: null, // 热点分类
		DataSoure: '0', // 数据范围 	 0：全部 ，1：本级
		Type: '0', // 0：待回复 1：已回复
		bjTime: [], // 办结时间
		FiledStartTime: null,
		FiledEndTime: null,
		ReplyUserName: null, // 回复人
		hfTime: [], // 回复时间
		ReplyStartTime: null,
		ReplyEndTime: null,
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
});
// 手动查询，将页码设置为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.AcceptanceStartTime = state.queryParams.slTime === null ? null : state.queryParams.slTime[0]; // 受理时间
	requestParams.value.AcceptanceEndTime = state.queryParams.slTime === null ? null : state.queryParams.slTime[1];
	Reflect.deleteProperty(requestParams.value, 'slTime'); // 删除无用的参数
	requestParams.value.ExpiredStartTime = state.queryParams.exTime === null ? null : state.queryParams.exTime[0]; // 期满时间
	requestParams.value.ExpiredEndTime = state.queryParams.exTime === null ? null : state.queryParams.exTime[1];
	Reflect.deleteProperty(requestParams.value, 'exTime'); // 删除无用的参数
	requestParams.value.FiledStartTime = state.queryParams.bjTime === null ? null : state.queryParams.bjTime[0]; // 办结时间
	requestParams.value.FiledEndTime = state.queryParams.bjTime === null ? null : state.queryParams.bjTime[1];
	Reflect.deleteProperty(requestParams.value, 'bjTime'); // 删除无用的参数
	requestParams.value.ReplyStartTime = state.queryParams.hfTime === null ? null : state.queryParams.hfTime[0]; // 回复时间
	requestParams.value.ReplyEndTime = state.queryParams.hfTime === null ? null : state.queryParams.hfTime[1];
	Reflect.deleteProperty(requestParams.value, 'hfTime'); // 删除无用的参数
	getObservationsReplyList(requestParams.value)
		.then((res) => {
			state.tableData = res?.result.items ?? [];
			state.total = res?.result.total;
			state.loading = false;
		})
		.finally(() => {
			state.loading = false;
		});
};
// 排序
const sortChange = (val: any) => {
	state.queryParams.SortField = val.order ? val.field : null;
	// 0 升序 1 降序
	state.queryParams.SortRule = val.order ? (val.order == 'desc' ? 1 : 0) : null;
	queryList();
};
/** 重置按钮操作 */
const drawerRuleFormRef = ref();
const drawer = ref(false);
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	ruleFormRef.value?.resetFields();
	queryList();
};
// 回复
const observeReplyRef = ref<RefType>();
const onReply = (row: any) => {
	observeReplyRef.value.openDialog(row);
};
// 回复详情
const observeDetailRef = ref<RefType>();
const onDetail = (row: any) => {
	observeDetailRef.value.openDialog(row);
};
// 获取基础信息
const acceptTypeOptions = ref<EmptyArrayType>([]); // 受理类型
const getBaseInfo = async () => {
	// 获取基础信息
	try {
		const { result } = await observeBaseData();
		acceptTypeOptions.value = result.acceptTypeOptions;
	} catch (e) {
		console.log(e);
	}
};
const tableRef = ref<RefType>();
const toolbarRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
	getBaseInfo();
});
</script>
