<template>
	<div class="province-zmhd-container layout-pd">
		<el-card shadow="never">
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent label-width="100px">
				<el-row>
          <el-col>
            <el-form-item label="快捷查询" prop="fastSearch">
              <el-radio-group v-model="fastSearch" @change="fastSearchChange">
                <el-radio-button label="all">全部</el-radio-button>
                <el-radio-button label="false">未处理</el-radio-button>
                <el-radio-button label="true">已公开</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6">
						<el-form-item label="工单标题" prop="Keyword">
							<el-input v-model="state.queryParams.Keyword" placeholder="工单标题" clearable @keyup.enter="handleQuery" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6">
						<el-form-item label="工单编码" prop="No">
							<el-input v-model="state.queryParams.No" placeholder="工单编码" clearable @keyup.enter="handleQuery" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6">
						<el-form-item label="省工单编号" prop="ProvinceNo">
							<el-input v-model="state.queryParams.ProvinceNo" placeholder="省工单编号" clearable @keyup.enter="handleQuery" />
						</el-form-item>
					</el-col>
					<transition name="el-zoom-in-top" v-show="!searchCol">
						<el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6">
							<el-form-item label="答复部门名称" prop="AnswerOu">
								<el-input v-model="state.queryParams.AnswerOu" placeholder="来电人姓名" clearable @keyup.enter="handleQuery" />
							</el-form-item>
						</el-col>
					</transition>
					<transition name="el-zoom-in-top" v-show="!searchCol">
						<el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6">
							<el-form-item label="初审人姓名" prop="AuditFirstName">
								<el-input v-model="state.queryParams.AuditFirstName" placeholder="初审人姓名" clearable @keyup.enter="handleQuery" />
							</el-form-item>
						</el-col>
					</transition>
					<transition name="el-zoom-in-top" v-show="!searchCol">
						<el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6">
							<el-form-item label="答复部门名称" prop="AnswerOu">
								<el-input v-model="state.queryParams.AnswerOu" placeholder="来电人姓名" clearable @keyup.enter="handleQuery" />
							</el-form-item>
						</el-col>
					</transition>
					<transition name="el-zoom-in-top">
						<el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6" v-show="!searchCol">
							<el-form-item label="审核时间" prop="auditTime">
								<el-date-picker
									v-model="state.queryParams.auditTime"
									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-col>
					</transition>
					<el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6">
						<el-form-item label=" ">
							<div class="flex-end w100">
								<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-button link type="primary" @click="closeSearch" :loading="state.loading">
									{{ searchCol ? '展开' : '收起' }}
									<SvgIcon :class="{ 'is-reverse': searchCol }" name="ele-ArrowUp" class="mr5 arrow" size="18px" />
								</el-button>
							</div>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
		</el-card>
		<el-card shadow="never">
			<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 #expiredStatusText="{ row }">
					<span :class="'overdue-status-' + row.expiredStatus" :title="row.expiredStatusText"></span>
				</template>
				<template #title="{ row }">
					<order-detail :order="row" @updateList="queryList">{{ row.title }}</order-detail>
				</template>
				<!-- 表格操作 -->
				<template #operation="{ row }">
					<el-button link type="primary" v-if="row.isProvinceZmhd" @click="onDetail(row)">明细</el-button>
					<el-button link type="primary" v-else @click="onPublic(row)">公开</el-button>
					<order-detail :order="row" @updateList="queryList" />
				</template>
			</ProTable>
		</el-card>
		<!--  公开  -->
		<public-handle ref="publicHandleRef" @updateList="queryList" />
		<public-detail ref="publicDetailRef" />
	</div>
</template>
<script setup lang="tsx" name="provinceZmhd">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import type { FormInstance } from 'element-plus';
import { defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import { useRoute, useRouter } from 'vue-router';
import { formatDate } from '@/utils/formatTime';
import Other from '@/utils/other';
import { provinceZmhdList } from '@/api/province';

// 引入组件
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const PublicHandle = defineAsyncComponent(() => import('@/views/province/zmhd/components/Public-handle.vue')); // 公开
const PublicDetail = defineAsyncComponent(() => import('@/views/province/zmhd/components/Public-detail.vue')); // 公开详情

// 定义变量内容
const state = reactive<any>({
	queryParams: {
		PageIndex: 1, // 当前页
		PageSize: 10, // 每页条数
		// 查询条件
		No: null, // 工单编码
		ProvinceNo: null, // 省工单编号
		Title: null, // 标题
		AnswerOu: null, // 回答部门名称
		AuditFirstName: null, // 初审人姓名
		auditTime: [], // 审核时间
		StartTime: null, // 审核开始时间
		EndTime: null, // 审核结束时间
		IsProvinceZmhd: null, // 是否政民互动公开
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
});
const fastSearch = ref('all'); // tab位置
const fastSearchChange = (val: string) => {
	fastSearch.value = val;
	switch (val) {
		case 'all':
			state.queryParams.IsProvinceZmhd = null;
			break;
		case 'false':
			state.queryParams.IsProvinceZmhd = false;
			break;
		case 'true':
			state.queryParams.IsProvinceZmhd = true;
			break;
	}
	handleQuery();
};
const ruleFormRef = ref<RefType>(); // 表单ref
const searchCol = ref(true); // 展开/收起
// 展开/收起
const closeSearch = () => {
	searchCol.value = !searchCol.value;
};
const route = useRoute(); // 路由
const router = useRouter(); // 路由
const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([
	{ prop: 'expiredStatusText', label: '超期状态', align: 'center', width: 80 },
	{ prop: 'statusText', label: '工单状态', width: 100 },
	{
		prop: 'isProvinceZmhd',
		label: '公开',
		render: (scope) => {
			return <span>{scope.row.isProvinceZmhd ? '已公开' : '未处理'}</span>;
		},
	},
	{ prop: 'actualHandleStepName', label: '办理节点', width: 100 },
	{
		prop: 'isUrgentText',
		label: '是否紧急',
		render: (scope) => {
			return <span class="color-danger font-bold">{scope.row.isUrgentText}</span>;
		},
	},
	{ prop: 'provinceNo', label: '省工单编码', minWidth: 230 },
	{ prop: 'no', label: '工单编码', width: 140 },
	{
		prop: 'startTime',
		label: '受理时间',
		width: 160,
		render: (scope) => {
			return <span>{formatDate(scope.row.startTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'title', label: '工单标题', width: 200 },
	{
		prop: 'expiredTime',
		label: '工单期满时间',
		width: 160,
		render: (scope) => {
			return <span>{formatDate(scope.row.expiredTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'actualHandleOrgName', label: '接办部门', width: 140 },
	{ prop: 'acceptType', label: '受理类型', width: 100 },
	{ prop: 'hotspotName', label: '热点分类', width: 150 },
	{ prop: 'acceptorName', label: '受理人', width: 120 },
	{ prop: 'operation', label: '操作', fixed: 'right', width: 140, align: 'center' },
]);
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const requestParams = ref({});
const queryList = () => {
	requestParams.value = Other.deepClone(state.queryParams);
	requestParams.value.StartTime = state.queryParams.auditTime === null ? null : state.queryParams.auditTime[0]; // 审核时间
	requestParams.value.EndTime = state.queryParams.auditTime === null ? null : state.queryParams.auditTime[1];
	Reflect.deleteProperty(requestParams.value, 'auditTime'); // 删除无用的参数
	state.loading = true;
	provinceZmhdList(requestParams.value)
		.then((response: any) => {
			state.tableData = response?.result.items ?? [];

			state.total = response?.result.total;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
  state.queryParams.IsProvinceZmhd = null;
	fastSearch.value = 'all';
	queryList();
};
// 查看明细
const publicDetailRef= ref<RefType>();
const onDetail = (row: any) => {
  publicDetailRef.value.openDrawer(row);
};
// 公开
const publicHandleRef = ref<RefType>();
const onPublic = (row: any) => {
	publicHandleRef.value.openDrawer(row);
};
onMounted(() => {
	queryList();
});
</script>
<style scoped lang="scss">
.province-zmhd-container {
	.arrow {
		transition: transform var(--el-transition-duration);
		cursor: pointer;
	}
	.arrow.is-reverse {
		transform: rotateZ(-180deg);
	}
}
</style>
