<!DOCTYPE html>
<html>

<head>
<!-- TABLES CSS CODE -->
<?php include"comman/code_css_datatable.php"; ?>

<!-- Lightbox -->
<link rel="stylesheet" href="<?php echo $theme_link; ?>plugins/lightbox/ekko-lightbox.css">
</head>

<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">

  <!-- Left side column. contains the logo and sidebar -->
  
  <?php include"sidebar.php"; ?>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        <?=$page_title;?>
      </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 -->
    <?= form_open('#', array('class' => '', 'id' => 'table_form')); ?>
    <input type="hidden" id='base_url' value="<?=$base_url;?>">

    <section class="content">
      <div class="row">
        <!-- ********** ALERT MESSAGE START******* -->
        <?php include"comman/code_flashdata.php"; ?>
        <!-- ********** ALERT MESSAGE END******* -->
        <div class="col-xs-12">
          <div class="box">
            <div class="box-header with-border">
                <div class="row">
                  <div class="col-md-12">                    
                    <div class="col-md-2">
                        <label for="from_date" class="control-label">From Date</label>
                          <input type="date" name="from_date" id="from_date" class="form-control" value="<?php $date = new DateTime("now", new DateTimeZone('Asia/Colombo') ); echo $date->format('Y-m-d');?>">
                    </div>
                    <div class="col-md-2">
                        <label for="to_date" class=" control-label">To Date</label>
                          <input type="date" name="to_date" id="to_date" class="form-control" value="<?php $date = new DateTime("now", new DateTimeZone('Asia/Colombo') ); echo $date->format('Y-m-d');?>">
                    </div>
                    <div class="col-md-2">
                        <br>
                        <button type="button" class="btn btn-success" id="btn_show"> Show</button>
                    </div>
                    <div class="col-md-2">
                        <label for="order_number" class=" control-label">Order Number</label>
                        <input type="text" class="form-control" name="order_number" id="order_number">
                    </div>
                    <div class="col-md-4">
                        <br>
                        <button type="button" class="btn btn-success" id="btn_load_all"> Load All</button>
                        <button type="button" class="btn btn-danger" id="btn_load_return"> Load Return</button>
                    </div>
                    
                  </div>
              </div>
            </div>
            <!-- /.box-header -->
            <div class="box-body">
              <table id="example2" class="table table-bordered table-striped" width="100%">
                <thead class="bg-primary ">
                    <tr>
                    <th class="text-center">#</th>
                    <th>Order No.</th>
                    <th>SKU</th>
                    <th>Item Name</th>
                    <th>Color</th>
                    <th>Size</th>
                    <th>Status</th>
                    <th>Order Date</th>
                    <th>Payment</th>
                    <?php if($this->session->userdata('role_id')=='1'){ ?>
                    <th>Profit</th>
                    <?php } ?>
                    </tr>
                </thead>
                <tbody>
                    
                </tbody>
                <tfoot>
                    <tr class="bg-gray">
                        <th colspan="8" style="text-align:right">Total</th><!-- 6 -->
                        <th><b> <label id="lbl_sum_payments"></label> </b></th><!-- 8 -->
                        <?php if($this->session->userdata('role_id')=='1'){ ?>
                        <th><b> <label id="lbl_sum_profit"></label> </b></th><!-- 8 -->
                        <?php } ?>
                    </tr>
                </tfoot>
              </table>

              <br>

                <div class="row">
                    <div class="col-md-12">
                        <button type="button" class="btn btn-info" id="return_received" style="float: right;">Mark As Return Received </button>
                    </div>
                </div>

            </div>

            
            <!-- /.box-body -->
          </div>
          <!-- /.box -->
        </div>
        <!-- /.col -->
      </div>
      <!-- /.row -->
    </section>
    <!-- /.content -->
     <?= form_close();?>
  </div>
  <!-- /.content-wrapper -->
  <?php include"footer.php"; ?>
  <!-- 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 include"comman/code_js_sound.php"; ?>
<!-- TABLES CODE -->
<?php include"comman/code_js_datatable.php"; ?>
<!-- Lightbox -->
<script src="<?php echo $theme_link; ?>plugins/lightbox/ekko-lightbox.js"></script>
<script type="text/javascript">
  $(document).on('click', '[data-toggle="lightbox"]', function(event) {
            event.preventDefault();
            $(this).ekkoLightbox();
        });
</script>
<script>
    $(document).ready(function() {
        $("[data-checkboxes]").each(function () {
        var me = $(this),
          group = me.data('checkboxes'),
          role = me.data('checkbox-role');

        me.change(function () {
          var all = $('[data-checkboxes="' + group + '"]:not([data-checkbox-role="dad"])'),
            checked = $('[data-checkboxes="' + group + '"]:not([data-checkbox-role="dad"]):checked'),
            dad = $('[data-checkboxes="' + group + '"][data-checkbox-role="dad"]'),
            total = all.length,
            checked_length = checked.length;

          if (role == 'dad') {
            if (me.is(':checked')) {
              all.prop('checked', true);
              calAmount(this);
            } else {
              all.prop('checked', false);
              calAmount(this);
            }
          } else {
            if (checked_length >= total) {
              dad.prop('checked', true);
            } else {
              dad.prop('checked', false);
            }
          }
        });
      });
    });
</script>


<!-- <script src="<?php echo $theme_link; ?>js/items.js"></script> -->

<script>
    $('#btn_load_all').on('click',function(){
      fill_all_tbData();
    });
</script>

<script>
    $('#btn_show').on('click',function(){
        fill_tbData();
    });
</script>

<script>
    $('#btn_load_return').on('click',function(){
      fill_return_tbData();
    });
</script>

<script>
  function fill_all_tbData(){

      $.ajax({
        url:"<?php echo site_url('reports/load_all_details_get')?>",
        type: 'GET',
        dataType: 'json',
        success: function (data) {

          var table;
          table = $('#example2').DataTable();
          table.clear(); 

          <?php if($this->session->userdata('role_id')=='1'){ ?>
            var indexNo = 1;           
            var payment_sum = 0;
            var profit_sum = 0;

            $.each(data.ord_info, function(){
              var s_payment =parseFloat(this['Paid']);
              var s_profit =parseFloat(this['profitNew']);
              var s_id = this['id'];
              table.row.add(['<div class="custom-checkbox custom-control"><input type="checkbox" data-checkboxes="mygroup" class="custom-control-input orderNo"  name="orderNo" id="checkbox-'+s_id+'" value="'+s_id+'" data-OrderNumber="'+this['OrderNumber']+'" data-SellerSKU="'+this['SellerSKU']+'"><label for="checkbox-'+s_id+'" class="custom-control-label">&nbsp;</label></div>',this['OrderNumber'],this['SellerSKU'], this['item_name'], this['item_colour'], this['item_size'], this['issueStatus'], this['Createdat'],this['Paid'], this['profitNew']]);
              indexNo++;
              payment_sum  += s_payment;
              profit_sum  += s_profit;
            });
            table.draw();
            $('#lbl_sum_payments').empty();
            $('#lbl_sum_payments').append(payment_sum.toFixed(2));
            $('#lbl_sum_profit').empty();
            $('#lbl_sum_profit').append(profit_sum.toFixed(2));

          <?php } else if($this->session->userdata('role_id')!='1'){ ?>
            var indexNo = 1;           
            var payment_sum = 0;
            var profit_sum = 0;

            $.each(data.ord_info, function(){
              var s_payment =parseFloat(this['Paid']);
              var s_profit =parseFloat(this['profitNew']);
              var s_id = this['id'];
              table.row.add(['<div class="custom-checkbox custom-control"><input type="checkbox" data-checkboxes="mygroup" class="custom-control-input orderNo"  name="orderNo" id="checkbox-'+s_id+'" value="'+s_id+'" data-OrderNumber="'+this['OrderNumber']+'" data-SellerSKU="'+this['SellerSKU']+'"><label for="checkbox-'+s_id+'" class="custom-control-label">&nbsp;</label></div>',this['OrderNumber'],this['SellerSKU'], this['item_name'], this['item_colour'], this['item_size'], this['issueStatus'], this['Createdat'],this['Paid']]);
              indexNo++;
              payment_sum  += s_payment;
              profit_sum  += s_profit;
            });
            table.draw();
            $('#lbl_sum_payments').empty();
            $('#lbl_sum_payments').append(payment_sum.toFixed(2));
            // $('#lbl_sum_profit').empty();
            // $('#lbl_sum_profit').append(profit_sum.toFixed(2));
          <?php } ?>
        },
        error: function (xhr, ajaxOptions, thrownError) {
          console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
      });
  }
</script> 

<script>
  function fill_tbData(){
    //   var payment_type = $('#payment_type').val();
    //   var agent_id = $('#agent_id').val();
      var to_date    = $('#to_date').val();
      var from_date    = $('#from_date').val();
    //   var platform_id   = $('#platform_id').val();

      //alert(fromDate+"           "+toDate);
      $.ajax({
        url:"<?php echo site_url('reports/order_revenue_load_details_get')?>",
        type: 'post',
        data: {to_date:to_date,from_date:from_date},
        dataType: 'json',
        success: function (data) {

          var table;
          table = $('#example2').DataTable();
          table.clear(); 

        //   $.each(data.data_count_info, function(){
            // var dataCount =parseInt(this['d_count']);
            // //console.log(dataCount);
            // if(dataCount>0) { 
               var indexNo = 1;           
               var payment_sum = 0;
               var profit_sum = 0;

               $.each(data.ord_info, function(){
                    var s_payment =parseFloat(this['Paid']);
                    var s_profit =parseFloat(this['profitNew']);

                    var s_id = this['id'];
                    // var itm_s_id = this['itm_s_id'];
                    // var s_waybill = this['waybill'];
                    // var s_agent_id = this['agent_id'];
                    // var s_TotalInvoiceAmount = this['TotalInvoiceAmount'];

                    table.row.add(['<div class="custom-checkbox custom-control"><input type="checkbox" data-checkboxes="mygroup" class="custom-control-input orderNo"  name="orderNo" id="checkbox-'+s_id+'" value="'+s_id+'" data-OrderNumber="'+this['OrderNumber']+'" data-SellerSKU="'+this['SellerSKU']+'"><label for="checkbox-'+s_id+'" class="custom-control-label">&nbsp;</label></div>',this['OrderNumber'],this['SellerSKU'], this['item_name'], this['item_colour'], this['item_size'], this['issueStatus'], this['Createdat'],this['Paid'], this['profitNew']]);
                    indexNo++;
                    payment_sum  += s_payment;
                    profit_sum  += s_profit;
               });
               table.draw();
               $('#lbl_sum_payments').empty();
               $('#lbl_sum_payments').append(payment_sum.toFixed(2));
               $('#lbl_sum_profit').empty();
               $('#lbl_sum_profit').append(profit_sum.toFixed(2));
        //     }else {
        //         $('#example2').DataTable().destroy();
        //        toastr["warning"]("Something Wrong! , Data Not Found. ");
        //     }
        //  });
          
        },
        error: function (xhr, ajaxOptions, thrownError) {
          console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
      });
  }
</script> 

<script>
  function fill_return_tbData(){
    //   var payment_type = $('#payment_type').val();
    //   var agent_id = $('#agent_id').val();
      var to_date    = $('#to_date').val();
      var from_date    = $('#from_date').val();
    //   var platform_id   = $('#platform_id').val();

      //alert(fromDate+"           "+toDate);
      $.ajax({
        url:"<?php echo site_url('reports/order_revenue_return_details_get')?>",
        type: 'post',
        data: {to_date:to_date,from_date:from_date},
        dataType: 'json',
        success: function (data) {

          var table;
          table = $('#example2').DataTable();
          table.clear(); 

        //   $.each(data.data_count_info, function(){
            // var dataCount =parseInt(this['d_count']);
            // //console.log(dataCount);
            // if(dataCount>0) { 
               var indexNo = 1;           
               var payment_sum = 0;
               var profit_sum = 0;

               $.each(data.ord_info, function(){
                    var s_payment =parseFloat(this['Paid']);
                    var s_profit =parseFloat(this['profitNew']);

                    var s_id = this['id'];
                    // var itm_s_id = this['itm_s_id'];
                    // var s_waybill = this['waybill'];
                    // var s_agent_id = this['agent_id'];
                    // var s_TotalInvoiceAmount = this['TotalInvoiceAmount'];

                    table.row.add(['<div class="custom-checkbox custom-control"><input type="checkbox" data-checkboxes="mygroup" class="custom-control-input orderNo"  name="orderNo" id="checkbox-'+s_id+'" value="'+s_id+'" data-OrderNumber="'+this['OrderNumber']+'" data-SellerSKU="'+this['SellerSKU']+'"><label for="checkbox-'+s_id+'" class="custom-control-label">&nbsp;</label></div>',this['OrderNumber'],this['SellerSKU'], this['item_name'], this['item_colour'], this['item_size'], this['issueStatus'], this['Createdat'],this['Paid'], this['profitNew']]);
                    indexNo++;
                    payment_sum  += s_payment;
                    profit_sum  += s_profit;
               });
               table.draw();
               $('#lbl_sum_payments').empty();
               $('#lbl_sum_payments').append(payment_sum.toFixed(2));
               $('#lbl_sum_profit').empty();
               $('#lbl_sum_profit').append(profit_sum.toFixed(2));
        //     }else {
        //         $('#example2').DataTable().destroy();
        //        toastr["warning"]("Something Wrong! , Data Not Found. ");
        //     }
        //  });
          
        },
        error: function (xhr, ajaxOptions, thrownError) {
          console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
      });
  }
</script> 

<script>
    $(document).ready(function(){
      fill_tbData();
    });
</script>
<!-- <script>
    $('#to_date').on('change',function(){
      fill_tbData();
    });
</script>
<script>
    $('#from_date').on('change',function(){
      fill_tbData();
    });
</script> -->
<script>
  // $("#order_number").on("",function(){
  //   fill_order_number_data();
  // });

  $('#order_number').keypress(function(event){
    var keycode = (event.keyCode ? event.keyCode : event.which);
    if(keycode == '13'){
      fill_order_number_data();
    }
  });
</script>
<script>
    function fill_order_number_data(){
      var input_text = $("#order_number").val().trim();
      //alert("asdfger");

      $.ajax({
        url:"<?php echo site_url('reports/order_number_details_get')?>",
        type: 'post',
        data: {input_text:input_text},
        dataType: 'json',
        success: function (data) {

          var table;
          table = $('#example2').DataTable();
          table.clear(); 

        //   $.each(data.data_count_info, function(){
            // var dataCount =parseInt(this['d_count']);
            // //console.log(dataCount);
            // if(dataCount>0) { 
               var indexNo = 1;           
               var payment_sum = 0;
               var profit_sum = 0;

               $.each(data.ord_info, function(){
                    var s_payment =parseFloat(this['Paid']);
                    var s_profit =parseFloat(this['profitNew']);

                    var s_id = this['id'];
                    // var itm_s_id = this['itm_s_id'];
                    // var s_waybill = this['waybill'];
                    // var s_agent_id = this['agent_id'];
                    // var s_TotalInvoiceAmount = this['TotalInvoiceAmount'];

                    table.row.add(['<div class="custom-checkbox custom-control"><input type="checkbox" data-checkboxes="mygroup" class="custom-control-input orderNo"  name="orderNo" id="checkbox-'+s_id+'" value="'+s_id+'" data-OrderNumber="'+this['OrderNumber']+'" data-SellerSKU="'+this['SellerSKU']+'"><label for="checkbox-'+s_id+'" class="custom-control-label">&nbsp;</label></div>',this['OrderNumber'],this['SellerSKU'], this['item_name'], this['item_colour'], this['item_size'], this['issueStatus'], this['Createdat'],this['Paid'], this['profitNew']]);
                    indexNo++;
                    payment_sum  += s_payment;
                    profit_sum  += s_profit;
               });
               table.draw();
               $('#lbl_sum_payments').empty();
               $('#lbl_sum_payments').append(payment_sum.toFixed(2));
               $('#lbl_sum_profit').empty();
               $('#lbl_sum_profit').append(profit_sum.toFixed(2));
        //     }else {
        //         $('#example2').DataTable().destroy();
        //        toastr["warning"]("Something Wrong! , Data Not Found. ");
        //     }
        //  });
          
        },
        error: function (xhr, ajaxOptions, thrownError) {
          console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
      });
    }
</script>
<script>
    $('#return_received').on('click',function(){
        var sale_itemSet = [];
        var data_table =[];
        $.each($("input[name='orderNo']:checked"), function(){
            sale_itemSet.push($(this).val());
            //$(this).attr('data-ID')
            var record = {
                'order_no' :  $(this).attr('data-OrderNumber'),
                'sale_sku' :  $(this).attr('data-SellerSKU'),
			};

            data_table.push(record);
        });

        var all_data ={'data_table':data_table,'sale_itemSet':sale_itemSet};

        var sl_len= sale_itemSet.length;
        if(sl_len>0){
        
            //console.log(data_table);
            $.ajax({
                url:"<?php echo site_url('daraz_order/return_update')?>",
                type: 'post',
                data: all_data,
                dataType: 'json',
                success: function (data) {
                  //console.log(data);
                    if(data=='success'){
                        fill_tbData();
                        toastr["success"]("Selected Item Return status changed and stock adjusted!");       
                    }else if(data=='failed'){
                        toastr["error"]("Sorry! Failed to Return.Try again!");
                    } 
                },
                error: function (xhr, ajaxOptions, thrownError) {
                console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                }
            });
        }else{
            toastr["warning"]("Something Wrong! , Please select order. ");
        }
    });
</script>

<!-- Make sidebar menu hughlighter/selector -->
<script>$(".<?php echo basename(__FILE__,'.php');?>-active-li").addClass("active");</script>
		
</body>
</html>
