<template>
	<x-dialog :title="titleMap[mode]" v-model="visible" :width="900" destroy-on-close @closed="$emit('closed')">

		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">

			<el-form-item label="角色名称" prop="title">
				<el-input v-model="form.title" clearable></el-input>
			</el-form-item>

			<el-form-item label="别名" prop="title">
				<el-input v-model="form.alias" clearable></el-input>
				<div class="el-form-item-msg">用于权限判断,可以留空; 不可重复</div>
			</el-form-item>

			<el-form-item label="升级点数" prop="anicount" v-if="form.type=='member'">
				<el-input v-model="form.anicount" clearable></el-input>
			</el-form-item>

			<el-form-item label="等级图片" prop="anicount" v-if="form.type=='member'">
				<x-upload v-model="form.image" title="等级图片"></x-upload>
			</el-form-item>


			<el-form-item label="是否有效" prop="status">

				<el-radio-group v-model="form.status">
				    <el-radio label="1" size="large">开启</el-radio>
				    <el-radio label="0" size="large">关闭</el-radio>
				</el-radio-group>

			</el-form-item>

			<el-form-item label="备注" prop="message">
				<el-input v-model="form.message" clearable type="textarea"></el-input>
			</el-form-item>

			<div class="rolepermission" v-if="form.type=='system'">
				<el-tabs tab-position="top">
					<el-tab-pane label="菜单权限">
						<div class="treeMain">
							<el-tree ref="menu" node-key="id" :data="menu.list" :props="menu.props" :default-checked-keys="menu.checked"  show-checkbox></el-tree>
						</div>
					</el-tab-pane>
					<el-tab-pane label="数据权限">
						<el-form label-width="100px" label-position="left">
							<el-form-item label="规则类型">
								<el-select v-model="form.authority.dataType" placeholder="请选择">
									<el-option label="全部可见" value="1"></el-option>
									<el-option label="本人可见" value="2"></el-option>
									<el-option label="所在部门可见" value="3"></el-option>
									<el-option label="所在部门及子级可见" value="4"></el-option>
									<el-option label="选择的部门可见" value="5"></el-option>
									<el-option label="自定义" value="6"></el-option>
								</el-select>
							</el-form-item>
							<el-form-item label="选择部门" v-show="form.dataType=='5'">
								<div class="treeMain" style="width: 100%;">
									<el-tree ref="dept" node-key="id" :data="dept.list" :props="dept.props" :default-checked-keys="dept.checked" show-checkbox></el-tree>
								</div>
							</el-form-item>
							<el-form-item label="规则值" v-show="form.dataType=='6'">
								<el-input v-model="form.authority.dataRule" clearable type="textarea" :rows="6" placeholder="请输入自定义规则代码"></el-input>
							</el-form-item>
						</el-form>
					</el-tab-pane>
					<el-tab-pane label="控制台模块">
						<div class="treeMain">
							<el-tree ref="grid" node-key="key" :data="grid.list" :props="grid.props" :default-checked-keys="grid.checked" show-checkbox></el-tree>
						</div>
					</el-tab-pane>
				</el-tabs>
			</div>
		</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>
	</x-dialog>
</template>

<style scoped>
	.treeMain {
		height: 280px;
	    overflow: auto;
	    border: 1px solid #dcdfe6cc;
	    border-radius: 4px;
	    padding: 4px 0;
	}
	.rolepermission{
		padding-left: 100px;
	}


	 @media (max-width: 992px){
		.rolepermission{
			padding-left: 0px;
		}
	}
</style>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id:"",
					title: "",
					alias: "",
					status: 1,
					message: "",
					authority:{
						menu:[],
						grid:[],
						dataType: 1,
					}
				},
				//验证规则
				rules: {
					title: [
						{required: true, message: '请填写名称'}
					],
					alias: [
						{required: true, message: '请填写别名'}
					],
				},
				menu: {
					list: [],
					checked: [],
					props: {
						label: (data)=>{
							return data.meta.title
						}
					}
				},
				grid: {
					list: [],
					checked: [],
					props: {
						label: (data)=>{
							return data.title
						},
						disabled: (data)=>{
							return data.isFixed
						}
					}
				},
				dept: {
					list: [],
					checked: [],
					props: {
						label: (data)=>{
							return data.title
						},
						disabled: (data)=>{
							return data.isFixed
						}
					}
				},
			}
		},
		mounted() {

		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){

				//选中的和半选的合并后传值接口
				if (this.form.type == 'system') {
					this.form.authority.menu = this.$refs.menu.getCheckedKeys().concat(this.$refs.menu.getHalfCheckedKeys())
					this.form.authority.grid = this.$refs.grid.getCheckedKeys().concat(this.$refs.grid.getHalfCheckedKeys())
				}else{
					this.form.authority = {
						menu:[],
						grid:[],
					};
				}

				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res = await this.$http.post('system/group/submit', this.form);
						this.isSaveing = false;
						if(res.code == 200){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
							return ;
						}
						this.$alert(res.message, "提示", {type: 'error'})
					}
				})
			},
			//表单注入数据
			setData(data){
				// this.form = data
				Object.assign(this.form, data)

				this.menu.checked = this.form.authority.menu || [];
				this.grid.checked = this.form.authority.grid || [];
				this.dept.checked = this.form.authority.dept || [];

				if (this.form.type=='system') {
					this.$http.post('system/group/menuSystem',{}).then((res) => {
						if (res.code==200) {
							// Object.assign(this.$data, res.data);
							this.menu.list = res.data.menu || [];
							this.grid.list = res.data.grid || [];
							this.dept.list = res.data.dept || [];
							return false;
						}
						this.$alert(res.message, "提示", {type: 'error'});
					});

				}

			}
		}
	}
</script>

<style>
</style>
