<!-- 角色添加修改 -->
<template>
	<div class='roleDialog'>
		<el-dialog title="" :show-close="false" :append-to-body="true" :close-on-click-modal="false" :close-on-press-escape="false" :lock-scroll="true" :visible.sync="dialogRoleVisible">
			<div slot="title" class="header-title">
				<span class="dialogHeader">{{dialogName}}</span>
				<img @click="closeDialog('roleForm')" class="dialogHeaderClose" src="../assets/images/modal-gb.png" />
			</div>
			<div>
				<el-form :hide-required-asterisk="true" :model="roleForm" :rules="rules" ref="roleForm" label-width="100px" class="demo-ruleForm">
					<el-form-item label="角色名称" prop="name">
						<el-input v-model="roleForm.name" placeholder="请输入角色名称"></el-input>
					</el-form-item>
					<el-form-item label="角色职位" prop="isLeader">
						<el-radio-group v-model="roleForm.isLeader">
							<el-radio label="0">部门领导</el-radio>
							<el-radio label="1">普通员工</el-radio>
						</el-radio-group>
					</el-form-item>
					<el-form-item label="角色权限" prop="authorities">
						<el-checkbox-group v-model="roleForm.authorities">
							<el-checkbox v-for="(item,index) in authorityList" :label="item.id" :key="index">{{item.name}}</el-checkbox>
						</el-checkbox-group>
					</el-form-item>
					<el-form-item label="角色描述" prop="description">
						<el-input type="textarea" v-model="roleForm.description"></el-input>
					</el-form-item>
				</el-form>
			</div>
			<div style="text-align: center;margin-top: 40px;">
				<el-button type="primary" @click="roleSubmit('roleForm')">确定</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
	//例如：import 《组件名称》 from '《组件路径》';

	export default {
		name: "roleDialog",
//import引入的组件需要注入到对象中才能使用
		components: {},
		props: {
			showRoleDialog: { type: Boolean, default: false },
			roleDetails: {type: Object},
			authorityLists: {type: Array},
		},
		data () {
//这里存放数据
			return {
				dialogRoleVisible: false,
				roleForm: {
					name: '',
					isLeader: '',
					description: '',
					authorities: [],
					departmentId: '',
				},
				rules:{
					name: [
						{ required: true, message: '请输入角色名称', trigger: 'blur' },
					],
					authorities: [
						{ type: 'array', required: true, message: '请至少选择一个角色权限', trigger: 'change' }
					],
					isLeader: [
						{ required: true, message: '请选择角色职位', trigger: 'change' }
					],
					description: [
						{ required: true, message: '请填写角色描述', trigger: 'blur' }
					]
				},
				dialogName: '',
				authorityList: []
			};
		},
//监听属性 类似于data概念
		computed: {},
//监控data中的数据变化
		watch: {
			showRoleDialog:{
				immediate: true,
				handler(showRoleDialog) {
					// console.log(showViewTrain)
					this.dialogRoleVisible = showRoleDialog;
					// if (showRoleDialog === true) {
					// this.getAuthorityList();
					// }
				}
			},
			authorityLists:{
				immediate: true,
				handler(authorityLists) {
					console.log(authorityLists)
					this.authorityList = authorityLists;
				}
			},
			roleDetails: {
				immediate: true,
				handler(roleDetails) {
					console.log(roleDetails)
					this.dialogName = roleDetails.name === '' ? '新增角色' : '修改角色';
					if (roleDetails.authorities === null) {
						roleDetails.authorities = [];
						this.roleForm = roleDetails;
					} else {
						let arr = [];
						roleDetails.authorities.forEach((v, i) => {
							console.log(v, i)
							arr.push(v.id);
						})
						console.log(arr)
						let data = JSON.parse(JSON.stringify(roleDetails));
						data.authorities = arr;
						this.roleForm = data;
					}
				}
			}
		},
//方法集合
		methods: {
			// getAuthorityList() {
			// 	this.$axios.putAuthority().then((res) => {
			// 		console.log(res)
			// 		if (res.request.status === 200) {
			// 			this.authorityList = res.data;
			// 		}
			// 	})
			// },
			closeDialog(val) {
				console.log(val)
				this.roleForm.name = '';
				this.roleForm.isLeader = '';
				this.roleForm.description = '';
				this.roleForm.authorities = [];
				this.$refs[val].resetFields();
				this.$emit('close', false);
			},
			roleSubmit(val) {
				console.log(this.roleForm)
				this.$refs[val].validate((valid) => {
					if (valid) {
						console.log(this.authorityLists)
						let arr = []
						for (let i = 0; i < this.authorityLists.length; i++) {
							for (let j = 0; j < this.roleForm.authorities.length; j++) {
								if (this.authorityLists[i].id === this.roleForm.authorities[j]) {
									arr.push(this.authorityLists[i]);
								}
							}
						}
						console.log(arr)
						let data = JSON.parse(JSON.stringify(this.roleForm));
						data.authorities = arr;
						this.$axios.postRole(data).then((res) => {
							console.log(res)
							if (res.request.status === 200) {
								this.$message({
									message: `${this.dialogName}角色成功!`,
									type: 'success'
								});
								this.closeDialog('roleForm');
							}
						})
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			}
		},
//生命周期 - 创建完成（可以访问当前this实例）
		created () {

		},
//生命周期 - 挂载完成（可以访问DOM元素）
		mounted () {

		},
		beforeCreate () {
		}, //生命周期 - 创建之前
		beforeMount () {
		}, //生命周期 - 挂载之前
		beforeUpdate () {
		}, //生命周期 - 更新之前
		updated () {
		}, //生命周期 - 更新之后
		beforeDestroy () {
		}, //生命周期 - 销毁之前
		destroyed () {
		}, //生命周期 - 销毁完成
		activated () {
		}, //如果页面有keep-alive缓存功能，这个函数会触发
	}
</script>
<style scoped>
	.header-title{
		text-align: center;
		font-size: 26px;
		position: relative;
		height: 30px;
	}
	.dialogHeaderClose{
		position: absolute;
		top: -10px;
		right: 0px;
		cursor: pointer;
	}
</style>
