<template>
  <div class="category-add">
    <h2 class="dialog_add-title">添加分类</h2>
    <dialog-input
      class="dialog_add-form dialog_add-input_name"
      v-model.trim="name"
      :errorShowing="isErrorShowing"
      placeholder="请输入分类名称"
    />
    <button class="dialog_add-btn_confirm" @click="checkForm">确定</button>
    <button class="dialog_add-btn_cancel" @click="closeDialog">取消</button>
  </div>
</template>

<script>
import DialogInput from "@/components/DialogInput.vue";
import { GO_URLS } from "@/config/URLS";
export default {
  data() {
    return {
      name: "",
      isErrorShowing: false, // 错误提示
      isInActive: false,
    };
  },
  components: {
    DialogInput,
  },
  created() {
    this.name = "";
  },
  methods: {
    //   //   关闭弹窗
    async closeDialog() {
      this.$emit("close", {});
    },
    //   添加按钮
    async checkForm() {
      if (this.isInActive) return;
      this.isInActive = true;
      this.isErrorShowing = true;
      // 追加非空验证
      if (this.name.length === 0) {
        this.$message("分类名称不可为空");
        this.isInActive = false;
        return null;
      }
      const data = await this.$ajax({
        type: "post",
        url: GO_URLS.addFolder,
        params: {
          name: this.name,
          info: this.name,
        },
      });
      if (data) {
        this.$emit("successCallback");
      }
      this.isInActive = false;
    },
  },
};
</script>

<style scoped>
.category-add {
  padding: 34px 50px 0 50px;
  height: 221px; /* 325px - 34px */
  width: 500px; /* 600px - 100px */
}

.dialog_add-title {
  margin: 0;
  color: #000;
  font-size: 22px;
  line-height: 30px;
}

.dialog_add-select_subject {
  margin-top: 29px;
}

.dialog_add-input_name {
  margin: 20px 0 34px 0;
}

.dialog_add-btn_confirm {
  float: right;
  width: 120px;
  height: 40px;
  color: #fff;
  font-size: 14px;
  outline: none;
  background: #3f79fe;
  border-radius: 4px;
  border: none;
  cursor: pointer;
}

.dialog_add-btn_cancel {
  float: right;
  margin-right: 18px; /* 28 - 10 */
  border: none;
  width: 48px; /* 28 + 增大20 */
  height: 40px;
  outline: none;
  background: #fff;
  color: #5c6476;
  font-size: 14px;
  cursor: pointer;
}
</style>
