<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: ["Copydetails"],

  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;
      }
      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 this.$ajax({
        type: "post",
        url: GO_URLS.add,
        params: params,
      });
      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: #0CC399;
  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>

