<template>
  <div id="root">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
      <el-form-item label="角色名称" prop="roleName">
        <span>{{ ruleForm.roleName }}</span>
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <span>{{ ruleForm.description }}</span>
      </el-form-item>
      <el-form-item label="权限资源" prop="menuArray">
        <!-- default-expanded-keys 展示选中的
             default-checked-keys 勾上选中的-->
        <el-tree
          ref="tree"
          :data="menuData"
          :default-expanded-keys="ruleForm.menuArray"
          :default-checked-keys="ruleForm.menuArray"
          :props="defaultProps"
          show-checkbox
          node-key="id"
          @check-change="handleCheckChange"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">去修改</el-button>
        <el-button @click="resetForm('ruleForm')">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        id: '',
        roleName: '',
        description: '',
        menuArray: [] // 已选中的权限资源
      },
      menuData: [], // 所有权限资源
      defaultProps: { // 设置树形节点的渲染的属性
        id: 'id',
        children: 'children',
        label: 'menuName',
        disabled: () => true
      },
      rules: {
        roleName: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ],
        menuArray: [
          { type: 'array', required: true, message: '请至少选择一个活动性质', trigger: 'change' }
        ]
      }
    }
  },
  mounted() {
    this.ruleForm.id = this.$route.query.id
    // 获取用户数据
    this.getInfo()
  },
  methods: {
    submitForm(formName) {
      this.$router.push({ name: 'role-edi', query: { id: this.ruleForm.id }})
    },
    resetForm() {
      // this.$refs[formName].resetFields()
      this.$router.push({ name: 'role-index' })
    },
    handleCheckChange(data, checked, indeterminate) { // 树形图选择事件
      console.log(data, checked, indeterminate)
    },
    getMenuList() { // 获取 菜单列表
      this.$store.dispatch('GetMenuList', this.disabled).then((res) => {
        const resData = res.data
        this.menuData = resData
      }).catch(() => {
        console.log('error GetMenuList')
      })
    },
    getInfo() { // 根据 角色id 角色
      this.$store.dispatch('GetRoleInfo', this.ruleForm.id).then((res) => {
        const resData = res.data
        const resInfo = resData.info
        const resMenu = resData.menuIdList

        this.ruleForm.roleName = resInfo.roleName
        this.ruleForm.description = resInfo.description
        for (const i in resMenu) {
          this.ruleForm.menuArray.push(resMenu[i].menuId)
        }

        this.getMenuList()
      }).catch(() => {
        console.log('error GetInfo')
      })
    }
  }
}
</script>

<style scoped>
.demo-ruleForm {
  width: 500px;
  margin-top: 20px;
  margin-left: auto;
  margin-right: auto;
}
</style>
