<!--
 * @Author: Guanghao
 * @Date: 2022-01-05 11:53:19
 * @LastEditors: Guanghao
 * @LastEditTime: 2022-01-05 18:21:24
 * @Description: 角色新建框
-->

<template>
  <div class="container">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="角色说明" prop="description">
        <el-input
          type="textarea"
          v-model="form.description"
          placeholder="请输入"
        />
      </el-form-item>
      <el-form-item class="btns">
        <el-button @click="$emit('on-cancel')">取消</el-button>
        <el-button type="primary" @click="handleConfirmClick">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import RoleBiz from "../../../biz/Rbac/Role.js";

export default {
  data() {
    return {
      // 角色表单
      form: {
        name: "",
        description: "",
      },
      // 角色表单验证
      rules: {
        name: [{ required: true, message: "请先填写角色名称" }],
      },
    };
  },

  methods: {
    // 点击-确定
    handleConfirmClick() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          const { name, description } = this.form;

          try {
            const res = await RoleBiz.add(name, description);
            console.log("roleAdd", res);

            this.$message.success("新建角色成功");
            this.$emit("on-submit", res);
          } catch ({ message }) {
            this.$message.error(message);
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.el-form-item.btns {
  padding-top: 20px;
  margin-bottom: 0;
  ::v-deep .el-form-item__content {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
