<template>
  <el-dialog
    :visible.sync="dialog"
    :title="isAdd ? '新增角色' : '编辑角色'"
    append-to-body
    width="500px"
    @close="close"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      size="small"
      label-width="80px"
    >
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="form.name" style="width: 370px;" />
      </el-form-item>
      <!-- <el-form-item label="角色级别" prop="sort">
        <el-input-number
          v-model.number="form.level"
          :min="1"
          controls-position="right"
          style="width: 370px;"
        />
      </el-form-item> -->
      <el-form-item label="描述信息">
        <el-input
          v-model="form.remark"
          style="width: 370px;"
          rows="5"
          type="textarea"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit"
        >确认</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { add, edit } from "@/api/role";
// import Treeselect from '@riophae/vue-treeselect'
// import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { DATA_SCOPE } from "@/utils/options";
export default {
  // components: { Treeselect },
  props: {
    isAdd: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      dateScopes: DATA_SCOPE,
      loading: false,
      dialog: false,
      form: {
        name: "",
        remark: ""
        // level: 3
      },
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }]
      }
    };
  },
  methods: {
    close() {
      this.resetForm();
    },
    cancel() {
      this.resetForm();
    },
    doSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.name.trim() === "") {
            this.$message({
              message: "名称不能为空",
              type: "error"
            });
            return false;
          }
          this.loading = true;
          if (this.isAdd) {
            this.doAdd();
          } else this.doEdit();
        } else {
          return false;
        }
      });
    },
    doAdd() {
      add(this.form)
        .then(res => {
          if (res.code === 0) {
            this.resetForm();
            this.$notify({
              title: "添加成功",
              type: "success",
              duration: 2500
            });
            this.$parent.init();
          } else {
            this.$notify({
              title: res.message,
              type: "error",
              duration: 2500
            });
          }
          this.loading = false;
        })
        .catch(err => {
          this.loading = false;
          console.log(err.response.data.message);
        });
    },
    doEdit() {
      edit(this.form)
        .then(res => {
          if (res.code === 0) {
            this.resetForm();
            this.$notify({
              title: "修改成功",
              type: "success",
              duration: 2500
            });
            this.$parent.init();
          } else {
            this.$notify({
              title: res.message,
              type: "error",
              duration: 2500
            });
          }
          this.loading = false;
        })
        .catch(err => {
          this.loading = false;
          console.log(err.response.data.message);
        });
    },
    resetForm() {
      this.dialog = false;
      this.$refs["form"].resetFields();
      this.form = { name: "", remark: "", level: 3 };
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-input-number .el-input__inner {
  text-align: left;
}
</style>
