function getCardNo(){
  if(typeof code2lab != 'undefined'){
    code2lab.readNFC();
  }
}
function setCardNo(cardNo){
  if(cardNo.length == 16){
    $("#paypar_account_no").val(cardNo);
    if ($("#read_modal").attr("data-for") == 'member') {
      member_card(cardNo)
    } else if ($("#read_modal").attr('data-for') == 'payment') {
      pay_with_card(cardNo);
    }
    $("#read_modal").modal('hide');
  }
}
function member_card(cardNo) {
  var g_customer_id = '';
  var g_customer_name = '';
  var g_membership_id = '';
  var g_membership_type = '';
  var sale_id     =   0 ;
  if(cardNo != 0){
    $("#oqs_loading_wrapper").show();
    $.ajax({
      type: "POST",
      url: "/foodcourt/"+sale_id+"/get_customer" ,
      data: { filter : cardNo ,type :"card"},
      dataType: "json",
      success: function(data) {
        if (data[0].customer_id == false) {
          swal("Alert!", data[0].message, "error");
          if ($("#order-items-table tr").length > 1){
            $("#create_pay_order").prop("disabled", false);
            $("#create_order").prop("disabled", false);
          }
          $("#oqs_loading_wrapper").hide();
        }else{
          g_customer_id = data[0].customer_id;
          g_customer_name = data[0].name;
          g_membership_id = data[0].membership_id;
          g_membership_type = data[0].membership_type;
          type = $("#link_type").val();
          modify_order = window.location.href.indexOf("modify_order");
          if(!$('.btn').hasClass('req_bill') && !$('.btn').is('#pay')){
            var booking_id  = $('#booking_id').text();
            if (modify_order !=-1) {
              var ajax_url   = '../../../'+type+'/update_modify_order';
              var table_type = $('#table_type').text();
              var table_id    = $('#table_id').text();
              var customer_id = $('#customer_id').text();
              var sale_id     = $('#sale_id').text();
            }else{
              var ajax_url = 'addorders/create';
              var table_type = $('#table_id').find("option:selected").data('type');
              var table_id    = $('#table_id').val();
              var customer_id = g_customer_id;
              var sale_id     = ""
            }

            var order_items = JSON.stringify(get_order_item_rows());

            var params = {'order_source': type, 'order_type': "dine_in",
                          'customer_id': customer_id, 'guest_info': "",
                          'table_id': table_id,
                          'order_items': order_items, 'sale_id': sale_id, 'create_type': "create_pay" };

            if (booking_id.length > 0) {
              params.booking_id = booking_id;
            }
            // alert('ajax', ajax_url);

            $.ajax({
              type: "POST",
              url: ajax_url,
              data: params,
              dataType: "json",
                success:function(result) {
                  // alert(">>>>>>>>>>> result");

                  if (result.sale_id) {
                    // alert(">>>>>>>>>>> modify order after api");
                    sale_id = result.sale_id;
                    window.location.href = '/foodcourt/sale/'+sale_id+'/food_court/payment/';
                  }else {
                    request_bill(g_membership_id, g_customer_id, g_customer_name);
                  }
                }
            });
          } else if($('.btn').is('#pay')){
            sale_id = $('#sale_id').text();
            window.location.href = '/foodcourt/sale/'+sale_id+'/food_court/payment/';
          } else {
            request_bill(g_membership_id, g_customer_id, g_customer_name);
          }
        }
      }
    });
  }
}

function request_bill(membership_id,customer_id,customer_name){
  if (window.location.pathname.includes('foodcourt')) {
    order_id = $('#order_id').val() || $('#save_order_id').val()
    data = { 'customer_id': customer_id }
    $.ajax({
      type: "GET",
      url: "/foodcourt/food_court/"+order_id+"/request_bill" ,
      data: data,
      dataType: "json",
      success: function(data) {
        if(data.status == true)
        {
          window.location.href = '/foodcourt/sale/'+data.sale_id+'/food_court/payment/';
        }else{
          swal("Alert!", "Error!", "error");
          location.reload();
        }
      }
    });
  }
}

function update_sale(membership_id, customer_id, customer_name, sale_id) {
  $("#oqs_loading_wrapper").css("display", "none");
  var customer="";
  var cashier_type = 'food_court';
  if(customer_name != ""){
    customer = '(' + customer_name + ')';
  }
  $("#oqs_loading_wrapper").show();
  $.ajax({
    type: "POST",
    url: "/foodcourt/"+sale_id+"/"+cashier_type+"/customers/update_sale" ,
    data: {customer_id:customer_id,sale_id:sale_id},
    dataType: "json",
    success: function(data) {
      if(data.status == true)
      {
        //change customer detail
        $("#customer_name").html(customer_name);
        $("#membership_id").html(membership_id);
        window.location.href = '/foodcourt/sale/'+sale_id+'/'+cashier_type+'/payment/';
      }else{
        window.location.href = '/foodcourt/sale/'+sale_id+'/'+cashier_type+'/payment/';
      }
    }
  });
}
