/*
 * clear_text
 * custom select
 * tabs
 * suggest input
 * tf panel
 * back Page
 * setTimeIn
 * hide popup
 * preloader
 */
(function ($) {
  "use strict";
  /* clear text
  ------------------------------------------------------------------------------------- */
  const clear_text = function () {
    $(".icon-clear").on("click", function () {
      $(".value_input").val("");
    });
  };
    /* custom select
  ------------------------------------------------------------------------------------- */
  const custom_select = function () {
    var langArray = [];
    $(".default-select option").each(function () {
      var img = $(this).attr("data-thumbnail");
      var text = this.innerText;
      var value = $(this).val();
      var item =
        '
 ' +
        text +
        " ";
      langArray.push(item);
    });
    $("#box-select").html(langArray);
    $(".btn-select").html(langArray[0]);
    $(".btn-select").attr("value", "en");
    $("#box-select li").click(function () {
      var img = $(this).find("img").attr("src");
      var value = $(this).find("img").attr("value");
      var text = this.innerText;
      var item =
        '
' +
        text +
        " ";
      langArray.push(item);
    });
    $("#box-select").html(langArray);
    $(".btn-select").html(langArray[0]);
    $(".btn-select").attr("value", "en");
    $("#box-select li").click(function () {
      var img = $(this).find("img").attr("src");
      var value = $(this).find("img").attr("value");
      var text = this.innerText;
      var item =
        ' ' + text + "";
      $(".btn-select").html(item);
      $(".btn-select").attr("value", value);
      $(".banks-select").toggle();
    });
    $(".btn-select").click(function () {
      $(".banks-select").toggle();
    });
    var sessionLang = localStorage.getItem("lang");
    if (sessionLang) {
      var langIndex = langArray.indexOf(sessionLang);
      $(".btn-select").html(langArray[langIndex]);
      $(".btn-select").attr("value", sessionLang);
    } else {
      var langIndex = langArray.indexOf("ch");
      $(".btn-select").html(langArray[langIndex]);
    }
  };
    /* tabs
  ------------------------------------------------------------------------------------- */
  const tfTabs = function () {
    $(".tf-tab").each(function () {
      $(this).find(".content-tab").children().hide();
      $(this).find(".content-tab").children().first().show();
      $(this)
        .find(".menu-tabs")
        .children(".nav-tab")
        .on("click", function (e) {
          e.preventDefault();
          var liActive = $(this).index();
          var contentActive = $(this)
            .siblings()
            .removeClass("active")
            .parents(".tf-tab")
            .find(".content-tab")
            .children()
            .eq(liActive);
          contentActive.addClass("active").fadeIn("slow");
          contentActive.siblings().removeClass("active");
          $(this)
            .addClass("active")
            .parents(".tf-tab")
            .find(".content-tab")
            .children()
            .eq(liActive)
            .siblings()
            .hide();
        });
    });
  };
   /* suggest input
  ------------------------------------------------------------------------------------- */
  const suggestInput = function () {
    $(".tag-money").on("click", function () {
      var val = $(this).text();
      $(".value_input").val(val);
    });
  };
   /* tf panel
  ------------------------------------------------------------------------------------- */
  const tfPanel = function () {
    var panel = $(".tf-panel");
    $(".panel-button").on("click", function () {
      var thispanel = $(this).data('panel');
      panel.removeClass("panel-open");
      $('#'+thispanel).addClass("panel-open");
      $('html').css('overflow', 'hidden');
      $(document).ready(function() {
        if ($('#'+thispanel).children('.panel-box').find('div.app-header').length !== 0) {
          document.querySelector("meta[name='theme-color']").setAttribute('content', '#fc712c');
        }else {
          document.querySelector("meta[name='theme-color']").setAttribute('content', '#191a1e');
        }
      });
    });
    $(".panel_overlay, .clear-panel").on("click", function () {
      panel.removeClass("panel-open");
      $('html').css('overflow', 'auto');
      $(document).ready(function() {
        document.querySelector("meta[name='theme-color']").setAttribute('content', '#28292d');
      });
    });
  };
  /* back Page
  ------------------------------------------------------------------------------------- */
  // const backPage = function () {
  //   $(".back-btn").on("click", function (e) {
  //     e.preventDefault();
  //     window.history.back();
  //   });
  // };
   /* setTimeIn
  ------------------------------------------------------------------------------------- */
  const setTimeIn = function () {
    if ($(".scan").length > 0) {
      setTimeout(function () {
        $(".scan").addClass("panel-open");
      }, 0);
    }
    if ($("#modalhome1").length > 0) {
      let showPopup = sessionStorage.getItem("showPopup");
      if (!JSON.parse(showPopup)) {
        setTimeout(function () {
          $("#modalhome1").modal('show');
        }, 3000);
      }
    }
  };
   /* hide popup
  ------------------------------------------------------------------------------------- */
  const hidePopupNoti = function () {
    $(".btn-hide-modal").on("click", function () {
      sessionStorage.setItem("showPopup", true);
    });
  };
  /* file upload
 ------------------------------------------------------------------------------------- */
  const fileupload = function () {
    var inputArray = document.getElementsByClassName('upload-file');
    if(inputArray.length){
      inputArray[0].addEventListener('change',prepareUpload,false);
        function prepareUpload(event){
            if (this.files && this.files[0]) {
            var img = document.getElementById('image-data');
            img.src = URL.createObjectURL(this.files[0]);
            img.classList.add('my-2');
        }
        const files = event.target.files;
        const fileName = files[0].name;
        const fileSize = (files[0].size/1000).toFixed(2)+'kb';
        const text = document.getElementsByClassName('upload-file-name')[0].getAttribute('data-text');
        document.getElementsByClassName('upload-file-name')[0].innerHTML =  text;
        document.getElementsByClassName('upload-file-name')[0].classList.add('pb-2');
      }
    }
  };
  const setgameheight = function () {
    var contentHeight = $("#gamelist li").height();
    var fullheight = $("#gamelist").height();
    var height = contentHeight * 2 + 13;
    const style = document.querySelector('style');
    if (style) {
      document.querySelector('style').textContent += "#gamelist{max-height: "+height+"px}#gamelist.viewall {max-height: "+fullheight+"px !important;}";
    }
  };
  const accordions = function () {
    $(document).ready(function() {
      const accordionBtns = document.querySelectorAll(".rebate-list");
      accordionBtns.forEach((accordion) => {
        accordion.onclick = function () {
          this.classList.toggle("is-open");
          let content = this.nextElementSibling;
          // console.log(content);
          if (content.style.maxHeight) {
            //this is if the accordion is open
            content.style.maxHeight = null;
          } else {
            //if the accordion is currently closed
            content.style.maxHeight = content.scrollHeight + "px";
            console.log(content.style.maxHeight);
          }
        };
      });
    });
  }
  /* preloader
 ------------------------------------------------------------------------------------- */
 const preloader = function () {
   setTimeout(function () {
     $(".preload").fadeOut("slow", function () {
       $(this).remove();
     });
   }, 300);
 };
  $(function () {
    clear_text();
    custom_select();
    tfTabs();
    suggestInput();
    tfPanel();
    // backPage();
    setTimeIn();
    hidePopupNoti();
    fileupload();
    setgameheight();
    accordions();
    preloader();
  });
})(jQuery);
' + text + "";
      $(".btn-select").html(item);
      $(".btn-select").attr("value", value);
      $(".banks-select").toggle();
    });
    $(".btn-select").click(function () {
      $(".banks-select").toggle();
    });
    var sessionLang = localStorage.getItem("lang");
    if (sessionLang) {
      var langIndex = langArray.indexOf(sessionLang);
      $(".btn-select").html(langArray[langIndex]);
      $(".btn-select").attr("value", sessionLang);
    } else {
      var langIndex = langArray.indexOf("ch");
      $(".btn-select").html(langArray[langIndex]);
    }
  };
    /* tabs
  ------------------------------------------------------------------------------------- */
  const tfTabs = function () {
    $(".tf-tab").each(function () {
      $(this).find(".content-tab").children().hide();
      $(this).find(".content-tab").children().first().show();
      $(this)
        .find(".menu-tabs")
        .children(".nav-tab")
        .on("click", function (e) {
          e.preventDefault();
          var liActive = $(this).index();
          var contentActive = $(this)
            .siblings()
            .removeClass("active")
            .parents(".tf-tab")
            .find(".content-tab")
            .children()
            .eq(liActive);
          contentActive.addClass("active").fadeIn("slow");
          contentActive.siblings().removeClass("active");
          $(this)
            .addClass("active")
            .parents(".tf-tab")
            .find(".content-tab")
            .children()
            .eq(liActive)
            .siblings()
            .hide();
        });
    });
  };
   /* suggest input
  ------------------------------------------------------------------------------------- */
  const suggestInput = function () {
    $(".tag-money").on("click", function () {
      var val = $(this).text();
      $(".value_input").val(val);
    });
  };
   /* tf panel
  ------------------------------------------------------------------------------------- */
  const tfPanel = function () {
    var panel = $(".tf-panel");
    $(".panel-button").on("click", function () {
      var thispanel = $(this).data('panel');
      panel.removeClass("panel-open");
      $('#'+thispanel).addClass("panel-open");
      $('html').css('overflow', 'hidden');
      $(document).ready(function() {
        if ($('#'+thispanel).children('.panel-box').find('div.app-header').length !== 0) {
          document.querySelector("meta[name='theme-color']").setAttribute('content', '#fc712c');
        }else {
          document.querySelector("meta[name='theme-color']").setAttribute('content', '#191a1e');
        }
      });
    });
    $(".panel_overlay, .clear-panel").on("click", function () {
      panel.removeClass("panel-open");
      $('html').css('overflow', 'auto');
      $(document).ready(function() {
        document.querySelector("meta[name='theme-color']").setAttribute('content', '#28292d');
      });
    });
  };
  /* back Page
  ------------------------------------------------------------------------------------- */
  // const backPage = function () {
  //   $(".back-btn").on("click", function (e) {
  //     e.preventDefault();
  //     window.history.back();
  //   });
  // };
   /* setTimeIn
  ------------------------------------------------------------------------------------- */
  const setTimeIn = function () {
    if ($(".scan").length > 0) {
      setTimeout(function () {
        $(".scan").addClass("panel-open");
      }, 0);
    }
    if ($("#modalhome1").length > 0) {
      let showPopup = sessionStorage.getItem("showPopup");
      if (!JSON.parse(showPopup)) {
        setTimeout(function () {
          $("#modalhome1").modal('show');
        }, 3000);
      }
    }
  };
   /* hide popup
  ------------------------------------------------------------------------------------- */
  const hidePopupNoti = function () {
    $(".btn-hide-modal").on("click", function () {
      sessionStorage.setItem("showPopup", true);
    });
  };
  /* file upload
 ------------------------------------------------------------------------------------- */
  const fileupload = function () {
    var inputArray = document.getElementsByClassName('upload-file');
    if(inputArray.length){
      inputArray[0].addEventListener('change',prepareUpload,false);
        function prepareUpload(event){
            if (this.files && this.files[0]) {
            var img = document.getElementById('image-data');
            img.src = URL.createObjectURL(this.files[0]);
            img.classList.add('my-2');
        }
        const files = event.target.files;
        const fileName = files[0].name;
        const fileSize = (files[0].size/1000).toFixed(2)+'kb';
        const text = document.getElementsByClassName('upload-file-name')[0].getAttribute('data-text');
        document.getElementsByClassName('upload-file-name')[0].innerHTML =  text;
        document.getElementsByClassName('upload-file-name')[0].classList.add('pb-2');
      }
    }
  };
  const setgameheight = function () {
    var contentHeight = $("#gamelist li").height();
    var fullheight = $("#gamelist").height();
    var height = contentHeight * 2 + 13;
    const style = document.querySelector('style');
    if (style) {
      document.querySelector('style').textContent += "#gamelist{max-height: "+height+"px}#gamelist.viewall {max-height: "+fullheight+"px !important;}";
    }
  };
  const accordions = function () {
    $(document).ready(function() {
      const accordionBtns = document.querySelectorAll(".rebate-list");
      accordionBtns.forEach((accordion) => {
        accordion.onclick = function () {
          this.classList.toggle("is-open");
          let content = this.nextElementSibling;
          // console.log(content);
          if (content.style.maxHeight) {
            //this is if the accordion is open
            content.style.maxHeight = null;
          } else {
            //if the accordion is currently closed
            content.style.maxHeight = content.scrollHeight + "px";
            console.log(content.style.maxHeight);
          }
        };
      });
    });
  }
  /* preloader
 ------------------------------------------------------------------------------------- */
 const preloader = function () {
   setTimeout(function () {
     $(".preload").fadeOut("slow", function () {
       $(this).remove();
     });
   }, 300);
 };
  $(function () {
    clear_text();
    custom_select();
    tfTabs();
    suggestInput();
    tfPanel();
    // backPage();
    setTimeIn();
    hidePopupNoti();
    fileupload();
    setgameheight();
    accordions();
    preloader();
  });
})(jQuery);