<template>
	<pi-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode==='show'" ref="formRef" label-width="100px"
				 label-position="right">
			<el-form-item label="名称" prop="role_name">
				<el-input v-model="form.role_name" clearable></el-input>
			</el-form-item>
			<el-form-item label="权限" prop="alias">
				<el-tree style="width: 100%;" ref="menuRef" node-key="menu_id" :data="menu.list"
						 :default-checked-keys="menu.checked"
						 :props="menu.props" show-checkbox></el-tree>
			</el-form-item>
			<el-form-item label="排序" prop="rank">
				<el-input-number v-model="form.rank" controls-position="right" :min="1"
								 style="width: 100%;"></el-input-number>
			</el-form-item>
			<el-form-item label="是否有效" prop="status">
				<el-switch v-model="form.status" :active-value="1" :inactive-value="0"></el-switch>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!=='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</pi-dialog>
</template>

<script setup>
import {ref, onMounted, getCurrentInstance} from "vue"
import tools from "@/utils/tools"
import api from "@/api/index"

defineExpose({
	open,
	setData
})
const emit = defineEmits(['success', 'closed'])

const formRef = ref(null)
const menuRef = ref(null)
const {proxy} = getCurrentInstance()

let mode = ref('add')
let titleMap = ref({
	add: '新增',
	edit: '编辑',
	show: '查看'
})
let visible = ref(false)
let isSaveing = ref(false)
let form = ref({
	role_id: "",
	role_name: "",
	menus: [],
	checked_menus: "",
	rank: 1,
	status: 1
})
let menu = ref({
	list: [],
	props: {
		label: (data) => {
			return data.title
		}
	},
	checked: []
})
let rules = ref({
	rank: [
		{required: true, message: '请输入排序', trigger: 'change'}
	],
	role_name: [
		{required: true, message: '请输入角色名称'}
	]
})

onMounted(() => {
	getMenu()
})

async function getMenu() {
	const res = await api.system.menu.list();
	menu.value.list = tools.makeTreeData(res.data, 0, "menu_id", "parent_id");
}

//显示
function open(m = 'add') {
	mode.value = m
	visible.value = true
}

//表单提交方法
async function submit() {
	let checked = menuRef.value.getCheckedNodes().map(item => item.menu_id);
	let allChecked = checked.concat(menuRef.value.getHalfCheckedNodes().map(item => item.menu_id))
	if (allChecked.length <= 0) return proxy.$message.error("请至少权限")
	form.value.menus = allChecked
	form.value.checked_menus = checked.toString()
	// 校验登录
	const validate = await formRef.value.validate().catch(() => {
	});
	if (!validate) {
		return false
	}
	isSaveing.value = true;
	const res = form.value.role_id ? await api.system.role.edit(form.value) : await api.system.role.add(form.value);
	isSaveing.value = false;
	emit('success', form.value, mode.value)
	visible.value = false;
	proxy.$message.success(res.msg)
}

//表单注入数据
function setData(data) {
	Object.assign(form.value, data)
	if (data.checked_menus) {
		menu.value.checked = data.checked_menus.split(",")
	}
}

</script>

<style>
</style>
