<template>
  <div class="category-add">
    <syDialogInput
      class="dialog_add-form dialog_add-input_name"
      v-model:value="name"
      :maxlength="30"
      :errorShowing="isErrorShowing"
      placeholder="请输入新的分类名称"
    />
  </div>
</template>

<script lang="ts">
import { $ajax } from "@/service";
import { GO_URLS, syDialogInput, syMessage as ElMessage } from "cqk-sy-ui";
import { defineComponent } from "vue";
export default defineComponent({
  components: {
    syDialogInput,
  },
  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) {
        ElMessage.error("分类名称不可为空");
        this.isInActive = false;
        return false;
      }
      const res = await $ajax({
        type: "put",
        url: GO_URLS.updateFolder,
        params: {
          name: this.name,
          id: this.oldID,
        },
      });
      if (res) {
        ElMessage({
          message: "重命名成功",
          type: "success",
        });
        this.closeDialog();
        this.$emit("update");
      }
      this.isInActive = false;
    },
  },
});
</script>
<style scoped lang="scss">
.category-add {
  box-sizing: content-box;
}

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

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

.dialog_add-input_name {
  width: 100%;
}

.dialog_add-btn_confirm {
  float: right;
  width: 120px;
  height: 40px;
  color: #fff;
  font-size: 14px;
  outline: none;
  background: var(--sy-blue);
  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>
