function getToday (str) {
  return str.replace(/year/gi, curruYear).replace(/month/gi, currMonth).replace(/date/gi, currDate);
}

function copyWx (wx, text = '复制成功!') {
  const element = document.createElement('input');
  element.value = wx;
  document.body.append(element);
  element.select();
  document.execCommand('Copy');
  document.body.removeChild(element);
  alert(text);
}

function showSuccessModal () {
  var successPopup = document.querySelector('[id^=success-popup]');
  if (successPopup) {
    $(successPopup).fadeIn();
  } else {
    alert("领取成功,稍后将有学习顾问联系您!");
    window.location = '/WebNew/user/Personal/UserInfo.aspx';
  }
}

// 阻止返回
function holdBack () {
  if (sessionStorage.getItem('mini_chat_show')) {
    return sessionStorage.removeItem('mini_chat_show');
  }
  $('[id*=back-popup]').fadeIn();
  history.pushState(null, null, document.URL);
}
// 监听客服,阻止弹出返回弹窗
function listenerChat () {
  if (!document.getElementById('mini_chat')) {
    setTimeout(() => {
      listenerChat();
    }, 1500);
  } else {
    const obs = new MutationObserver((_target) => {
      if ($('#mini_chat').css('display') === 'block') {
        sessionStorage.setItem('mini_chat_show', 1);
      }
    })
    obs.observe(document.getElementById('mini_chat'), { attributes: true })
  }
}
function openModal(dom, selector){
  const id = $(dom).attr('data-popup-id');
  const popup = $('#register-popup-' + id);
  const attrs = dom.attributes;
  // 更改字符
  for (let i = 0; i < attrs.length; i++) {
    const name = attrs[i].name;
    const value = decodeURI(attrs[i].value);
    if(name.indexOf('data-popup-hash-') !== -1){
      const ele = popup.find('[data-modal-hash=' + name.split('data-popup-hash-')[1] + ']');
      if (!value) continue;
      if(value === 'false') {
        ele.hide();
        continue;
      } else if(value === 'true') {
        ele.show();
        continue;
      }
      if(!ele[0]) continue;
      setDomParams(ele[0], value);
    }
  }
  popup.fadeIn();
}


function setDomParams(dom, value){
  var ele = $(dom);
  switch (dom.nodeName) {
    case 'IMG':
      if (ele.attr('src') !== value) {
        ele.attr('src', value);
      }
      break;
    case 'INPUT':
      if (ele.attr('placeholder') !== value) {
        ele.attr('placeholder', value);
      }
      break;
    default:
      if (ele.html() !== value) {
        ele.html(getToday(value));
      }
      break;
  }
}


$(function () {
  listenerChat();
})


window.registerCallback = function () {
  // handle pay
  if(window.defaultPay && window.payConfig){
    if (window.defaultPay.value || window.payConfig.value){
      let value = window.defaultPay.value;
      let sort = window.defaultPay.sort;
      if (window.payConfig.value){
        value = window.payConfig.value;
        sort = window.payConfig.sort;
      }
      if (!value) return;
      if (window.paytype == "wechat" && sort === 'order') {
        var ua = window.navigator.userAgent.toLowerCase();
        if (ua.match(/MicroMessenger/i) == 'micromessenger') {
          window.location = "https://www.acadsoc.com.cn/WebNew/user/MobilePay/AuthResponse.aspx?id=" + value + "&payid=4";
        } else {
          window.location = "https://www.acadsoc.com.cn/WebNew/user/MobilePay/MobilePay.aspx?DesCid=" + value + "&RedPack=0&payid=11";
        }
      } else if (window.paytype == "alipay" && sort === 'order') {
        window.location = "https://www.acadsoc.com.cn/WebNew/user/MobilePay/MobilePay.aspx?DesCid=" + value + "&RedPack=0&payid=10";
      } else {
        window.location = value;
      }
    }
  }
}