<template>
  <div class="category-add">
    <h2 class="dialog_add-title">重命名</h2>
    <dialog-input
      class="dialog_add-form dialog_add-input_name"
      v-model="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 {
  components: {
    DialogInput,
  },
  data() {
    return {
      isErrorShowing: false,
      isInActive: false,
      name: "",
    };
  },
  props: ["oldName", "oldID"],
  created() {
    this.name = this.oldName;
  },
  methods: {
    //  关闭弹窗
    async closeDialog() {
      this.$emit("close", {});
    },
    // 确认按钮
    async checkForm() {
      if (this.isInActive) return;
      this.isInActive = true;
      this.isErrorShowing = true;
      // 追加非空验证
      if (this.name.trim().length === 0) {
        this.$message.error("分类名称不可为空");
        this.isInActive = false;
        return null;
      }
      const res = await this.$ajax({
        type: "PUT",
        url: GO_URLS.updateFolder,
        params: {
          name: this.name,
          id: this.oldID,
        },
      });
      if (res) {
        this.$message({
          message: "重命名成功",
          type: "success",
        });
        this.closeDialog();
        this.$emit("update");
      }
      this.isInActive = false;
    },
  },
};
</script>
<style scoped lang="less">
.category-add {
  padding: 34px 50px 0 50px;
  height: 245px; /* 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: 42px 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>

