<!DOCTYPE html>
<html>
   <head>
      <!-- TABLES CSS CODE -->
      <?php $this->load->view('comman/code_css_form');?>
      <?php $this->load->view('comman/code_css_datatable');?>
      <!-- </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 orders<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-6">
         
          <div class="box">
            <div class="box-header">
             <h5>Pending Orders : <label id="lbl_pendingCount"></label></h5>
            </div>
            <!-- /.box-header -->
            <div class="box-body">

              <table class="table table-bordered table-hover " id="report-data" >
                <thead>
                <tr>
                  <th style="text-align:center">#</th>
                  <th style="text-align:center">ITEM NAME</th>
                  <th style="text-align:center">Order-number</th>
                  <th style="text-align:center">REQUESTED-SKU</th>
                  <th style="text-align:center">CREATE DATE</th>
                </tr>
                </thead>
                <tbody id="pendingData">
                                  
                </tbody>
              </table>
              
            </div>
            <!-- /.box-body -->
          </div>
          <!-- /.box -->
        </div>
        <div class="col-md-6">
         
          <div class="box">
            <div class="box-header">
                <h5>Issued Orders : <label id="lbl_issuedCount"></label></h5>
            </div>
            <!-- /.box-header -->
            <div class="box-body">
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label for="formDate" class="col-sm-3 control-label">Form Date</label>
                            <div class="col-sm-9">
                              <input type="date" class="form-control" id="formDate" name="formDate" value="<?php $date = new DateTime("now", new DateTimeZone('Asia/Colombo')); $date->modify('-30 day'); echo $date->format('Y-m-d');?>">
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label for="toDate" class="col-sm-3 control-label">To Date</label>
                            <div class="col-sm-9">
                              <input type="date" class="form-control" id="toDate" name="toDate" value="<?php $date = new DateTime("now", new DateTimeZone('Asia/Colombo') ); echo $date->format('Y-m-d');?>">
                            </div>
                        </div>
                    </div>
                    <!-- <div class="col-md-2">
                        <div class="form-group">
                            <button type="button" class="btn btn-success" id="btn-refresh" name="btn-refresh">Refrash</button>
                        </div>
                    </div> -->
                </div>
                <div class="table-responsive">

                    <table class="table table-bordered table-hover " id="issued-report-data" >
                        <thead>
                            <tr>
                                <th style="text-align:center">#</th>
                                <th style="text-align:center">ITEM NAME</th>
                                <th style="text-align:center">Order-number</th>
                                <th style="text-align:center">REQUESTED-SKU</th>
                                <th style="text-align:center">CREATE DATE</th>
                            </tr>
                        </thead>
                        <tbody id="issueData">
                                            
                        </tbody>
                    </table>

                </div>
              
              
            </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>
        $(document).ready(function () {
            //$('#issued-report-data').DataTable();
            $('#report-data').DataTable();
        });
      </script>
    <script type="text/javascript">
        $(document).ready(function() {
            fill_pending_tbData();
            fill_issued_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/import_daraz_orders_csv',
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            success: function(result){
                //console.log(result);
              //alert(result);return;
              
              if(result=="success")
              {
                toastr["success"]("Record Updated 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_pending_tbData();
             }
             });
        }
        });
      </script>
<script>
    function fill_pending_tbData(){
        $.ajax({
            url:"<?php echo site_url('daraz_order/daraz_pending_ajax_list')?>",
            type: 'GET',
            dataType: 'json',
            success: function (data) {
              var table;
              table = $('#report-data').DataTable();
              table.clear();
              var indexNo = 1;
              $.each(data.pending_order_info, function(){
                    table.row.add([indexNo,this['ItemName'],this['OrderNumber'], this['SellerSKU'], this['Createdat']]);
                    indexNo++;
              });
              table.draw();

                $('#lbl_pendingCount').empty();lbl_issuedCount
                $.each(data.pending_order_count, function(){
                  $('#lbl_pendingCount').append(this['pending_count']);
                });
            },
            error: function (xhr, ajaxOptions, thrownError) {
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            }
        });
    }
</script>
<script>
    $('#formDate').on('change',function(){
        fill_issued_tbData();
    });
</script>
<script>
    $('#toDate').on('change',function(){
        fill_issued_tbData();
    });
</script>
<script>
    function fill_issued_tbData(){
        var formDate = $('#formDate').val();
        var toDate = $('#toDate').val();
        $.ajax({
            url:"<?php echo site_url('daraz_order/daraz_issued_ajax_list')?>",
            type: 'POST',
            dataType: 'json',
            data : {formDate:formDate,toDate:toDate},
            success: function (data) {
              var table;
              table = $('#issued-report-data').DataTable();
              table.clear();
              var indexNo = 1;
              $.each(data.issue_order_info, function(){
                    table.row.add([indexNo,this['ItemName'],this['OrderNumber'], this['SellerSKU'], this['Createdat']]);
                    indexNo++;
              });
              table.draw();

                $('#lbl_issuedCount').empty();
                $.each(data.issue_order_count, function(){
                  $('#lbl_issuedCount').append(this['issued_count']);
                });
            },
            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_issuedItems',
            type: 'GET',
            dataType: 'json',
            success: function(data) {
                if (data=="ok") {
                    fill_pending_tbData();
                    fill_issued_tbData();
                    //swal("Successfully!", "Size Added !", "success");
                    toastr["success"]("Issued Items Added!!");
                }else if (data=="error"){
                    //swal("Sorry","Error occured !!","error");
                    toastr["error"]("Sorry! Something Wrong.check stock!");
                }
            }
        });
    });
</script>
      <!-- Make sidebar menu hughlighter/selector -->
      <script>$(".<?php echo basename(__FILE__,'.php');?>-active-li").addClass("active");</script>
   </body>
</html>

