<template>
  <div class="list-box">
    <div class="titleBox">
      <div class="titleBox_l">My order</div>
      <div class="titleBox_r">
        <div class="titleBox_r_l">
          <n-input v-model:value="keyword" type="text" class="custom-placeholder" clearable placeholder="Please enter your order number for inquiry" />
        </div>
        <div class="titleBox_r_r">
          Search
        </div>
      </div>
    </div>
    <div class="list">
      <div class="item" v-for="(v,i) in 3" :key="i">
        <div class="item_title">Order Number：2145878541</div>
        <div class="item_content">
          <img src="" alt="">
          <div>
            <div class="item_content_title">
              <img src="" alt="">
              <div>
                China Industrial Park Industry Market In-depth Research andInvestment Strategy Planning Report 2024-2030
              </div>
            </div>
            <div class="item_content_time">Order placement time：<span>December 21, 2014, 15:11</span></div>
            <div class="item_content_method">Payment method：<span>Alipay payment</span></div>
            <div class="item_content_amount">Payment amount：<span>$4.950.00</span></div>
          </div>
        </div>
        <div class="btnBox">
          <div class="blue">PDF download</div>
          <div class="green">PPT Download</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { useI18n } from "#imports";
import { ref, reactive, onMounted, watch, onUnmounted } from "vue";
import {
  NButton,
  NPagination,
  createDiscreteApi,
  NEmpty,
  NModal,
  NSpin,
} from "naive-ui";
import QrcodeVue from "qrcode.vue";
import { useUserStore } from "@/store/user";
const config = useRuntimeConfig();
const BaseUrl = ref(config.public.baseUrl);
const { t, locale, setLocale } = useI18n();
const pageSize = 10;
const userStore = useUserStore();
const pcShow = ref(true);
const props = defineProps({
  isMobile: {
    type: Boolean,
    default: false,
  },
});

const queryParams = reactive({
  pageNo: 1,
  pageSize: 5,
  orderNumber: "",
});

const pageData = ref({});
const wxQRCode = ref("");
const payVisible = ref<boolean>(false);
const payTitle = ref<string>("");
const count = ref(0);
const timer = ref();
const spinShow = ref(false);
const description = ref(t("myInfo.loading"));
const payShow = ref(false);
const closePayVisible = ref(false);
const closePayId = ref(null);
const emailShow = ref(false);
const activeItem = ref();
// const Message = createDiscreteApi(["message"]);
const model = ref({
  email: "",
  affirmEmail: "",
});
const openInpEmail = (item) => {
  activeItem.value = item;
  emailShow.value = true;
};
const getList = async () => {
  spinShow.value = true;
  description.value = t("myInfo.loading");
  let apiFun = getMyOrder_Api;
  if (userStore.getLang != "zh-CN") {
    apiFun = websiteUserResearchOrderList;
  }
  const { code, data } = await apiFun(queryParams);
  if (code === 200) {
    pageData.value = data;
  }
  spinShow.value = false;
};

// 提交邮箱
const submitEmit = () => {
  if (model.value.affirmEmail != model.value.email) {
    message.message.error("The email addresses are inconsistent.");
    return;
  }
  let param = {
    id: activeItem.value.id,
    email: model.value.email,
  };
  websiteUserResearchOrderUpdateEmail(param).then((res) => {
    if (res.code == 200) {
      message.message.success(t("report.demand.submitSuccess"));
      close();
      queryParams.pageNo = 1;
      getList();
    }
  });
};
const close = () => {
  emailShow.value = false;
};
// onMounted(() => {
getList();
// });
onBeforeMount(async () => {
  pcShow.value = !isMobile();
});
onUnmounted(() => {
  payEnd();
});

watch(
  () => payVisible.value,
  (newVal, oldVal) => {
    if (newVal === false) {
      payEnd();
    }
  }
);

const searchData = (val: string | any) => {
  queryParams.orderNumber = val;
  getList();
};
const goPay = (item) => {
  window.open(item.orderPayUrl);
};
const message = createDiscreteApi(["message"]);

// 下载
const handleTools = async (item: Object, fileType: string) => {
  if (fileType == "pay") {
    if (props.isMobile) {
      if (item.payMethod == "zfb" || item.payMethod == "wx") {
        return message.message.info("请前往PC端个人中心支付订单");
      }
    } else {
      if (item.payMethod == "zfb_h5" || item.payMethod == "wx_h5") {
        return message.message.info("请前往H5端个人中心支付订单");
      }
    }
    toPay(item);
  } else {
    const { code, data } = await getDownUrl_Api({
      researchBriefReportId: item.researchBriefReportId,
      fileType: fileType,
    });
    if (code === 200) {
      let url = `${BaseUrl}/fileupload/${data.filePath}${data.fileName}`;
      if (typeof window !== "undefined") {
        window.open(url);
      }
    }
  }
};

const toPay = (item) => {
  if (item.payMethod == "wx") {
    payVisible.value = true;
    wxQRCode.value = item.orderPayUrl;
    setTimeout(() => {
      payDetail(item);
    }, 500);
  } else {
    downloadFile(item.orderPayUrl);
  }
};

const payDetail = async (item) => {
  payShow.value = true;
  const { code, data } = await bulletinDetailPay_Api({
    researchBriefReportId: item.researchBriefReportId,
    orderNumber: item.orderNumber,
  });
  if (code === 200) {
    if (data.payStatus == 1) {
      message.message.success("支付成功");
      payEnd();
    } else if (data.payStatus != 1) {
      if (payVisible.value) {
        if (count.value >= 5) {
          payEnd();
          message.message.error("请检查是否支付完成");
        } else {
          timer.value = setTimeout(() => {
            count.value++;
            payDetail(item);
          }, 10 * 1000);
        }
      }
    }
  }
};

const payEnd = () => {
  count.value = 0;
  payVisible.value = false;
  clearTimeout(timer.value);
};

const handleClosePay = async (item) => {
  closePayId.value = item.id;
  closePayVisible.value = true;
};

const submitClosePayCallback = async () => {
  let apiFun = closePay_Api;
  if (userStore.getLang == "en-US") {
    // 英文  取消订单
    apiFun = websiteUserResearchCancelOrder;
  }
  const { code } = await apiFun({
    id: closePayId.value,
  });
  if (code === 200) {
    getList();
    message.message.success(t("myInfo.PaymentOrderCancelledSuccessfully"));
  }
  closePayId.value = null;
  closePayVisible.value = false;
};

const cancelClosePayCallback = () => {
  closePayId.value = null;
  closePayVisible.value = false;
};

// 改变页数
const changePage = (page: number) => {
  document.documentElement.scrollTop = 0;
  queryParams.pageNo = page;
  getList();
};

defineExpose({
  searchData,
});
</script>

<style lang="scss"  scoped>
@import "~/assets/css/tool.scss";
.list-box {
  .titleBox {
    display: flex;
    justify-content: space-between;
    padding-bottom: 7px;
    border-bottom: 1px solid #e6e6e6;
    .titleBox_l {
      font-size: 24px;
      font-family: Arial, Arial-Bold;
      font-weight: 700;
      color: #1a1a1a;
      line-height: 44px;
    }
    .titleBox_r {
      display: flex;
      align-items: center;
      width: 500px;
      height: var(--size-44);
      background: #f5f5f5;
      border-radius: 8px;
      overflow: hidden;
      .titleBox_r_l {
        flex-grow: 1;
        .n-input {
          --n-height: var(--size-26) !important;
          line-height: var(--size-26) !important;
          background: #f5f5f5 !important;
          font-size: 14px !important;
        }
      }
      .titleBox_r_r {
        width: var(--size-105);
        font-size: var(--size-16);
        color: #ffffff;
        text-align: center;
        height: var(--size-44);
        background: linear-gradient(0deg, #7b9c4f 0%, #2da19d 100%), #1a1a1a;
        line-height: var(--size-44);
        cursor: pointer;
      }
    }
  }
  .list {
    margin-top: 45px;
    .item {
      border: 1px solid #e6e6e6;
      position: relative;
      margin-bottom: 30px;
      &:last-child {
        margin-bottom: 0;
      }
      .item_title {
        font-size: 20px;
        font-family: Arial, Arial-Bold;
        font-weight: 700;
        text-align: left;
        color: #1a1a1a;
        line-height: 44px;
        letter-spacing: 0.06px;
        padding: 14px 25px;
        background: #f5f5f5;
      }
      .item_content {
        padding: 20px 25px 23px;
        display: flex;
        align-items: center;
        > img {
          width: 148px;
          height: 216px;
          object-fit: contain;
        }
        > div {
          margin-left: 40px;
          .item_content_title {
            font-size: 21px;
            font-family: Arial, Arial-Regular;
            color: #1a1a1a;
            line-height: 28px;
            img {
              display: none;
            }
          }
          .item_content_time,
          .item_content_method {
            display: flex;
            font-size: 16px;
            font-family: Arial, Arial-Regular;
            color: #1a1a1a;
            letter-spacing: 0.05px;
            span {
              color: #808080;
            }
          }
          .item_content_time {
            margin: 20px 0 0;
          }
          .item_content_method {
            margin: 6px 0 10px;
          }
          .item_content_amount {
            display: flex;
            font-size: 16px;
            font-family: Arial, Arial-Regular;
            color: #1a1a1a;
            line-height: 44px;
            letter-spacing: 0.05px;
            align-items: baseline;
            span {
              font-size: 30px;
              color: #ee001f;
            }
          }
        }
      }
      .btnBox {
        display: flex;
        position: absolute;
        right: 20px;
        bottom: 45px;
        > div {
          width: 144px;
          height: 48px;
          border-radius: 8px;
          font-size: 14px;
          font-family: Arial, Arial-Regular;
          text-align: center;
          color: #ffffff;
          line-height: 48px;
          margin-right: 14px;
          cursor: pointer;
          &:last-child {
            margin-right: 0;
          }
        }
        .blue {
          background: #60ac92;
        }
        .green {
          background: #84a86c;
        }
      }
    }
  }
}
@include responseTo("phone") {
  .list-box {
    .titleBox {
      padding-bottom: 20px;
      display: block;
      .titleBox_r {
        width: 100%;
      }
    }
    .list {
      margin-top: 20px;
      .item {
        .item_title {
          padding: 10px 15px;
          font-size: 18px;
          line-height: 24px;
        }
        .item_content {
          padding: 10px;
          > img {
            display: none;
          }
          > div {
            margin: 0;
            .item_content_title {
              display: flex;
              > div {
                font-size: 18px;
                line-height: 26px;
                overflow: hidden;
                text-overflow: ellipsis;
                display: -webkit-box;
                -webkit-line-clamp: 4;
                -webkit-box-orient: vertical;
              }
              > img {
                width: 74px;
                height: 108px;
                object-fit: contain;
                margin-right: 10px;
              }
            }
            .item_content_time,
            .item_content_method {
              font-size: 14px;
            }
            .item_content_amount {
              font-size: 14px;
              line-height: 30px;
              span {
                font-size: 26px;
              }
            }
          }
        }
        .btnBox {
          position: static;
          width: 100%;
          justify-content: center;
          padding-bottom: 20px;
        }
      }
    }
  }
}
</style>
