<template>
  <div>
    <el-dialog
      :title="title"
      :destroy-on-close="true"
      :visible.sync="Visible"
      width="30%"
      @close="handleClose"
      :close-on-click-modal="false"
      top="10vh"
    >
      <el-form ref="form" :model="form" :rules="rules" size="medium" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input placeholder="请输入角色名称" v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            resize="none"
            :autosize="{ minRows: 3, maxRows: 3 }"
            type="textarea"
            placeholder="请输入备注"
            v-model="form.remark"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="medium" @click="handleRest">重 置</el-button>
        <el-button size="medium" type="primary" :loading="loading" @click="handleOk"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addRole } from '@/api/system';
export default {
  components: {},
  props: {
    title: {
      type: String,
      required: true,
      default: ''
    },
    show: {
      type: Boolean,
      required: true,
      default: false
    }
  },
  data() {
    return {
      loading: false,
      form: {
        name: '',
        remark: ''
      },
      rules: {
        name: [{ required: true, message: '请输入角色名称', trigger: 'blur' }]
      }
    };
  },
  computed: {
    Visible: {
      get() {
        return this.show;
      },
      set(val) {
        this.$emit('update:show', val);
      }
    }
  },
  methods: {
    // 确定
    handleOk() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          let res = await addRole(this.form);
          let { code, msg } = res.data;
          if (code === 1) {
            this.$message.success(msg);
            this.$emit('success');
            this.handleClose();
          }
          this.loading = false;
        }
      });
    },
    // 新增
    onAdd() {
      Object.assign(this.form, this.$options.data().form);
      this.form.id && this.$delete(this.form, 'id');
    },
    // 编辑
    onEdit(row) {
      setTimeout(() => {
        this.form = row;
      }, 10);
    },
    // 重置
    handleRest() {
      this.$resetForm('form');
    },
    // 关闭
    handleClose() {
      this.$resetForm('form');
      this.Visible = false;
    }
  }
};
</script>

<style lang="less" scoped></style>
