<template>
	<lay-container fluid="true" style="padding: 10px">
		<lay-row space="10">
			<lay-col span="24">
				<lay-card>
					<lay-form :model="searchObj" class="search-box">
						<lay-row class="search-items">
							<lay-form-item label="角色名称" prop="FName">
								<lay-input v-model="searchObj.FName"></lay-input>
							</lay-form-item>
							<lay-form-item label="角色描述" prop="FRemark">
								<lay-input v-model="searchObj.FRemark"></lay-input>
							</lay-form-item>
						</lay-row>
						<lay-form-item class="search-btn">
							<lay-button size="sm" type="primary" @click="_clickSearch">搜索</lay-button>
							<lay-button size="sm" @click="_clickReset">重置</lay-button>
						</lay-form-item>
					</lay-form>
					<lay-table :columns="dataColumn" id="id" :dataSource="dataList" v-model:selectedKeys="selectedKeys"
						:checkbox="checkbox" :default-toolbar="defaultToolbar" :page="dataList.length>0?pageInfo:null"
						@change="changePage">
						<template v-slot:toolbar>
							<lay-button size="sm" @click="_getRolePageList(false,true)">刷新</lay-button>
							<lay-button size="sm" type="normal" @click="_changeRole()">新增</lay-button>
						</template>
						<template v-slot:FName="{ data }">
							{{ data.FName }}
						</template>
						<template v-slot:FRemark="{ data }">
							{{ data.FRemark }}
						</template>
						<template v-slot:FAddDate="{ data }">
							{{ data.FAddDate }}
						</template>
						<template v-slot:FEditDate="{ data }">
							{{ data.FEditDate }}
						</template>
						<template v-slot:operator="{ data }">
							<lay-button size="xs" type="primary" @click="_changeRole(data)"> 编辑 </lay-button>
							<lay-button size="xs" type="normal" @click="_setRolePower(data)"> 权限 </lay-button>
							<lay-button size="xs" type="danger" @click="_deleteRole(data.FID)">删除</lay-button>
						</template>
					</lay-table>
				</lay-card>
			</lay-col>
		</lay-row>
		<lay-layer area="500px" v-model="showEditBox" :title="editBoxTitle">
			<EditRole :roleInfoObj="roleInfoObj" @cancelClick="cancelClick"></EditRole>
		</lay-layer>
		<lay-layer area="50%" v-model="showPowerBox" title="设置权限">
			<PowerRole :roleInfoObj="roleInfoObj" @cancelClick="cancelClick"></PowerRole>
		</lay-layer>
	</lay-container>
</template>
<script>
	import {
		ref
	} from "vue";
	import {
		getRolePageList,
		deleteRoleModel
	} from "/src/api/api/user";
	import EditRole from './components/EditRole.vue';
	import PowerRole from './components/PowerRole2.vue';
	import '@layui/layui-vue/es/checkbox/index.css';
	export default {
		components: {
			EditRole,
			PowerRole
		},
		setup() {
			let dataColumn = [{
					title: "角色名称",
					key: "FName",
					width: "20%",
					minWidth: "200px"
				},
				{
					title: "角色描述",
					key: "FRemark",
					ellipsisTooltip: true
				},
				{
					title: "创建时间",
					key: "FAddDate",
					width: "126px"
				},
				{
					title: "最后修改时间",
					key: "FEditDate",
					width: "186px"
				},
				{
					title: "操作",
					key: "operator",
					width: "148px"
				}
			];
			dataColumn.forEach((item) => {
				item.customSlot = item.key;
				item.align = "center";
			});
			const selectedKeys = ref(["1"]);
			const checkbox = ref(false);
			const defaultToolbar = ref(true);
			const showEditBox = ref(false);
			const showPowerBox = ref(false);
			const roleInfoObj = ref({});
			const pageInfo = ref({
				total: 0,
				limit: 10,
				current: 1
			});
			const dataList = ref([]);
			const editBoxTitle = ref("");
			const searchObj = ref({});
			const postData = ref({});
			return {
				roleInfoObj,
				editBoxTitle,
				showEditBox,
				showPowerBox,
				selectedKeys,
				checkbox,
				defaultToolbar,
				dataColumn,
				dataList,
				pageInfo,
				searchObj,
				postData
			};
		},
		mounted() {
			this._getRolePageList(true);
		},
		methods: {
			changePage(obj) {
				this.pageInfo.current = obj.current;
				this.pageInfo.limit = obj.limit;
				this._getRolePageList();
			},
			//获取角色集合
			async _getRolePageList(isFirst, showOk) {
				//this.dataList = [];
				this.pageInfo.total = 0;
				if (isFirst) {
					this.pageInfo.current = 1;
					this.pageInfo.total = 0;
				}
				this.postData.FPageIndex = this.pageInfo.current;
				this.postData.FPageSize = this.pageInfo.limit;
				let result = await getRolePageList(this.postData);
				result.Data.List = result.Data.List || [];
				result.Data.List.forEach((item) => {
					item.FAddDate = item.FAddDate == null ? "" : this.dateFormat(item.FAddDate).split(' ')[0];
					item.FEditDate = item.FEditDate == null ? "" : this.dateFormat(item.FEditDate);
				});
				this.pageInfo.total = result.Data.Total || 0;
				this.dataList = result.Data.List;
			},
			//日期处理
			dateFormat(dataStr) {
				return dataStr.replace('T', ' ').split('.')[0];
			},
			//删除角色弹窗
			_deleteRole(roleId) {
				let $this = this;
				layer.confirm("你确定要删除此角色吗？", {
					btn: [{
							text: '确定',
							callback: function(id) {
								$this.__deleteRole(id, roleId);
							}
						},
						{
							text: '取消',
							callback: function(id) {
								layer.close(id);
							}
						}
					]
				});
			},
			//实际删除角色
			async __deleteRole(layId, roleId) {
				let result = await deleteRoleModel(roleId);
				layer.close(layId);
				if (result && result > 0) {
					layer.msg("操作成功", {
						time: 1000,
						icon: 1
					})
				} else {
					layer.msg("操作失败", {
						time: 1000,
						icon: 2
					})
				}
				this._getRolePageList(true);
			},
			//操作角色信息
			_changeRole(roleObj) {
				this.roleInfoObj = roleObj || {};
				if (roleObj) this.editBoxTitle = "修改角色";
				else this.editBoxTitle = "添加角色";
				this.showEditBox = true;
			},
			cancelClick(isRefresh) {
				if (isRefresh && this.showEditBox) {
					this._getRolePageList(true);
				}
				this.showEditBox = this.showPowerBox = false;
			},
			//显示权限设置
			_setRolePower(roleObj) {
				this.roleInfoObj = roleObj || {};
				this.showPowerBox = true;
			},
			//检索-检索
			_clickSearch() {
				this.postData = {};
				for (let key in this.searchObj) {
					let val = this.searchObj[key];
					if (val != null && val != "") this.postData[key] = val;
				}
				this._getRolePageList(true);
			},
			//检索-重置
			_clickReset() {
				this.searchObj = {};
				this._clickSearch();
			}
		}
	}
</script>
