<!DOCTYPE html>
<html>
   <head>
      <!-- TABLES CSS CODE -->
      <?php $this->load->view('comman/code_css_datatable');?>
      <?php $this->load->view('comman/code_css_form');?>
      <!-- </copy> -->  
   </head>
   <body class="hold-transition skin-blue sidebar-mini">
      <div class="wrapper">
         <?php $this->load->view('sidebar');?>
         <?php
            if(!isset($brand_name)){
                 $brand_code=$brand_name=$description="";
            }
            ?>
         <!-- Content Wrapper. Contains page content -->
         <div class="content-wrapper">
            <!-- Content Header (Page header) -->
            <section class="content-header">
               <h1>
                  <?=$page_title;?>
                  <small></small>
               </h1>
               <ol class="breadcrumb">
                  <li><a href="<?php echo $base_url; ?>dashboard"><i class="fa fa-dashboard"></i> Home</a></li>
                  <li class="active"><?=$page_title;?></li>
               </ol>
            </section>
            <!-- Main content -->
            <section class="content">
               <div class="row">
                  <!-- right column -->
                  <div class="col-md-12">
                     <!-- Horizontal Form -->
                     <div class="box box-info ">
                        <div class="box-header with-border">
                           <h3 class="box-title">Please Enter Valid Data</h3>
                        </div>
                        <!-- /.box-header -->
                        <!-- form start -->
                        <form class="form-horizontal" id="import-form" enctype="multipart/form-data" method="POST">
                           <input type="hidden" name="<?php echo $this->security->get_csrf_token_name();?>" value="<?php echo $this->security->get_csrf_hash();?>">
                           <input type="hidden" id="base_url" value="<?php echo $base_url;; ?>">
                           <div class="box-body">
                              <div class="form-group">
                                 <label for="brand" class="col-sm-2 control-label">Import Transaction<label class="text-danger">*</label></label>
                                 <div class="col-sm-4">
                                    <input type="file" id="import_file" name="import_file">
                                    <span id="import_file_msg" style="display:block;" class="text-danger">
                                      Note: File must be in CSV format.
                                    </span>
                                    <span id="er_area" style="display:block;" class="text-danger">
                                    </span>
                                 </div>
                              </div>
                           </div>
                           <!-- /.box-footer -->
                           <div class="box-footer">
                              <div class="col-sm-8 text-center">
                                 <div class="col-md-3 ">
                                    <button type="button" id="import" class=" btn btn-block btn-success" title="Save Data"><i class="fa fa-arrow-circle-o-left "></i> Import</button>
                                 </div>
                                 <div class="col-sm-3">
                                    <a href="<?=base_url('dashboard');?>">
                                    <button type="button" class="col-sm-3 btn btn-block btn-warning close_btn" title="Go Dashboard">Close</button>
                                    </a>
                                 </div>
                              </div>
                           </div>
                           <!-- /.box-footer -->
                        </form>
                     </div>
                     <!-- /.box -->
                  </div>
                  <!--/.col (right) -->
               </div>
               <!-- /.row -->
            </section>
            <!-- /.content -->
    <section class="content">
      <div class="row text-center">
        <button type="button" class="btn btn-success" id="startProcess" name="startProcess" style="justify-content: center;">Start Process</button>
      </div>
      <br>
      <div class="row">
        <!-- right column -->
        <div class="col-md-12">
         
          <div class="box">
            <!-- <div class="box-header">

            </div> -->
            <!-- /.box-header -->
            <div class="box-body">

            <table id="example2" class="table table-striped table-bordered" style="width:100%">
               <thead class="bg-primary ">
                  <tr>
                     <th style="text-align:center">#</th>
                     <th style="text-align:center">Transaction Date</th>
                     <th style="text-align:center">Amount</th>
                     <th style="text-align:center">VAT in Amount</th>
                     <th style="text-align:center">Order No</th>
                     <th style="text-align:center">Order Item No</th>
                     <th style="text-align:center">Order Item SKU</th>
                     <th style="text-align:center">Order Item Name</th>
                     <th style="text-align:center">Reference</th>
                     <th style="text-align:center">Batch ID</th>
                  </tr>
                </thead>
                <tbody id="pendingData">
                                  
                </tbody>
              </table>
              
            </div>
            <!-- /.box-body -->
          </div>
          <!-- /.box -->
        </div>
      </div>
    </section>
         </div>
         <!-- /.content-wrapper -->
         <?php $this->load->view('footer');?>
         <!-- Add the sidebar's background. This div must be placed
            immediately after the control sidebar -->
         <div class="control-sidebar-bg"></div>
      </div>
      <!-- ./wrapper -->
      <!-- SOUND CODE -->
      <?php $this->load->view('comman/code_js_sound');?>
      <?php $this->load->view('comman/code_js_datatable');?>
      <!-- TABLES CODE -->
      <?php $this->load->view('comman/code_js_form');?>
    <script type="text/javascript">
        $(document).ready(function() {
         //$('#report-data').DataTable();
            fill_payment_tbData();
        });
    </script>
      <script type="text/javascript">
        //Post the file
        $("#import").click(function(e) {
          var base_url = $("#base_url").val();
          if($("#import_file").val()==''){
            toastr["warning"]("Please select file to Import!");
            failed.currentTime = 0; 
            failed.play();
            return;
          }

          if(confirm("Are you sure ?")){
            e.preventDefault();
            data = new FormData($('#import-form')[0]);//form name
            /*Check XSS Code*/
            if(!xss_validation(data)){ return false; }
            
            $(".box").append('<div class="overlay"><i class="fa fa-refresh fa-spin"></i></div>');
            $("#import").attr('disabled',true);  //Enable Save or Update button
            $.ajax({
            type: 'POST',
            url: base_url+'daraz_order/daraz_trans_csv',
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            success: function(result){
                //console.log(result);
              //alert(result);return;
              
              if(result=="success")
              {
                toastr["success"]("Record Added Successfully!");
                //window.location=base_url+"items";
              }
              else if(result=="failed")
              {
                toastr["error"]("Sorry! Failed to save Record.Try again!");
                 //alert("Sorry! Failed to save Record.Try again");
                 // return;
              }
              else
              {
                //toastr["error"](result);
                result=result.trim().split("<<>>");
                var o_id_Set =result[1];  // ${triningIdArray} this value getting from server 
                //console.log(o_id_Set);  // alerts [1,2]
                var type = typeof(o_id_Set);
                //console.log(type);   // // alerts String

                //remove square brackets
                o_id_Set = o_id_Set.replace('[','');
                o_id_Set = o_id_Set.replace(']','');
                //console.log(o_id_Set);  // alerts 1,2        
                var trainindIdArray = o_id_Set.split(',');
                var er_text = 'Same Order-No found in database, <br>';
                $.each(trainindIdArray, function( i, val ) {
                    er_text += val+'<br>';
                });
                er_text += ' Please check.';
                $('#er_area').empty();
                $('#er_area').append(er_text);              

               toastr["error"]("Sorry! Failed to save Record.Try again!");
              }
              $("#import").attr('disabled',false);  //Enable Save or Update button
              $(".overlay").remove();
              $('#import_file').val("");
              fill_payment_tbData();
             }
             });
        }
        });
      </script>
<script>
    function fill_payment_tbData(){
        $.ajax({
            url:"<?php echo site_url('daraz_order/daraz_pay_ajax_list')?>",
            type: 'GET',
            dataType: 'json',
            success: function (data) {

               var table;
               table = $('#example2').DataTable();
               table.clear();
               var indexNo = 1;
                $.each(data.order_pay_info, function(){
                      table.row.add([indexNo,this['Transaction_Date'],this['Amount'], this['VAT_in_Amount'], this['Order_No'], this['Order_Item_No'], this['Seller_SKU'], this['item_name'], this['Reference'], this['batchID']]);
                      indexNo++;
                });
                table.draw();
            },
            error: function (xhr, ajaxOptions, thrownError) {
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            }
        });
    }
</script>

<script>
    $('#startProcess').on('click',function(){
        $.ajax({
            url: $("#base_url").val()+'daraz_order/update_pay_data',
            type: 'GET',
            dataType: 'json',
            success: function(data) {
                if (data=="ok") {
                    fill_payment_tbData();
                    toastr["success"]("Payment Details Updated!!");
                }else if (data=="error"){
                    //swal("Sorry","Error occured !!","error");
                    toastr["error"]("Sorry! Something Wrong.check orders!");
                }
            }
        });
    });
</script>
      <!-- Make sidebar menu hughlighter/selector -->
      <script>$(".<?php echo basename(__FILE__,'.php');?>-active-li").addClass("active");</script>
   </body>
</html>

