<template>
  <div class="category-add">
    <syDialogInput
      class="dialog_add-form dialog_add-input_name"
      v-model:value="name"
      :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: ["Copydetails"],

  methods: {
    //  关闭弹窗
    async closeDialog() {
      this.name = "";
      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 null;
      }
      let newNote = JSON.parse(this.Copydetails.note);
      let newDetail = JSON.parse(this.Copydetails.detail);
      newNote.evidenceName = this.name;
      for (let index = 0; index < newDetail.length; index++) {
        const element = newDetail[index];

        if (element.data && element.data.length === 0) {
          newDetail.splice(index, 1);
        }
        if (element.label === "ext") {
          element.data[0].data.value = this.name;
        }
      }
      let params = {
        note: JSON.stringify(newNote),
        detail: JSON.stringify(newDetail),
        name: this.name,
      };
      const res = await $ajax({
        type: "post",
        url: GO_URLS.add,
        params: params,
      });
      if (res) {
        ElMessage({
          message: "创建副本成功",
          type: "success",
        });
        this.closeDialog();
        this.$emit("update");
      }
      this.isInActive = false;
      this.name = "";
      return true;
    },
  },
});
</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>
