<template>
  <el-dialog :close-on-click-modal="false" id="staffManageDialog" append-to-body :title="item.id ? '编辑角色' : '添加角色'" :visible.sync="dialogVisible">
    <el-form ref="item" :model="item" :rules="rules" label-width="120px" class="content">
      <el-form-item label="角色名称" prop="name">
        <el-input clearable v-model="item.name" />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-input clearable v-model="item.type" />
      </el-form-item>
      <el-form-item label="备注">
        <el-input clearable v-model="item.description" type="textarea" />
      </el-form-item>
      <el-form-item label="是否为默认角色">
        <el-switch v-model="item.defaults" :active-value="active" :inactive-value="inactive" />
      </el-form-item>
      <el-form-item label="是否禁用">
        <el-switch v-model="item.delete" :active-value="active" :inactive-value="inactive" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="submit('item')">确定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { saveRole } from '@/api/system_manage'

export default {
  components: {},
  props: {
    value: {
      type: Boolean,
      default: false
    },
    item: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      dialogVisible: false,
      active: 1,
      inactive: 0,
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' },
          { max: 100, message: '角色名称超长', trigger: 'blur' }],
        type: [
          { max: 20, message: '类型超长', trigger: 'blur' }]
      }
    }
  },
  watch: {
    value(val) {
      this.dialogVisible = val
    },
    dialogVisible(val) {
      this.$emit('input', val)
    }
  },
  mounted() {
  },
  methods: {
    submit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          saveRole(this.item).then(rt => {
            if (rt.status === '200') {
              this.dialogVisible = false
              this.$message.success(`操作成功`)
              this.$emit('confirm', true)
            }
          })
        }
      })
    },
    cancel() {
      this.dialogVisible = false
    }
  }
}
</script>

<style scoped>
.content {
  width: 60%;
  margin: 0 auto;
  overflow: auto;
  padding: 0 10px;
}
.w100 {
  width: 100%;
}
</style>
<style lang="scss">
