<template>
	<div class="business-countersign-container layout-pd">
		<!-- 搜索  -->
		<el-card shadow="never">
			<div class="flex-center-align mb20">
				<span style="color: var(--el-text-color-regular); display: inline-block; text-align: right; padding-right: 12px">快捷查询</span>
				<el-radio-group v-model="fastSearch" @change="fastSearchChange">
					<el-radio-button label="InitiatedCountersignature">发起的会签</el-radio-button>
					<el-radio-button label="HandleCountersignature">已办会签</el-radio-button>
				</el-radio-group>
			</div>
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent label-width="100px">
				<el-row :gutter="10">
					<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="AcceptTypes">
							<el-select
								v-model="state.queryParams.AcceptTypes"
								placeholder="请选择受理类型"
								multiple
								clearable
								class="w100"
								collapse-tags
								collapse-tags-tooltip
								:max-collapse-tags="2"
							>
								<el-option v-for="item in state.acceptTypeOptions" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
							</el-select>
						</el-form-item>
					</el-col>
					<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="Channels">
								<el-select
									v-model="state.queryParams.Channels"
									placeholder="请选择来源渠道"
									multiple
									clearable
									class="w100"
									collapse-tags
									collapse-tags-tooltip
									:max-collapse-tags="2"
								>
									<el-option v-for="item in state.channelOptions" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
								</el-select>
							</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="CounterSignType">
								<el-select v-model="state.queryParams.CounterSignType" placeholder="会签类型" class="w100" @change="handleQuery" clearable>
									<el-option v-for="item in state.counterSignTypeOptions" :value="item.key" :key="item.key" :label="item.value" />
								</el-select>
							</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="ActualHandleStepName">
								<el-input v-model="state.queryParams.ActualHandleStepName" 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="HotspotIds">
								<hot-spot-select
									v-model="state.queryParams.HotspotIds"
									class="w100"
									:hotspotExternal="state.hotspotExternal"
									show-checkbox
									ref="hotSpotRef"
									@confirm="handleQuery"
								/>
							</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 #expiredStatus="{ row }">
					<span :class="'overdue-status-' + row.order?.expiredStatus" :title="row.order?.expiredStatusText"></span>
				</template>
				<template #isProvince="{ row }">
					<span>{{ row.order?.isProvince ? '省工单' : '市工单' }}</span>
				</template>
				<template #title="{ row }">
					<order-detail :order="row.order" @updateList="queryList">{{ row.order?.title }}</order-detail>
				</template>
				<!-- 表格操作 -->
				<template #operation="{ row }">
					<div class="flex-center-center">
						<order-detail :order="row.order" @updateList="queryList" />
						<el-button type="primary" link @click="onRecord(row)" class="ml10">流程明细</el-button>
					</div>
				</template>
			</ProTable>
		</el-card>

		<!-- 流转记录 -->
		<audit-record ref="auditRecordRef"></audit-record>
	</div>
</template>
<script setup lang="tsx" name="businessCountersign">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { ElButton, FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { useRouter } from 'vue-router';
import { countersignBase, countersignData } from "@/api/query/countersign";
import { storeToRefs } from 'pinia';
import { useUserInfo } from '@/stores/userInfo';
// 引入组件
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const AuditRecord = defineAsyncComponent(() => import('@/components/AuditRecord/index.vue')); // 流程明细
const HotSpotSelect = defineAsyncComponent(() => import('@/components/Hotspot/index.vue')); // 选择热点

const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([
	{ prop: 'order.expiredStatus', label: '超期状态', align: 'center',fixed: 'left',width: 80 },
	{ prop: 'stateText', label: '会签状态', width: 120 },
	{ prop: 'order.counterSignTypeText', label: '会签类型', width: 120 },
	{ prop: 'order.no', label: '工单编码', width: 150 },
	{ prop: 'order.sourceChannel', label: '来源方式', width: 100 },
	{ prop: 'order.actualHandleStepName', label: '办理节点', width: 120 },
	{
		prop: 'order.startTime',
		label: '受理时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.order?.startTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'order.title', label: '工单标题', width: 300 },
	{ prop: 'order.fromPhone', label: '来电号码', width: 150 },
	{ prop: 'order.actualHandleOrgName', label: '接办部门', width: 150 },
	{
		prop: 'order.actualHandleTime',
		label: '接办时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.order?.actualHandleTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'order.hotspotName', label: '热点分类', width: 150 },
	{ prop: 'starterOrgName', label: '会签发起部门', width: 150 },
	{ prop: 'starterName', label: '会签发起人', width: 120 },
	{
		prop: 'creationTime',
		label: '会签发起时间',
		width: 170,
		render: (scope) => {
			return <span>{formatDate(scope.row.creationTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{
		prop: 'order.expiredTime',
		label: '工单期满时间',
		width: 170,
		render: (scope: any) => {
			return <span>{formatDate(scope.row.order?.expiredTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'order.acceptType', label: '受理类型', width: 150 },
	{ prop: 'order.hotspotName', label: '热点分类', width: 150 },
	{ prop: 'operation', label: '操作', fixed: 'right', width: 180, align: 'center' },
]);
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const router = useRouter(); // 路由
const state = reactive<any>({
	queryParams: {
		// 查询条件
		PageIndex: 1,
		PageSize: 10,
		Keyword: null, // 关键字
		IsOnlyStarter: false, // 是否中心
		HandleCountersignature: null, // 已办会签
		InitiatedCountersignature: true, // 是否发起会签
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
  acceptTypeOptions:[],// 受理类型
  channelOptions:[],// 来源方式
  counterSignTypeOptions:[],// 会签类型
});
const fastSearch = ref('InitiatedCountersignature');
const fastSearchChange = (val: any) => {
	fastSearch.value = val;
	state.queryParams.HandleCountersignature = null;
	state.queryParams.InitiatedCountersignature = null;
	switch (val) {
		case 'InitiatedCountersignature':
			state.queryParams.InitiatedCountersignature = true;
			break;
		case 'HandleCountersignature':
			state.queryParams.HandleCountersignature = true;
			break;
	}
	handleQuery();
};
const storesUserInfo = useUserInfo();
const { userInfos } = storeToRefs(storesUserInfo); // 用户信息
const searchCol = ref(true); // 展开/收起
// 展开/收起
const closeSearch = () => {
	searchCol.value = !searchCol.value;
};
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const queryList = () => {
	state.loading = true;
	state.queryParams.IsOnlyStarter = userInfos.value.isCenter;
	countersignData(state.queryParams)
		.then((res) => {
			state.tableData = res.result?.items ?? [];
			state.total = res.result?.total ?? 0;
		})
		.catch(() => {})
		.finally(() => {
			state.loading = false;
		});
};
/** 重置按钮操作 */
const hotSpotRef = ref<RefType>();
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	fastSearch.value = 'InitiatedCountersignature';
	state.queryParams.HandleCountersignature = null;
	state.queryParams.InitiatedCountersignature = true;
	hotSpotRef.value?.reset();
	queryList();
};
// 流转记录
const auditRecordRef = ref<RefType>(); // 流转记录
const onRecord = (row) => {
	const params = {
		dialogTitle: '流转记录',
		...row.order,
	};
	auditRecordRef.value.openDialog(params);
};
// 查询基础信息
const baseInfo = async () => {
  try {
    const {result} = await countersignBase();
    state.acceptTypeOptions = result.acceptTypeOptions;
    state.channelOptions = result.channelOptions;
    state.counterSignTypeOptions = result.counterSignType;
  }catch (e){
    console.log(e)
  }
}
onMounted(() => {
  baseInfo();
	queryList();
});
</script>
<style scoped lang="scss">
.business-countersign-container {
	.arrow {
		transition: transform var(--el-transition-duration);
		cursor: pointer;
	}
	.arrow.is-reverse {
		transform: rotateZ(-180deg);
	}
}
</style>
