<template>
	<div class="snapshot-config-sms-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="IndustryName">
							<el-input
								v-model="state.queryParams.IndustryName"
								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="resetQuery(ruleFormRef)" class="default-button"> <SvgIcon name="ele-Refresh" class="mr5" />重置 </el-button>
						</el-form-item>
					</el-form>
				</template>
				<template #toolbar_buttons>
					<el-button type="primary" @click="onAdd" v-auth="'snapshot:config:sms:add'"> <SvgIcon name="ele-Plus" class="mr5" />新增短信 </el-button>
				</template>
				<template #action="{ row }">
					<el-button link type="primary" @click="onEdit(row)" v-auth="'snapshot:config:sms:edit'" title="编辑"> 编辑 </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>
		<!--  新增  -->
		<sms-add ref="smsAddRef" @updateList="queryList" />
		<!--  编辑  -->
		<sms-edit ref="smsEditRef" @updateList="queryList" />
	</div>
</template>

<script lang="tsx" setup name="snapshotConfigSms">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { getSmsList } from '@/api/snapshot/config';

// 引入组件
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
const SmsAdd = defineAsyncComponent(() => import('@/views/snapshot/config/sms/components/Sms-add.vue')); // 新增
const SmsEdit = defineAsyncComponent(() => import('@/views/snapshot/config/sms/components/Sms-edit.vue')); // 编辑

// 定义变量内容
const state = reactive<any>({
	loading: false,
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 20,
		IndustryName: 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: 'snapshotConfigSms',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	columns: [
		{
			field: 'content',
			title: '短信内容',
			minWidth: 200,
		},
		{
			field: 'industryName',
			title: '行业类型',
		},
		{
			field: 'isEnable',
			title: '启用状态',
			slots: {
				default: ({ row }: any) => {
					return row.isEnable ? '启用' : '禁用';
				},
			},
		},
		{
			field: 'isPublic',
			title: '是否公用',
			slots: {
				default: ({ row }: any) => {
					return row.isPublic ? '是' : '否';
				},
			},
		},
		{
			field: 'statusTxt',
			title: '审批类型',
		},
		{
			field: 'displayOrder',
			title: '排序',
		},

		{ title: '操作', width: 90, fixed: 'right', align: 'center', slots: { default: 'action' } },
	],
	data: [],
});
const ruleFormRef = ref<any>(null); // 表单ref
/** 搜索按钮操作 节流操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
// 获取参数列表
const queryList = () => {
	state.loading = true;
	gridOptions.loading = true;
	getSmsList(state.queryParams)
		.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 resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 新增
const smsAddRef = ref<RefType>();
const onAdd = () => {
	smsAddRef.value.openDialog();
};
// 编辑
const smsEditRef = ref<RefType>();
const onEdit = (row: any) => {
	smsEditRef.value.openDialog(row.id);
};
// 页面加载时
onMounted(() => {
	queryList();
});
</script>
