<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close">
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="角色名称" prop="roleName">
        <el-input v-model="form.roleName" />
      </el-form-item>
      <el-form-item label="权限字符" prop="roleKey">
        <el-input v-model="form.roleKey" />
      </el-form-item>
      <el-form-item label="角色顺序" prop="sort">
        <el-input-number
          v-model="form.sort"
          controls-position="right"
          :min="0" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio label="10">正常</el-radio>
          <el-radio label="20">停用</el-radio>
          <!--          <el-radio-->
          <!--            v-for="dict in dict.type.sys_normal_disable"-->
          <!--            :key="dict.value"-->
          <!--            :label="dict.value"-->
          <!--          >{{dict.label}}</el-radio>-->
        </el-radio-group>
      </el-form-item>
      <el-form-item label="菜单权限">
        <div class="vab-tree-border">
          <el-tree
            ref="tree"
            :data="list"
            :default-checked-keys="[]"
            :default-expanded-keys="[]"
            node-key="id"
            show-checkbox>
            <template #default="{ data }">
              <span>{{ data.meta.title }}</span>
            </template>
          </el-tree>
        </div>
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="form.remark"
          placeholder="请输入内容"
          type="textarea" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import roleApi from '@/api/role'
  import menuApi from '@/api/menu'

  export default {
    name: 'RoleEdit',
    data() {
      return {
        form: {
          sort: 0,
          status: '10',
        },
        rules: {
          roleName: [
            { required: true, trigger: 'blur', message: '请输入角色名称' },
          ],
          roleKey: [
            { required: true, trigger: 'blur', message: '请输入权限字符' },
          ],
          status: [{ required: true, trigger: 'blur', message: '请输入状态' }],
        },
        title: '',
        dialogFormVisible: false,
        list: [],
        /* btnRoles demo */
        btnRoles: [
          {
            lable: '读',
            value: 'read:system',
          },
          {
            lable: '写',
            value: 'write:system',
          },
          {
            lable: '删',
            value: 'delete:system',
          },
        ],
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      async fetchData() {
        const { data: data } = await menuApi.getTree({ isAll: 'all' })
        this.list = data
      },
      save() {
        // 具体业务请自行调整
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const tree = this.$refs.tree.getCheckedKeys()
            const treeObject = { 'treeArray:': tree }
            if (this.form.id) {
              const { msg } = await roleApi.doEdit({
                ...this.form,
                ...treeObject,
              })
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            } else {
              const { msg } = await roleApi.doAdd({
                ...this.form,
                ...treeObject,
              })
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            }

            this.$emit('fetch-data')
            this.close()
          }
        })
      },
    },
  }
</script>

<style lang="scss" scoped>
  .vab-tree-border {
    height: 200px;
    padding: $base-padding;
    overflow-y: auto;
    border: 1px solid #dcdfe6;
    border-radius: $base-border-radius;
  }
</style>
