<template>
  <span>
    <a-modal
      wrap-class-name="error-message-toast"
      :destroy-on-close="true"
      :mask-closable="false"
      :keyboard="false"
      :visible="visible"
      :title="$t('ERROR_MESSAGE')"
      :z-index="100000"
      @cancel="handleCancel"
    >
      <section>
        <h3 class="section-title title-error">
          <a-icon type="exclamation-circle" />
          {{ $t("ERROR_MESSAGE") }}
        </h3>
        <div class="section-content content-error">
          {{ error.msg || $t("NO_DATA") }}
        </div>
      </section>
      <section>
        <h3 class="section-title title-tip">
          <a-icon type="tool" />
          {{ $t("OP_TIP") }}
        </h3>
        <div class="section-content content-tip">
          {{ error.tip || $t("NO_DATA") }}
        </div>
      </section>
      <template slot="footer">
        <span> {{ $t("SERIAL_NUMBER") }}:{{ error.logId }} </span>

        <div>
          <a-button
            class="report-case"
            :disabled="
              !(Boolean(errorId) && applyReport && Boolean(businessName))
            "
            @click="postReportCase"
          >
            {{ $t("ONE_CLICK_REPORT") }}
          </a-button>
          <a-button type="primary" @click="handleCancel">
            {{ $t("CLOSE_AFTER_A_FEW_SECOND").replace("{}", countdown) }}
          </a-button>
        </div>
      </template>
    </a-modal>
    <a-modal
      wrap-class-name="report-success-modal"
      :visible="messageVisible"
      :title="null"
      :mask="false"
      :content="null"
      :footer="null"
      :closable="false"
      :z-index="100002"
    >
      <div class="ant-message-notice">
        <div class="ant-message-notice-content">
          <div class="ant-message-custom-content ant-message-success">
            <a-icon type="check-circle" theme="filled" />
            <span>{{ $t("REPORT_SUCCESS") }}</span>
          </div>
        </div>
      </div>
    </a-modal>
  </span>
</template>

<script>
import ApiConstant from "@/api/ApiConstant";

export default {
  data() {
    return {
      visible: false,
      toastNode: null,
      timer: null,

      enableCountDown: true,
      countdown: 3,

      businessName: "",
      error: {},
      errorId: "",
      // 申请报案
      applyReport: true,

      messageVisible: false,
    };
  },
  computed: {
    instance() {
      if (window) {
        return window.$nuxt;
      }
      return null;
    },
  },
  watch: {
    visible: {
      handler(val) {
        if (val) {
          this.postErrorRecord();
          this.enableCountDown &&
            this.$nextTick(() => {
              this.toastNode = document.querySelector(
                ".error-message-toast .ant-modal-content"
              );
              if (this.toastNode) {
                this.handleRemove();
                this.handleBind();
                this.mouseLeaveEvent();
              }
            });
        }
      },
      immediate: true,
    },
  },
  beforeDestroy() {
    this.handleRemove();
    this.mouseEnterEvent();
  },
  methods: {
    // 异常记录
    postErrorRecord() {
      const http = this.$axios || this.instance?.$axios || null;
      const userName = this.$store.state.auth?.user?.user?.userName || "";
      http &&
        userName &&
        http({
          method: "post",
          url: `${ApiConstant.PREFIX_CORE}/api/sys/exception`,
          data: {
            errorMsg: this.error.msg,
            businessName: this.businessName,
            logId: this.error.logId,
            isCancelApiLoad: true,
            userName,
          },
        }).then((res) => {
          this.errorId = res.data.data;
        });
    },
    // 一键报案
    postReportCase() {
      const http = this.$axios || this.instance?.$axios || null;
      http &&
        this.errorId &&
        http({
          method: "put",
          url: `${ApiConstant.PREFIX_CORE}/api/sys/exception/report`,
          params: {
            id: this.errorId,
            isCancelApiLoad: true,
          },
        })
          .then((res) => {
            // 申请报案
            this.applyReport = false;

            this.reportSuccessMsg();
          })
          .catch((_) => {
            // 申请报案
            this.applyReport = true;
          });
    },
    reportSuccessMsg() {
      this.messageVisible = true;
      let secondsToGo = 3;
      const interval = setInterval(() => {
        secondsToGo -= 1;
        if (secondsToGo <= 0) {
          clearInterval(interval);
          this.messageVisible = false;
        } else {
          secondsToGo -= 1;
        }
      }, 1000);
    },
    handleCancel() {
      this.handleRemove();
      this.mouseEnterEvent();
      this.visible = false;
    },
    handleRemove() {
      if (this.toastNode) {
        this.toastNode.removeEventListener("mouseenter", this.mouseEnterEvent);
        this.toastNode.removeEventListener("mouseleave", this.mouseLeaveEvent);
      }
    },
    handleBind() {
      if (this.toastNode) {
        this.toastNode.addEventListener("mouseenter", this.mouseEnterEvent);
        this.toastNode.addEventListener("mouseleave", this.mouseLeaveEvent);
      }
    },
    mouseLeaveEvent() {
      this.mouseEnterEvent();
      this.timer = setInterval(() => {
        this.countdown -= 1;
        if (this.countdown === 0) {
          this.handleCancel();
        }
      }, 1000);
    },
    mouseEnterEvent() {
      clearInterval(this.timer);
      this.timer = null;
    },
  },
};
</script>

<style lang="less">
.error-message-toast {
  .ant-modal-content {
    border-radius: 4px;
  }
  .ant-modal-header {
    padding: 0 20px;
    height: 50px;
    background-color: rgba(240, 240, 240, 0.78);
    border-radius: 10px 10px 0 0;
    .ant-modal-title {
      font-weight: 700;
      line-height: 50px;
    }
  }
  .ant-modal-body {
    padding: 20px;
    .section-title {
      font-size: 16px;
      line-height: 20px;
      margin-bottom: 0;
      height: 30px;
    }
    .title-error .anticon {
      color: rgb(255, 153, 3);
      font-weight: 700;
      font-size: 18px;
      margin-right: 8px;
    }
    .title-tip .anticon {
      transform: rotate(-90deg);
      color: #005583;
      font-weight: 700;
      font-size: 18px;
      margin-right: 8px;
    }
    .section-content {
      min-height: 36px;
      padding: 10px 14px;
      line-height: 26px;
    }
    .content-error {
      background-color: rgba(0, 85, 131, 0.1);
      margin-bottom: 20px;
    }
    .content-tip {
      background-color: rgba(0, 85, 131, 0.1);
    }
  }
  .ant-modal-footer {
    display: flex;
    justify-content: space-between;
    line-height: 36px;
    .countdown {
      margin-right: 12px;
    }
    .report-case {
      background-color: rgb(255, 153, 3);
      border-color: rgb(255, 153, 3);
      color: #fff;
      &:hover {
        background-color: rgba(255, 153, 3, 0.85);
        border-color: rgba(255, 153, 3, 0.85);
      }
      &[disabled] {
        background-color: rgba(255, 153, 3, 0.55);
        border-color: rgba(255, 153, 3, 0.55);
        color: #fff;
      }
    }
  }
}
.report-success-modal {
  .ant-modal {
    top: 12px;
    width: fit-content !important;
    padding-bottom: 0;
    &-body {
      padding: 0;
    }
    &-content {
      box-shadow: 0 0;
      background-color: transparent;
    }
  }
}
</style>
