<template>
  <div class="category-add">
    <syDialogInput
      class="dialog_add-form dialog_add-input_name"
      v-model:value="name"
      :errorShowing="isErrorShowing"
      placeholder="请输入新的模板名称"
    />
    <sySelect v-model="value" placeholder="请选择">
      <syOption
        v-for="item in MyCategories"
        :key="item.value"
        :label="item.name"
        :value="item.id"
      ></syOption>
    </sySelect>
  </div>
</template>

<script lang="ts">
import { defineComponent } from "vue";
import { ElMessage } from "element-plus";
import { GO_URLS, syDialogInput, sySelect, syOption } from "cqk-sy-ui";
import { $ajax } from "@/service";
export default defineComponent({
  components: { syDialogInput, sySelect, syOption },
  props: {
    activeId: {
      type: Number,
    },
    TemplateID: {
      type: Number,
    },
  },
  data() {
    return {
      isErrorShowing: false,
      isInActive: false,
      name: "",
      MyCategories: [] as any[],
      value: "" as any,
      detail: "",
    };
  },
  mounted() {
    this.getFolderList();
    this.value = this.activeId;
    this.gettemplate(this.TemplateID);
  },
  methods: {
    // 获取模板
    async gettemplate(id: number | undefined) {
      const res = await $ajax({
        type: "get",
        url: GO_URLS.getSystemTemplate,
        params: {
          id: id,
        },
      });
      if (res) {
        this.detail = res.data.detail;
      }
    },
    // 获取我的模板列表
    async getFolderList() {
      const res = await $ajax({
        type: "post",
        url: GO_URLS.folderList,
        params: {
          page: 1,
          page_size: 100,
          template_type: 1,
        },
      });
      if (res) {
        this.MyCategories = res.data.results;
      }
    },
    async checkForm() {
      if (this.isInActive) return;
      this.isInActive = true;
      this.isErrorShowing = true;
      // 追加非空验证
      if (this.name === "") {
        ElMessage("模板名称不可为空");
        this.isInActive = false;
        return false;
      }
      if (this.value === "") {
        this.value = this.MyCategories[0].id;
      }
      const data = await $ajax({
        type: "post",
        url: GO_URLS.customize,
        params: {
          detail: this.detail,
          folder_id: this.value,
          name: this.name,
          info: this.name,
        },
      });
      if (data) {
        this.$emit("successCallback", this.value);
      }
      this.isInActive = false;
      return true;
    },
    closeDialog() {
      this.$emit("close");
    },
  },
  emits: ["successCallback", "close"],
});
</script>

<style lang="scss" scoped>
.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 {
  margin: 0px 0 34px 0;
  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;
}
.el-select {
  width: 100%;
  margin-bottom: 0px;
}
:deep(.el-input__inner) {
  height: 44px;
  padding: 0 26px;
}
</style>
