<div class="container-fluid">
  <div id="loading_wrapper" style="display:none;">
    <div id="loading"></div>
  </div>

  <div class='row clearfix'>
    <div class='col-md-10'>
      <h1><%= t :close_cashier %></h1>
      <br>
      <table class='table table-striped'>
        <tr>
          <td><%= t :shift_started_at %></td>
          <td><%= @shift.shift_started_at.utc.getlocal.strftime("%d/%m/%Y - %I:%M %p") rescue '-' %></td>
        </tr>
        <tr>
          <td><%= t :cashier_name %></td>
          <td><%= @shift.employee.name rescue ''%></td>
        </tr>

      </table>
      <hr>
      <div class='row clearfix '>
        <div class='col-md-5'>
          <span style='font-size:20px;'><b><%= t :closing_balance %></b></span>
        </div>
        <div class='col-md-6'>
          <span style='font-size:20px;'><b><input type='number' class="form-control" id='closing_balance_amount' value='' onkeypress="return isNumberKey(event);" onkeyup="notNegative(this.value);"></b></span>
        </div>
      </div>
    </div>
    <div class="col-lg-2 col-md-2 col-sm-2">
    <button type="button" class="btn bg-default btn-block" id='back'><i class="material-icons">reply</i> <%= t("views.btn.back") %> </button>
    <button type="button" class="btn bg-blue btn-block green" id='close_cashier'> <%= t("views.btn.close_cashier") %> </button>
    <% if @bank_integration == '1' && @cashier_type!="ordering" %>
      <button type="button" class="btn bg-blue btn-block green" id="card_settlement"> <%= t("views.btn.card_settle") %> </button>
    <% end %>
    </div>
  </div>

  <div class="modal fade" id="cardSettleModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="cardSettleModal">Select Device: </h4>
            </div>
            <div class="modal-body">
               <select class="form-control show-tick sel_com_port"  id="sel_com_port" >
                 
                </select>   
            </div>
            <div class="modal-footer  p-r-30">
                <button type="button" class="btn btn-link btn-danger waves-effect" data-dismiss="modal">CLOSE</button>
                &nbsp;  &nbsp;
               <button type="button" class="btn btn-link bg-blue waves-effect" id="card_settle">OK</button>
            </div>
        </div>
    </div>
  </div>
</div>
<script>

  function isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) {
      return false;
    } else {
      return true;
    }      
  }

  function greaterThanOne(val){
    if(parseInt(val)==0) $("#closing_balance_amount").val(1);
  }

  var cashier_type = '<%= @cashier_type %>';
  $('#close_cashier').on('click',function(){
    var amount = $('#closing_balance_amount').val();
    var shift_id = "<%= @shift.id rescue ""%>"
    $.ajax({type: "POST",
      url: "<%= origami_close_shift_path %>",
      data: "closing_balance="+ amount + "&shift_id="+ shift_id,
      success:function(result){
        console.log(result)
        window.location.href = '/';
      }
    });
  });

  $('#back').on('click',function(){
    if(cashier_type=='cashier'){
      window.location.href = '/origami';
    }else if(cashier_type=='quick_service'){
      window.location.href = '/origami/quick_service';
    }else{
      window.location.href = '/origami/order_reservation';
    }
  });

  /** start CB Card Settle process **/
  $("#card_settlement").on('click', function(){
    if(typeof code2lab != 'undefined'){
      $('#cardSettleModal').modal({ keyboard: false, backdrop: false });
      code2lab.getCommPorts();
    }else{
      swal({
        title: 'Oops',
        text: 'Settlement can not print out in browser!',
        type: 'error',
        html: true,
        closeOnConfirm: false,
        closeOnCancel: false,
        allowOutsideClick: false
      }, function () {
        window.location.href = '/origami/shift/'+cashier_type+'/close';
      });
    }
  });

  //CB Bank payment settlement ECR integration
  function setCommPorts(comPortLists) {
      // alert(comPortLists);
      $('#sel_com_port').html("");
      var jsonPortLists = $.parseJSON(comPortLists);
      if((jsonPortLists!=undefined && jsonPortLists!='') && (jsonPortLists.length > 0)){
          $.each(jsonPortLists,function(key,value){
              $('#sel_com_port').append("<option value='"+value+"'>"+value+"</option>");
          });
      }
      else{
        swal({
          title: 'Oops',
          text: 'Payment can not pay in browser!',
          type: 'error',
          html: true,
          closeOnConfirm: false,
          closeOnCancel: false,
          allowOutsideClick: false
        }, function () {
          window.location.href = '/origami/shift/'+cashier_type+'/close';
        });
      }
  }

  $("#card_settle").on('click', function () {
      $("#loading_wrapper").show();
      $('#cardSettleModal').modal('hide');
      reqCBBankMPUSettlement();
  });

  //add req data to card_settle_trans table
  function reqCardSettleTrans(cmd_type,payment_type,com_port) {
    var jobj = {"cmd_type" : cmd_type, "payment_type" : payment_type};
    $.ajax({
        type: "POST",
        url: "/origami/bank_integration/settle_trans",
        data: {type:"request", data: jobj},
        dataType: "json",
        success: function(data) {
            if (data.status == "success"){
               resCardSettleTrans(data.card_settle_trans_id,cmd_type,payment_type,com_port);
            }
         }
      });
  }

  //add res data to card_settle_trans table
  function resCardSettleTrans(card_settle_trans_id,cmd_type,payment_type,com_port) {
    var resMsg = "";
    if($("#loading_wrapper").is(':visible')){
      resMsg = code2lab.reqBankPayment(cmd_type, payment_type, 1, "", com_port);
    }
    
    if(resMsg.includes("STATUS")){
      var jobj = $.parseJSON(resMsg);

      $.ajax({
        type: "POST",
        url: "/origami/bank_integration/settle_trans",
        data: {type:"response", card_settle_trans_id: card_settle_trans_id, data: jobj},
        dataType: "json",
        success: function(data) {
          if(data.status == "success"){
            if(payment_type == "MPU"){
              reqCBBankVMJSettlement();
            }
            else if(payment_type == "VMJ"){
              reqCBBankJCBSettlement();
            }
            else if(payment_type == "JCB"){
              reqCBBankCUPSettlement();
            }
            else if(payment_type == "CUP"){
              $("#loading_wrapper").hide();
              swal({
                title: 'Information!',
                text: 'Settlement is successfully.',
                type: 'success',
                html: true,
                closeOnConfirm: false,
                closeOnCancel: false,
                allowOutsideClick: false
              }, function () {
                window.location.href = '/origami/shift/'+cashier_type+'/close';
              });
              // reqCBBankAlipaySettlement();
            }
            // else if(payment_type == "Alipay"){
            //   reqCBBankIPPSettlement();
            //   $('#loading').hide();
            // }
          }
        }
      });
    }
    else{
      $("#loading_wrapper").hide();
      swal ( "Oops" ,  resMsg.toString() ,  "error" );
    }
  }

  //settle for MPU / MPU-UPI
  function reqCBBankMPUSettlement() {
    var com_port = $('#sel_com_port').val();
    var cmd_type = "SETTLEMENT";
    var payment_type = "MPU";
    $("#loading_wrapper").show();
    reqCardSettleTrans(cmd_type,payment_type,com_port);
  }

  //settle for VMJ (VISA, MASTER)
  function reqCBBankVMJSettlement() {
    var com_port = $('#sel_com_port').val();
    var cmd_type = "SETTLEMENT";
    var payment_type = "VMJ"; //VISA,Master and JCB
    $("#loading_wrapper").show();
    reqCardSettleTrans(cmd_type,payment_type,com_port);
  }

  //settle for JCB
  function reqCBBankJCBSettlement() {
    var com_port = $('#sel_com_port').val();
    var cmd_type = "SETTLEMENT";
    var payment_type = "JCB";
    $("#loading_wrapper").show();
    reqCardSettleTrans(cmd_type,payment_type,com_port);
  }

  //settle for International Union Pay (CUP)
  function reqCBBankCUPSettlement() {
    var com_port = $('#sel_com_port').val();
    var cmd_type = "SETTLEMENT";
    var payment_type = "CUP";
    $("#loading_wrapper").show();
    reqCardSettleTrans(cmd_type,payment_type,com_port);
  }

  //settle for Alipay
  function reqCBBankAlipaySettlement() {
    var com_port = $('#sel_com_port').val();
    var cmd_type = "SETTLEMENT";
    var payment_type = "Alipay";
    $("#loading_wrapper").show();
    reqCardSettleTrans(cmd_type,payment_type,com_port);
  }

  //settle for insert/direct swipe (now does not using this state - future one)
  function reqCBBankIPPSettlement() {
    var com_port = $('#sel_com_port').val();
    var cmd_type = "SETTLEMENT";
    var payment_type = "IPP";
    $("#loading_wrapper").show();
    reqCardSettleTrans(cmd_type,payment_type,com_port);
  }
  /** end CB Card Settle process **/
</script>
