<!DOCTYPE html>
<html>
<head>
<!-- TABLES CSS CODE -->
<?php include"comman/code_css_datatable.php"; ?>
<!-- bootstrap datepicker -->
<link rel="stylesheet" href="<?php echo $theme_link; ?>plugins/datepicker/datepicker3.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"; ?>

  <?php 
      /*Total Invoices*/
     
      /*Total Invoices Total*/
      $ins_income_total=$this->db->query("SELECT COALESCE(sum(income),0) AS tot_ins_income_total, COALESCE(sum(expence),0) AS tot_ins_expence_total, COALESCE(sum(income)-sum(expence),0) AS tot_ins FROM db_ins")->row()->tot_sal_grand_total;

      /*PAID AMOUNT*/
      $tot_received_amt=$this->db->select("COALESCE(SUM(paid_amount),0) AS paid_amount")->from("db_sales")->get()->row()->paid_amount;

      $sales_due_total=$this->db->query("SELECT COALESCE(SUM(sales_due),0) AS sales_due FROM db_customers")->row()->sales_due;
      //$sales_due_total = $sal_total - $sal_return_total;
     
  ?>
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        <?=$page_title;?>
        <small>View/Add Ins</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>

    <div class="pay_now_modal">
    </div>
    <div class="view_payments_modal">
    </div>

    <!-- 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">
              <h3 class="box-title"><?=$page_title;?></h3>
            </div>
            <!-- /.box-header -->
            <div class="box-body">
                <div class="row">
                    <div class="col-sm-4">
                        <div class="form-group">
                            <label for="tot_ins">Total Ins</label>
                            <?php
                                $query_tot_ins="SELECT COALESCE(sum(income),0) AS tot_ins_income_total, COALESCE(sum(expence),0) AS tot_ins_expence_total, COALESCE(sum(income)-sum(expence),0) AS tot_ins FROM db_ins";
                                $q_tot_ins=$this->db->query($query_tot_ins);
                                 foreach($q_tot_ins->result() as $res_tot_ins)
                                 { 
                                     echo "<input type='number' class='form-control' id='tot_ins' name='tot_ins' placeholder='' value='".$res_tot_ins->tot_ins."' >";
                                 } ?>
                        </div>
                        <div class="form-group">
                            <label for="redeem">Redeem</label>
                            <input type="number" class="form-control" id="redeem" name="redeem" placeholder="" value="" >
                        </div>
                        <!-- <div class="form-group">
                            <label for="category_id">Category <span class="text-danger">*</span></label>
                            <select class="form-control select2" id="category_id" name="category_id"  style="width: 100%;"  value="<?php // print $category_id; ?>"> -->
                                <?php /* 
                                   $query1="select * from db_category where status=1";
                                   $q1=$this->db->query($query1);
                                   if($q1->num_rows($q1)>0)
                                    {  echo '<option value="">-Select-</option>'; 
                                        foreach($q1->result() as $res1)
                                      { 
                                        $selected = ($category_id==$res1->id)? 'selected' : '';
                                        echo "<option $selected value='".$res1->id."'>".$res1->category_name."</option>";
                                      }
                                    }
                                    else
                                    {
                                      */ ?>
                                <!-- <option value="">No Records Found</option> -->
                                <?php //}?>
                            <!-- </select>
                        </div> -->
                        <!-- <div class="form-group">
                            <label for="cat_itm_qty">Qty</label>
                            <input type="number" class="form-control" id="cat_itm_qty" name="cat_itm_qty" placeholder="" value="" >
                        </div>
                        <div class="form-group">
                            <label for="redeem_Per_Item">Redeem Per Item</label>
                            <input type="number" class="form-control" id="redeem_Per_Item" name="redeem_Per_Item" placeholder="" value="" >
                        </div> -->
                        <div class="form-group">
                            <button type="button" class="btn btn-primary" id="ins_save" name="ins_save" style="float: right;">Redeem</button>
                        </div>
                        <br><br>   
                    </div>
                    <div class="col-sm-8">
                        <table id="example2" class="table table-bordered table-striped" width="100%">
                            <thead class="bg-primary ">
                                <tr>
                                    <th>#</th>                                    
                                    <th>Note</th>
                                    <th>Date</th>
                                    <th>Income</th>
                                    <th>Expences</th>
                                </tr>
                            </thead>
                            <tbody>
                            
                            </tbody>
                            <tfoot>
                                <tr class="bg-gray">
                                    <th colspan="3" style="text-align:right">Total</th><!-- 6 -->
                                    <?php
                                        $query_tot= "SELECT COALESCE(sum(income),0) AS tot_ins_income_total, COALESCE(sum(expence),0) AS tot_ins_expence_total, COALESCE(sum(income)-sum(expence),0) AS tot_ins FROM db_ins";
                                        $q_tot=$this->db->query($query_tot);
                                        foreach($q_tot->result() as $res_tot)
                                        { 
                                            echo "<th style='text-align:right;'>".$res_tot->tot_ins_income_total."</th><th style='text-align:right;'>".$res_tot->tot_ins_expence_total."</th>";
                                        } ?>
                                </tr>
                            </tfoot>
                        </table>
                    </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"; ?>
<!-- bootstrap datepicker -->
<script src="<?php echo $theme_link; ?>plugins/datepicker/bootstrap-datepicker.js"></script>

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

<script>
    $("#redeem").blur(function() {
        this.value = parseFloat(this.value).toFixed(2);
    });
</script>

<script type="text/javascript">
$(document).ready(function() {
    //datatables
   var table = $('#example2').DataTable({ 

      /* FOR EXPORT BUTTONS START*/
  dom:'<"row margin-bottom-12"<"col-sm-12"<"pull-left"l><"pull-right"fr><"pull-right margin-left-10 "B>>>tip',
 /* dom:'<"row"<"col-sm-12"<"pull-left"B><"pull-right">>> <"row margin-bottom-12"<"col-sm-12"<"pull-left"l><"pull-right"fr>>>tip',*/
      buttons: {
        buttons: [
            {
                className: 'btn bg-red color-palette btn-flat hidden delete_btn pull-left',
                text: 'Delete',
                action: function ( e, dt, node, config ) {
                    multi_delete();
                }
            },
            { extend: 'copy', className: 'btn bg-teal color-palette btn-flat',exportOptions: { columns: [0,1,2]} },
            { extend: 'excel', className: 'btn bg-teal color-palette btn-flat',exportOptions: { columns: [0,1,2]} },
            { extend: 'pdf', className: 'btn bg-teal color-palette btn-flat',exportOptions: { columns: [0,1,2]} },
            { extend: 'print', className: 'btn bg-teal color-palette btn-flat',exportOptions: { columns: [0,1,2]} },
            { extend: 'csv', className: 'btn bg-teal color-palette btn-flat',exportOptions: { columns: [0,1,2]} },
            { extend: 'colvis', className: 'btn bg-teal color-palette btn-flat',text:'Columns' },  

            ]
        },
        /* FOR EXPORT BUTTONS END */

        "processing": true, //Feature control the processing indicator.
        "serverSide": true, //Feature control DataTables' server-side processing mode.
        "order": [], //Initial no order.
        "responsive": true,
        language: {
            processing: '<div class="text-primary bg-primary" style="position: relative;z-index:100;overflow: visible;">Processing...</div>'
        },
        // Load data for the table's content from an Ajax source
        "ajax": {
            "url": "<?php echo site_url('sales/ins_ajax_list')?>",
            "type": "POST",
            
            complete: function (data) {
             /*$('.column_checkbox').iCheck({
                checkboxClass: 'icheckbox_square-orange',
                //uncheckedClass: 'bg-white',
                radioClass: 'iradio_square-orange',
                increaseArea: '10%' // optional
              });*/
             //call_code();
              //$(".delete_btn").hide();
             },

        },

        //Set column definition initialisation properties.
         "columnDefs": [
        // { 
        //     "targets": [ 2 ], //first column / numbering column
        //     "orderable": false, //set not orderable
        // },
        {
            "targets" :[2],
            "className": "text-center",
        },
        {
            "targets" :[3,4],
            "className": "text-right",
        },
        
        ],
    });
    new $.fn.dataTable.FixedHeader( table );
});
</script>

<script>
    $('#category_id').on('change',function(){
        var redeem = $('#redeem').val();
        var category_id = $('#category_id').val();
        $.ajax({
            url: $("#base_url").val()+'sales/get_cat_qty',
            type: 'POST',
            data:{category_id:category_id},
            dataType: 'json',
            success: function(data) {
            $.each(data.qty_info, function(){
                var itmQty =parseFloat(this['tot_qty']);
                $('#cat_itm_qty').val(itmQty);
                if(redeem==""||redeem=='0'){
                    toastr["error"]("Sorry! Please Enter Redeem Value !");
                }else{
                    var  redeem_per_itm = parseFloat(redeem)/itmQty;
                    $('#redeem_Per_Item').val(redeem_per_itm.toFixed(2));
                }
            });  
            }
        });
    });
</script>
<!-- <script>
    $('#redeem').on('change',function(){
        var redeem = $('#redeem').val();
        var category_id = $('#category_id').val();
        var cat_itm_qty = $('#cat_itm_qty').val();
        if(cat_itm_qty==""||cat_itm_qty=='0'){
            //toastr["error"]("Sorry! Please Enter Redeem Value !");
        }else{
            var itmQty =parseFloat(cat_itm_qty);
            $('#cat_itm_qty').val(itmQty);
            var  redeem_per_itm = parseFloat(redeem)/itmQty;
            $('#redeem_Per_Item').val(redeem_per_itm.toFixed(2));
        }
    });
</script> -->
<script>
    $('#ins_save').on('click',function(){
        var redeem = $('#redeem').val();
        var category_id = $('#category_id').val();
        var cat_itm_qty = $('#cat_itm_qty').val();
        var redeem_Per_Item = $('#redeem_Per_Item').val();

        $.ajax({
            url: $("#base_url").val()+'sales/save_new_ins',
            type: 'POST',
            data:{redeem:redeem}, //category_id:category_id,cat_itm_qty:cat_itm_qty,redeem_Per_Item:redeem_Per_Item
            dataType: 'json',
            success: function(data) {
                if (data=="success") {
                  //swal("Successfully!", "Size Added !", "success");
                  toastr["success"]("Ins Data Added!!");
                  //window.location=$("#base_url").val()+'sales/ins_view';//"items-view.php";
                }else if (data=="failed"){
                  //swal("Sorry","Error occured !!","error");
                  toastr["error"]("Sorry! Something Wrong.Try again!");
               }
            }
        });
    });
</script>
</body>
</html>
