<template>
  <div class="root">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="add-ruleForm">
      <el-form-item label="角色名称" prop="roleName">
        <el-input v-model="ruleForm.roleName"/>
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model="ruleForm.description"/>
      </el-form-item>
      <el-form-item label="资源权限" prop="menuArray">
        <el-tree
          ref="tree"
          :data="menuList"
          :default-expanded-keys="ruleForm.menuArray"
          :default-checked-keys="ruleForm.menuArray"
          :props="defaultProps"
          show-checkbox
          node-key="id"/>
      </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>
import { mapState, mapActions, mapMutations } from 'vuex'
import waves from '@/directive/waves' // Waves directive
import { parseTime } from '@/utils'

export default {
  computed: {
    ...mapState('role', {
      menuList: s => s.menuList
    })
  },
  data() {
    return {
      ruleForm: {
        id: '',
        roleName: '',
        description: '',
        menuArray: []
      },
      rules: {
        roleName: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        menuArray: [
          // { type: 'array', required: true, message: '请至少选择一个菜单权限', trigger: 'change' }
        ]
      },
      defaultProps: {
        children: 'children',
        label: 'menuName'
      }
    }
  },
  created() {
    this.ruleForm.id = this.$route.params.id
    this.getDataById()
    this.getMenuList()
  },
  methods: {
    ...mapMutations('role', {
    }),
    ...mapActions('role', [
      'FetchMenuList',
      'UpdateRoleInfo',
      'GetRoleInfo'
    ]),
    submitForm(formName) { // 提交
      // 获取选中的树形节点 key
      this.ruleForm.menuArray = this.$refs.tree.getCheckedKeys()
      console.log(this.ruleForm.menuArray)
      if (this.ruleForm.menuArray.length < 1) {
        this.$message.error('请至少选择一个菜单权限')
        return
      }
      console.log(this.ruleForm.menuArray)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.updateRole()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getMenuList() {
      this.FetchMenuList().then(() => {
      }).catch(() => {
        console.log('get list error')
      })
    },
    resetForm(formName) { // 重置
      this.$refs[formName].resetFields()
      this.$refs.tree.setCheckedKeys([])
      this.ruleForm.roleName = ''
      this.ruleForm.description = ''
    },
    updateRole() {
      // 加载框
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      this.UpdateRoleInfo(this.ruleForm).then((res) => {
        if (res.code === '0') {
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.$router.push({ name: 'role-index' })
          loading.close()
          return
        }
        this.$message.error(res.message)
        loading.close()
      }).catch(() => {
        loading.close()
        console.log('error updateRole')
      })
    },
    getDataById() {
      this.GetRoleInfo(this.ruleForm.id).then((res) => {
        this.ruleForm.roleName = res.data.roleName
        this.ruleForm.description = res.data.description
        this.ruleForm.menuArray = res.data.menuArray
      }).catch(() => {
        console.log('error init role')
      })
    }
  }
}
</script>

<style scoped>
.add-ruleForm{
  width: 800px;
  margin-left: auto;
  margin-right: auto;
  margin-top: 50px;
}
</style>
