$(function(){
  
  $('.order_btn_link').click(orderLinkFn);
  $('.package_list>p').click(packageSelectFn);
  $('.static_buy').click(orderNowFn);

  $('.c_close').on('click', function(){
    $('.coupon-model').hide();
  })


  // 点击减少按钮
  $('.reduce_short').click(function() {
    const countElement = $(this).siblings('.countInfo');
    let currentCount = parseInt(countElement.text(), 10);
    
    if (currentCount > 10) {
      currentCount -= 10;
      countElement.text(currentCount);
      updateTotalPrice($(this).closest('.unlimt-detail'));
    }
  });

  // 点击增加按钮
  $('.add_short').click(function() {
    const countElement = $(this).siblings('.countInfo');
    let currentCount = parseInt(countElement.text(), 10);
    
    currentCount += 10;
    countElement.text(currentCount);
    updateTotalPrice($(this).closest('.unlimt-detail'));
  });

  // 更新总价
  function updateTotalPrice(container) {
    const count = parseInt(container.find('.countInfo').text(), 10);
    const price = parseFloat(container.find('.price span').text());
    const total = (count * price).toFixed(2);
    
    container.find('.total_money_show p:last-child').text(`$${total}`);
  }

  $('#package_num').focus();
  checkUserTypeFn();
  if(localStorage.getItem("userType") && localStorage.getItem("userType") == 0){
    document.addEventListener('visibilitychange', function () {
      if (document.visibilityState === 'visible' && localStorage.getItem('session')) {
        checkUserTypeFn();
      }
    });
  }

  $('.already-pay,.checkpay-close').on('click', function(){
    $('.checkpay').hide();
  })

 
 

})
let submitFlag1 = false;
let countdown_check = 300;
let check_time = null;
const checkUserTypeFn = async () => {
  const res = await $POST("/v1/memberSimple");
  if(res.code!=0)return;
  localStorage.setItem("userType", res.data.repay);
  if(res.data.repay != 0){
    $('.neworder').fadeOut();
  }
}
const session = localStorage.getItem("session");
const user = localStorage.getItem("user");
let lang = $('#language').val();
const orderLinkFn = async (e) => {
  const DOM = $(e.currentTarget);
  const sn = DOM.data("sn");
  const num = DOM.parent().find('.countInfo').text();
  console.log(sn);
  
  countdown_check = 30;
  var currentUrl = window.location.href;
  var match = currentUrl.match(/\.com(.*)/);
  
  if (!session || !user || user == undefined || user == "undefined") {
    let url = "/sign-up/" ;
    if (lang == "zh"){
      url = `/${lang}/sign-up/`
    }
    let r = "";
    if (match && match.length > 1) {
      var contentAfterCom = match[1];
      r = contentAfterCom;
    }
    window.location.href = url +"?r=" + r
  } else {
    settime();
    check_time = setInterval(function () {
      checkpay()
    }, 3000);
    if(submitFlag1) return;
    submitFlag1 = true;
    const res = await $POST("/v1/orderStaticShort", { sn, lang, num });
    submitFlag1 = false;
    if(res.code!=0)return messageFn(res.msg);
    window.open(res.data.url);

  }
}

const checkpay = async () => {
  const res = await $POST("/v1/paymentCheck", { session, lang });
  if(res.code!=0)return;
  if(res.data.show == 1){
    $(".checkpay").show();
    clearInterval(check_time);
  }
}



function settime() {
  clearInterval(check_time);
  check_time = setInterval(function () {
      if (countdown_check == 0) {
          clearInterval(check_time);
      } else {
          countdown_check--;
      }
  }, 1000);
}

let price = $('.package_list>p.active').data("price");
let costprice = $('.package_list>p.active').data("costprice");
let number = 1;
let total = Math.floor(price * number * 100) / 100;;
const packageSelectFn = (e) => {
  $('#package_num').focus();

  const DOM = $(e.currentTarget);
  $(DOM).addClass('active').siblings().removeClass('active');
  price = DOM.data("price");
  costprice = DOM.data("costprice");
  number = $("#package_num").val();
  randerFn();
}


const orderNowFn = async (e) => {
  var currentUrl = window.location.href;
  var match = currentUrl.match(/\.com(.*)/);
  
  if(total<1) return messageFn('Quantity must be greater than 0');;
  if (!session || !user || user == undefined || user == "undefined") {
    let url = "/sign-up/" ;
    if (lang == "zh"){
      url = `/${lang}/sign-up/`
    }
    let r = "";
    if (match && match.length > 1) {
      var contentAfterCom = match[1];
      r = contentAfterCom;
    }
    
    window.location.href = url +"?r=" + r
  } else {
    window.open(requestApi+`/recharge/${lang}/${session}/${total}`) ;

  }
}




// couponCuttime();
