<template>
  <Modal v-model="visible" title="新增角色" class-name="v-modal permission-modal" :mask-closable="false" :width="400" :footer-hide="true">
    <Form ref="role-form" :model="form" :label-width="formOptions.labelWidth" :rules="rules">
      <FormItem label="角色名" prop="name">
        <Input v-model.trim="form.name" clearable placeholder="请输入角色名(1~6位)" :maxlength="6" :size="formOptions.size"></Input>
      </FormItem>
    </Form>
    <div class="action-wrap">
      <Button class="btn-primary-reverse" @click="visible = false">取消</Button>
      <Button type="primary" @click="checkForm('role-form', addRole)" :disabled="btnLoading" :loading="btnLoading">确定</Button>
    </div>
  </Modal>
</template>

<script>
import formMixin from '../../mixins/form'
import role from '../../apis/role'

export default {
  mixins: [formMixin],
  props: {
    sysId: String
  },
  data () {
    return {
      form: {
        name: ''
      },
      formOptions: {
        labelWidth: 70,
        size: 'large'
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名' }
        ]
      },
      visible: false,
      btnLoading: false
    }
  },
  created () { },
  methods: {
    openModal () {
      this.visible = true
      this.init()
    },
    init () {
      this.resetForm('role-form')
      this.btnLoading = false
    },
    async addRole () {
      let { name } = this.form
      try {
        this.btnLoading = true
        let res = await role.addRole(this.sysId, this.form.name)
        this.$emit('success', res.data.id)
        this.visible = false
      } catch (e) {
        this.btnLoading = false
      }
    }
  }
}
</script>