<!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 Fees</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="month" class=" control-label">Month</label>
                            <input type="month" name="month" id="month" class="form-control">
                        </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="0">-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="fees">Fees</label>
                            <input type="number" class="form-control" id="fees" name="fees" placeholder="" value="" >
                            <input type="hidden" class="form-control" name="qty_Item" id="qty_Item">
                        </div>
                        <div class="form-group">
                            <button type="button" class="btn btn-primary" id="fees_save" name="fees_save" style="float: right;">Add</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>Month</th>                                    
                                    <th>Category</th>
                                    <th>Fees</th>
                                    <th>Per unit fees</th>
                                </tr>
                            </thead>
                            <tbody>
                            
                            </tbody>
                        </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>
    $("#fees").blur(function() {
        this.value = parseFloat(this.value).toFixed(2);
    });
</script>

<script type="text/javascript">
     $('#example2').DataTable({
     });
</script>
<script type="text/javascript">
    $(document).ready(function() {
        fill_tbData();
    });
</script>
<script>
  function fill_tbData(){
    $.ajax({
        url:"<?php echo site_url('sales/fb_ajax_list')?>",
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var table;
            table = $('#example2').DataTable();
            table.clear();
            var indexNo = 1;
            if(data!='') {             
              $.each(data.fb_fees_info, function() {
                var s_fees =  parseFloat(this['fees']);
                table.row.add([indexNo,this['month'],this['category_name'],s_fees.toFixed(2),this['per_unit_fees']]);
                indexNo++;
              });
            }
            table.draw();
        },
        error: function (xhr, ajaxOptions, thrownError) {
          console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
 }
</script>
<script>
    $('#category_id').on('change',function(){
        var month = $('#month').val();
        var category_id = $('#category_id').val();
        if(month==""){
          toastr["error"]("Sorry! Please select month.Try again!");
          $('#category_id').val(null);
        }else{        
          $.ajax({
              url: $("#base_url").val()+'sales/get_sold_cat_qty',
              type: 'POST',
              data:{category_id:category_id,month:month},
              dataType: 'json',
              success: function(data) {
              $.each(data.qty_info, function(){
                  var itmQty =parseFloat(this['tot_qty']);
                  $('#qty_Item').val(itmQty);
              });  
              }
          });
      }
    });
</script>
<script>
    $('#month').on('change',function(){
        var month = $('#month').val();
        var category_id = $('#category_id').val();
        if(category_id==""){
        }else{        
          $.ajax({
              url: $("#base_url").val()+'sales/get_sold_cat_qty',
              type: 'POST',
              data:{category_id:category_id,month:month},
              dataType: 'json',
              success: function(data) {
              $.each(data.qty_info, function(){
                  var itmQty =parseFloat(this['tot_qty']);
                  $('#qty_Item').val(itmQty);
              });  
              }
          });
      }
    });
</script>
<script>
    $('#fees_save').on('click',function(){
        var fees = parseFloat($('#fees').val());
        var category_id = $('#category_id').val();
        var month = $('#month').val();
        var qty_Item = parseFloat($('#qty_Item').val());
        var per_unit = parseFloat(fees/qty_Item);

          //console.log(category_id+"   "+fees+"   "+month+"   "+per_unit+"   "+qty_Item);
        if(qty_Item==0){
          toastr["error"]("Sorry! Please select another category !");
        }else{
          $.ajax({
              url: $("#base_url").val()+'sales/save_new_fb_fees',
              type: 'POST',
              data:{category_id:category_id,fees:fees,month:month,per_unit:per_unit},
              dataType: 'json',
              success: function(data) {
                  if (data=="success") {
                      fill_tbData();
                    //swal("Successfully!", "Size Added !", "success");
                    toastr["success"]("Fees Data Added!!");
                  }else if (data=="failed"){
                    //swal("Sorry","Error occured !!","error");
                    toastr["error"]("Sorry! Something Wrong.Try again!");
                }
              }
          });
        }
    });
</script>
</body>
</html>
