<template>
	<div class="system-user-container layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<div class="system-user-search mb15">
				<el-input size="default"  v-model="state.search" placeholder="请输入字典名称" style="max-width: 180px"> </el-input>
				<el-button size="default" type="primary" class="ml10" @click="seletgetTableData()">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button size="default" type="success" class="ml10" @click="onOpenAddDic('add')">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增字典
				</el-button>
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
				<el-table-column type="index" label="序号" width="100" />
				<el-table-column prop="name" label="字典名称" show-overflow-tooltip></el-table-column>
				<el-table-column label="字典编码" show-overflow-tooltip>
					<template #default="scope">
						<div style="color: blue;cursor: pointer;" @click="Gocaidan(scope.row)">{{scope.row.type}}</div>
					</template>
				</el-table-column>
				<el-table-column prop="remark" label="字典描述" show-overflow-tooltip></el-table-column>
				<el-table-column  label="状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.enable">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="createTime" label="创建时间" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" width="100">
					<template #default="scope">
						<el-button size="small" text type="primary" @click="onOpenEditDic('edit', scope.row)">修改</el-button>
						<el-button size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
					@size-change="onHandleSizeChange"
					@current-change="onHandleCurrentChange"
					class="mt15"
					v-model:current-page="state.tableData.param.pageNum"
					background
					v-model:page-size="state.tableData.param.pageSize"
					layout="total, sizes, prev, pager, next, jumper"
					:total="state.tableData.total"
			>
			</el-pagination>
		</el-card>
		<DicDialog ref="dicDialogRef" @refresh="getTableData()"/>
	</div>
</template>

<script setup lang="ts" name="systemDic">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import {ElMessageBox, ElMessage, ElNotification} from 'element-plus';
import { dicApi } from "/@/api/dic/index";
import { useRouter } from "vue-router";

// 引入组件
const DicDialog = defineAsyncComponent(() => import('/@/views/sys/dictionary/dialog.vue'));
// 定义变量内容
const dicDialogRef = ref();
let router = useRouter();
const state = reactive<any>({
	search:"",
	tableData: {
		data: [],
		total: 1000,
		loading: false,
		param: {
			pageNum: 1,
			pageSize:10,
		},
	},
});
const seletgetTableData = () =>{
	state.tableData.param.pageNum = 1
	getTableData()
}
// 初始化表格数据
const getTableData = async () => {
	state.tableData.loading = true;
	const item = await dicApi().searchApi({
		name:state.search,
		page:state.tableData.param.pageNum,
		limit:state.tableData.param.pageSize,
	});
	state.tableData.data = item.data
	state.tableData.total = item.count;
	setTimeout(() => {
		state.tableData.loading = false;
	}, 500);
};
const Gocaidan = (res) =>{
	router.push({
		path:'/system/manage',
		query:{
			type:res.type
		}
	});
}
// 打开新增字典弹窗
const onOpenAddDic = (type: string) => {
	dicDialogRef.value.openDialog(type);
};
// 打开修改字典弹窗
const onOpenEditDic = (type: string, row: RowDicType) => {
	dicDialogRef.value.openDialog(type, row);
};
// 删除字典
const onRowDel = (row: RowDicType) => {
	ElMessageBox.confirm(`此操作将永久删除字典名称：“${row.name}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then( async () => {
			const item =await dicApi().deleteApi({
				ids:row.id
			});//删除接口
			if (item.code != 200){
				ElNotification({
					title: "提示",
					message: item.msg,
					type: "error",
					duration: 3000,
				});
			}else {
				getTableData();
				ElNotification({
					title: "提示",
					message: item.msg,
					type: "success",
					duration: 3000,
				});
			}
		})
		.catch(() => {});
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
	state.tableData.param.pageNum = val;
	getTableData();
};
const onHandleSizeChange = (val: number) => {
	state.tableData.param.pageSize = val;
	getTableData();
};
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>

<style scoped lang="scss">
.system-user-container {
  :deep(.el-card__body) {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: auto;
    .el-table {
      flex: 1;
    }
  }
}
</style>
