<template>
	<div class="snapshot-info-ssp-config-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<vxe-grid v-bind="gridOptions" ref="gridRef">
				<template #form>
					<el-form :model="state.queryParams" ref="ruleFormRef" inline @submit.native.prevent :disabled="gridOptions.loading">
						<el-form-item label="审批状态" prop="State">
							<el-radio-group v-model="state.queryParams.State" @change="handleQuery">
								<el-radio v-for="item in bulletinState" :label="item.value" :value="item.key" :key="item.key"></el-radio>
							</el-radio-group>
						</el-form-item>
						<el-form-item label="标题" prop="Title">
							<el-input v-model="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>
				</template>
				<template #toolbar_buttons>
					<el-button type="primary" @click="onAdd" v-auth="'snapshot:info:sspConfig:add'"> <SvgIcon name="ele-Plus" class="mr5" />新增 </el-button>
				</template>
				<template #action="{ row }">
					<el-button link type="primary" @click="onEdit(row)" v-if="[0, 3].includes(row.bulletinState)" v-auth="'snapshot:info:sspConfig:edit'">
						编辑
					</el-button>
					<el-button link type="danger" @click="onDelete(row)" v-if="[0].includes(row.bulletinState)" v-auth="'snapshot:info:sspConfig:delete'">
						删除
					</el-button>
					<el-button
						link
						type="primary"
						@click="onCommit(row)"
						v-if="[0, 3].includes(row.bulletinState)"
						title="提交审批"
						v-auth="'snapshot:info:sspConfig:commit'"
					>
						提交
					</el-button>
					<el-button
						link
						type="primary"
						@click="onShelfFn(row)"
						v-if="[2].includes(row.bulletinState) && !row.isArrive"
						v-auth="'snapshot:info:sspConfig:onShelf'"
					>
						上架
					</el-button>
					<el-button
						link
						type="primary"
						@click="offShelfFn(row)"
						v-if="[2].includes(row.bulletinState) && row.isArrive"
						v-auth="'snapshot:info:sspConfig:offShelf'"
					>
						下架
					</el-button>
					<el-button link type="primary" @click="onDetail(row)" v-if="[1, 2].includes(row.bulletinState)"> 查看详情 </el-button>
				</template>
				<template #pager>
					<pagination
						@pagination="queryList"
						:total="state.total"
						v-model:current-page="state.queryParams.PageIndex"
						v-model:page-size="state.queryParams.PageSize"
						:disabled="state.loading"
					/>
				</template>
			</vxe-grid>
		</div>
		<!--	更多查询	-->
		<el-drawer v-model="drawer" title="更多查询" size="500px">
			<el-form :model="state.queryParams" ref="drawerRuleFormRef" @submit.native.prevent label-width="100px">
				<el-form-item label="类别名称" prop="SnapshotBulletinTypeName">
					<el-input
						v-model="state.queryParams.SnapshotBulletinTypeName"
						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" />
				</el-form-item>
				<el-form-item label="部门名称" prop="DepartmentName">
					<el-input v-model="state.queryParams.DepartmentName" placeholder="请填写部门名称" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="发布人" prop="CreatorName">
					<el-input v-model="state.queryParams.CreatorName" placeholder="请填写发布人" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="发布时间" prop="fbTime">
					<el-date-picker
						v-model="state.queryParams.fbTime"
						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>
			<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>
		<!--  新增  -->
		<config-add ref="configAddRef" @updateList="queryList" />
		<!--  修改  -->
		<config-edit ref="configEditRef" @updateList="queryList" />
		<!--  详情  -->
		<config-detail ref="configDetailRef" @updateList="queryList" />
	</div>
</template>

<script lang="tsx" setup name="snapshotInfoSspConfig">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus';
import {
	delSnapshotBulletin,
	getSnapshotBulletinBaseData,
	getSnapshotBulletinList,
	shelfSnapshotBulletin,
	submitSnapshotBulletin,
} from '@/api/snapshot/info';
import { defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import Other from '@/utils/other';

// 引入组件
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
const ConfigAdd = defineAsyncComponent(() => import('@/views/snapshot/info/sspConfig/components/Config-add.vue')); // 新增
const ConfigEdit = defineAsyncComponent(() => import('@/views/snapshot/info/sspConfig/components/Config-edit.vue')); // 编辑
const ConfigDetail = defineAsyncComponent(() => import('@/views/snapshot/info/sspConfig/components/Config-detail.vue')); // 详情

// 定义变量内容
const state = reactive<any>({
	loading: false,
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 20,
		State: 0, // 审核状态 默认草稿
		SnapshotBulletinTypeName: null, // 类型名称
		Title: null, // 标题
		No: null, // 文档编号
		DepartmentName: null, // 部门名称
		CreatorName: null, // 发布人
		fbTime: [], // 发布时间
		BeginCreationTime: null,
		EndCreationTime: null,
	},
	total: 0, // 总条数
});

const gridOptions = reactive<any>({
	loading: false,
	border: true,
	showOverflow: true,
	columnConfig: {
		resizable: true,
	},
	scrollY: {
		enabled: true,
		gt: 100,
	},
	toolbarConfig: {
		zoom: true,
		custom: true,
		refresh: {
			queryMethod: () => {
				handleQuery();
			},
		},
		slots: {
			buttons: 'toolbar_buttons',
		},
	},
	customConfig: {
		storage: true,
	},
	id: 'snapshotConfigClue',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	columns: [
		{
			field: 'snapshotBulletinTypeName',
			title: '类别名称',
			width: 150,
		},
		{
			field: 'title',
			title: '标题',
			minWidth: 200,
			slots: {
				default: ({ row }) => {
					return (
						<el-button link type="primary" onClick={() => onDetail(row)}>
							{row.title}
						</el-button>
					);
				},
			},
		},
		{
			field: 'no',
			title: '文档编码',
			width: 100,
		},
		{
			field: 'readedNum',
			title: '阅读次数',
			width: 100,
		},
		{
			field: 'departmentName',
			title: '部门名称',
			width: 140,
		},
		{
			field: 'creatorName',
			title: '发布人',
			width: 120,
		},
		{
			field: 'bulletinTime',
			title: '发布时间',
			width: 160,
			formatter: 'formatDate',
		},
		{
			field: 'isOpen',
			title: '是否公开',
			width: 100,
			slots: {
				default: ({ row }) => {
					return row.isOpen ? '是' : '否';
				},
			},
		},
		{
			field: 'isOpenWebsite',
			title: '网站公开',
			width: 100,
			slots: {
				default: ({ row }) => {
					return row.isOpenWebsite ? '是' : '否';
				},
			},
		},
		{
			field: 'isWeChat',
			title: '微信公开',
			width: 100,
			slots: {
				default: ({ row }) => {
					return row.isWeChat ? '是' : '否';
				},
			},
		},
		{
			field: 'isWeibo',
			title: '微博公开',
			width: 100,
			slots: {
				default: ({ row }) => {
					return row.isWeibo ? '是' : '否';
				},
			},
		},
		{
			field: 'bulletinStateText',
			title: '审批状态',
			width: 100,
		},
		{ title: '操作', width: 200, fixed: 'right', showOverflow: false, align: 'center', slots: { default: 'action' } },
	],
	data: [],
});
/** 搜索按钮操作 节流操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
// 获取参数列表
const requestParams = ref<EmptyObjectType>({});
const queryList = () => {
	state.loading = true;
	gridOptions.loading = true;
	requestParams.value = Other.deepClone(state.queryParams);
	requestParams.value.BeginCreationTime = state.queryParams.fbTime === null ? null : state.queryParams.fbTime[0]; // 受理时间
	requestParams.value.EndCreationTime = state.queryParams.fbTime === null ? null : state.queryParams.fbTime[1];
	Reflect.deleteProperty(requestParams.value, 'fbTime'); // 删除无用的参数
	getSnapshotBulletinList(requestParams.value)
		.then((res) => {
			state.loading = false;
			gridOptions.data = res.result.items ?? [];
			state.total = res.result.total ?? 0;
			gridOptions.loading = false;
		})
		.finally(() => {
			state.loading = false;
			gridOptions.loading = false;
		});
};
// 重置表单
const drawerRuleFormRef = ref<RefType>();
const ruleFormRef = ref<any>(null); // 表单ref
const drawer = ref(false);
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	ruleFormRef.value?.resetFields();
	queryList();
};
// 新增
const configAddRef = ref<RefType>();
const onAdd = () => {
	configAddRef.value.openDialog();
};
// 编辑
const configEditRef = ref<RefType>();
const onEdit = (row: any) => {
	configEditRef.value.openDialog(row.id);
};

// 删除
const onDelete = (row: any) => {
	ElMessageBox.confirm(`您确定要删除：【${row.title}】，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
		autofocus: false,
	})
		.then(() => {
			delSnapshotBulletin(row.id).then(() => {
				ElMessage.success('操作成功');
				queryList();
			});
		})
		.catch(() => {});
};
// 提交
const onCommit = (row: any) => {
	ElMessageBox.confirm(`您确定要提交审批：【${row.title}】，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
		autofocus: false,
	})
		.then(() => {
			submitSnapshotBulletin({ id: row.id }).then(() => {
				ElMessage.success('操作成功');
				queryList();
			});
		})
		.catch(() => {});
};
// 上架
const onShelfFn = (row: any) => {
	ElMessageBox.confirm(`您确定要上架：【${row.title}】，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
		autofocus: false,
	})
		.then(() => {
			shelfSnapshotBulletin({ id: row.id, isArrive: true }).then(() => {
				ElMessage.success('操作成功');
				queryList();
			});
		})
		.catch(() => {});
};
// 下架
const offShelfFn = (row: any) => {
	ElMessageBox.confirm(`您确定要下架：【${row.title}】，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
		autofocus: false,
	})
		.then(() => {
			shelfSnapshotBulletin({ id: row.id, isArrive: false }).then(() => {
				ElMessage.success('操作成功');
				queryList();
			});
		})
		.catch(() => {});
};
// 查看详情
const configDetailRef = ref<RefType>();
const onDetail = (row: any) => {
	configDetailRef.value.openDialog(row.id);
};
// 获取基础信息
const bulletinState = ref<EmptyArrayType>([]);
const getBaseInfo = async () => {
	try {
		const { result } = await getSnapshotBulletinBaseData();
		bulletinState.value = result.bulletinState;
	} catch (e) {
		console.log(e);
	}
};
// 页面加载时
onMounted(() => {
	queryList();
	getBaseInfo();
});
</script>
