<template>
  <div id="root">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
      <el-form-item label="角色名称" prop="roleName">
        <el-input v-model="ruleForm.roleName"/>
      </el-form-item>
      <!-- prop="description" -->
      <el-form-item label="角色描述">
        <el-input v-model="ruleForm.description"/>
      </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: {
        roleName: '',
        description: '',
        menuArray: [] // 已选中的权限资源
      },
      menuData: [], // 所有权限资源
      defaultProps: { // 设置树形节点的渲染的属性
        id: 'id',
        children: 'children',
        label: 'menuName'
      },
      rules: {
        roleName: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ],
        menuArray: [
          // { type: 'array', required: true, message: '请至少选择一个权限资源', trigger: 'change' }
        ]
      }
    }
  },
  mounted() {
    this.getMenuList()
  },
  methods: {
    submitForm(formName) {
      // 获取选中的树形节点 key
      this.ruleForm.menuArray = this.$refs.tree.getCheckedKeys()
      this.$refs[formName].validate((valid) => {
        const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
        if (valid) {
          if (this.ruleForm.menuArray.length === 0) {
            this.$message.error('请至少选择一个权限资源')
            loading.close()
            return
          }
          this.$store.dispatch('AddRole', this.ruleForm).then((res) => {
            const resCode = res.code
            if (resCode === '0') {
              this.$message({
                message: res.message,
                type: 'success'
              })
              loading.close()
              this.$router.push({ name: 'role-index' })
            }
          }).catch(() => {
            console.log('error AddRole')
            loading.close()
          })
        } else {
          console.log('error submit!!')
          loading.close()
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
      // 清空选中的树节点
      this.$refs.tree.setCheckedKeys([]);
      this.ruleForm.roleName = ''
      this.ruleForm.description = ''
    },
    handleCheckChange(data, checked, indeterminate) { // 树形图选择事件
      console.log(data, checked, indeterminate)
    },
    getMenuList() { // 获取 菜单列表
      this.$store.dispatch('GetMenuList').then((res) => {
        this.menuData = res.data
      }).catch(() => {
        console.log('error GetMenuList')
      })
    }
  }
}
</script>

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