var gmeinfo;
var gtypeinfo;
var gvalid_info;
jQuery(document).ready(function ($) {
  // single product page  ---------------------------------------------- start
  // member users---------------------------
  $(".direct_sp_member_sub").on("click", function (e) {
    e.preventDefault();
    var $this = $(this);
    var $user_id = $this.siblings(".keraspay_user_id").val();
    var $product_id = $this.siblings(".keraspay_product_id").val();
    var $nonce = $this.siblings(".keraspay_nonce").val();
    var $checksum = $this.siblings(".keraspay_checksum").val();

    var $init_text = $this.text();
    jQuery.ajax({
      url: keraspay_ajax_opt.ajax_url,
      type: "POST",
      data: {
        action: "direct_sp_payment_member",
        userid: $user_id,
        productid: $product_id,
        security: $nonce,
        checksum: $checksum,
      },
      beforeSend: function () {
        $this.text($this.attr("data-connectingtext"));
        $this.prop("disabled", "true");
      },
      success: function (response) {
        if ($.trim(response.data.message) == "success") {
          window.location.replace(response.data.pay_url);
        }
        if ($.trim(response.data.message) == "wrong-user") {
          keraspay_notification(keraspay_messeges.wrongUser);
        }
        if ($.trim(response.data.message) == "wrong-info") {
          keraspay_notification(keraspay_messeges.wrongInfo);
          $this.text($init_text);
        }
        if ($.trim(response.data.message) == "wrong-product") {
          keraspay_notification(keraspay_messeges.wrongProduct);
        }
      },
      complete: function () {
        $this.removeAttr('disabled');
      },
    });
  });
  // single product page  ---------------------------------------------- end

  // custom form logged in users ---------------------------------------------- start

  $(".sub_member").on("click", function (e) {
    e.preventDefault();
    var $this = $(this);
    var $user_id = $this.attr("data-userid");
    var $form_id = $this.attr("data-formid");
    var $checksum = $this.attr("data-checksum");
    var $userisnotuser = $this.siblings(".kf").find('.kiw_submit .userisnotuser').val();

    var $init_text = $this.text();
    jQuery.ajax({
      url: keraspay_ajax_opt.ajax_url,
      type: "POST",
      data: {
        action: "direct_payment_loggedin",
        userid: $user_id,
        formid: $form_id,
        checksum: $checksum,
        userisnotuser: $userisnotuser,
      },
      beforeSend: function () {
        $this.text($this.attr("data-connectingtext"));
        $this.prop("disabled", "true");
        $this.siblings(".kfotp").find(".kloading").removeClass("khide");
      },
      success: function (response) {
        if ($.trim(response.data.message) == "success") {
          window.location.replace(response.data.pay_url);
        }
        if ($.trim(response.data.message) == "wrong-user") {
          keraspay_notification(keraspay_messeges.wrongUser);
        }
        if ($.trim(response.data.message) == "wrong-info") {
          keraspay_notification(keraspay_messeges.wrongInfo);
          $this.text($init_text);
        }
        if ($.trim(response.data.message) == "wrong-formid") {
          keraspay_notification(keraspay_messeges.wrongFormID);
        }
        if ($.trim(response.data.message) == "wrong-checksum") {
          keraspay_notification(keraspay_messeges.wrongChecksum);
        }
        if ($.trim(response.data.message) == "wrong-checksumnq") {
          keraspay_notification(keraspay_messeges.wrongChecksumnq);
        }
      },
      complete: function () {
        $this.siblings(".kfotp").find(".kloading").addClass("khide");
        $this.removeAttr('disabled');
      },
    });
  });
  // custom form logged in users ---------------------------------------------- end
});

var keraspay_messeges = {

  wrongFormID: "اطلاعات فرم ارسال شده معتبر نیست",
  wrongUser: "اطلاعات کاربر معتبر نیست",
  wrongProduct: "اطلاعات محصول معتبر نیست",
  wrongInfo: "اطلاعات وارد شده معتبر نیست",
  wrongChecksum: "کد تطبیق معتبر نیست",
  wrongChecksumnq: "اطلاعات ارسالی همخوانی ندارند",

};

function keraspay_notification(message) {
  jQuery.toast({
    text: message,
    showHideTransition: "fade",
    textAlign: "center",
    stack: false,
    position: "bottom-center",
    bgColor: "#444",
    textColor: "#fff",
    loaderBg: "#9EC600",
    class: "mreeir-error-toast",
    hideAfter: 3000,
    allowToastClose: false,
  });
}