<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 { defineComponent } from "vue";
import { GO_URLS, syDialogInput, syMessage } from "cqk-sy-ui";
import { $ajax } from "@/service";
export default defineComponent({
  props: ["pre_hash", "evidenceName", "baseProof", "navigate", "callback"],
  components: {
    syDialogInput,
  },
  data() {
    return {
      name: "",
      isErrorShowing: false,
      isInActive: false,
    };
  },
  computed: {
    getBaseProof() {
      return this.baseProof;
    },
  },
  methods: {
    async checkForm() {
      if (this.isInActive) return;
      this.isInActive = true;
      this.isErrorShowing = true;
      // 追加非空验证
      if (this.name.trim().length === 0) {
        syMessage({
          showClose: true,
          message: "名称不可为空",
          type: "warning",
        });
        this.isInActive = false;
        return null;
      }
      const data = await $ajax({
        type: "post",
        url: GO_URLS.increment,
        params: {
          base_id: this.getBaseProof.id, //基础存证id
          base_hash: this.getBaseProof.hash, //基础存证哈希
          name: this.name,
          pre_hash: this.pre_hash, //上一条存证哈希
          note: JSON.stringify({
            version: 1,
            evidenceName: this.evidenceName,
            stepName: this.name,
            banners: [],
          }),
          detail: JSON.stringify([
            {
              label: "ext",
              key: "",
              type: 3,
              data: [
                {
                  data: {
                    type: "text",
                    format: "string",
                    value: this.name,
                  },
                  type: 0,
                  key: "存证名称",
                  label: "存证名称",
                },
                {
                  data: {
                    type: "text",
                    format: "hash",
                    value: this.getBaseProof.hash,
                  },
                  type: 0,
                  key: "basehash",
                  label: "basehash",
                },
                {
                  data: {
                    type: "text",
                    format: "hash",
                    value: this.pre_hash,
                  },
                  type: 0,
                  key: "prehash",
                  label: "prehash",
                },
                {
                  data: {
                    type: "text",
                    format: "string",
                    value: this.name,
                  },
                  type: 0,
                  key: "存证类型",
                  label: "存证类型",
                },
              ],
            },
            {},
          ]),
        },
      });
      if (data) {
        syMessage({
          message: "增量更新成功",
          type: "success",
        });
        if (this.callback) {
          this.callback(data.data.id);
          this.$emit("close", {});
        } else
          this.navigate({
            path: "/tracingDetail",
            query: {
              id: this.getBaseProof.id,
              activeProofId: data.data.id,
            },
          });
      }
      this.isInActive = false;
    },
    async closeDialog() {
      this.$emit("close", {});
    },
  },
});
</script>
<style scoped lang="scss">
.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>
