<?php 
if( !hasPermission($_SESSION['user_detail']['role_id'], 'bill_list') ) {
  echo '<script>window.location.href="index.php?page=no_permission.php"</script>';
  die();
}

$bill_no     = "SELECT * FROM `billing` GROUP BY invoice_no";
$bill_no_res = mysqli_query($conn,$bill_no);
$bill_no = array();
while ($bill_no_row = mysqli_fetch_assoc($bill_no_res)) {
  $bill_no[] = $bill_no_row;
}
?>
<section class="content-header">
  <div class="row">
      <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 bottom20">
        <h3 class="capital font15 padding5 bold">VIEW BILL DETAILS</h3>
      </div>
      <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 text-right bottom20">
        <h3>
          <?php if( hasPermission($_SESSION['user_detail']['role_id'], 'bill_add') ) { ?>
          <!-- <button class="btn btn-danger capital bottom10 custome-btn" data-toggle="modal" data-target="#multiple-bill-print"><em class="fa fa-print"></em> multiple print</button> -->

          <a href="index.php?page=bill/add.php" class="btn btn-danger capital bottom10 custome-btn">Add New BILL</a>

        <?php } ?>
        </h3>
      </div>
  </div>
</section>
<hr class="bottom35"> 
<section class="hitz-top-header">
  <div class="card bottom40">
    <div class="card-body table-responsive">
      <table id="example1" class="table table-bordered table-striped list_data width100">
        <thead>
          <tr>
            <th width="50" class="text-center">SR NO.</th>
            <th class="text-center">PARTY</th>
            <th width="60" class="text-center">INVOICE</th>
            <th width="70" class="text-center">BILL DATE</th>
            <th width="80">NET AMOUNT</th>
            <th width="90">GROSS AMOUNT</th>
            <th width="90">CREATED BY</th>
            <th width="170">ACTION</th> 
          </tr>
        </thead>
        <tbody>
          <tr>
            <td colspan="8"><?php echo "NO RECORD FOUND"; ?></td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</section>
<div id="multiple-bill-print" class="modal fade">
  <div class="modal-dialog modal-sm">
    <div class="modal-content">
      <div class="modal-header multiple-bill-model-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">MULTIPLE BILL PRINT</h4>
      </div>
      <div class="modal-body">
        <form method="post" action="bill/multiple-bill.php" target="_blank">
          <div class="row">
            <div class="col-md-11" align="text-center">
              <label class="capital">START</label>
              <select class="form-control select2" name="start_invoice" id="start_invoice" style="width: 100%">
                <option value></option>
                <?php 
                  foreach ($bill_no as $key => $s_bill_no) { ?>
                    <option value="<?php echo $s_bill_no['invoice_no'];?>"><?php echo $s_bill_no['invoice_no'];?></option>
                  <?php }
                ?>
              </select>
            </div>
            <div class="col-md-11">
              <label class="capital">END</label>
              <select class="form-control select2" name="end_invoice" id="end_invoice" style="width: 100%">
                <option value></option>
                <?php 
                  foreach ($bill_no as $key => $e_bill_no) { ?>
                    <option value="<?php echo $e_bill_no['invoice_no'];?>"><?php echo $e_bill_no['invoice_no'];?></option>
                  <?php }
                ?>
              </select>
            </div>
            <div class="col-md-11"><br/>
              <label class="">
              <input type="checkbox" name="include_header" ata-checkbox="icheckbox_flat" class="icheck" id="header-checkbox" checked="" value="IH"> <span class="label-text">INCLUDE HEADER</span>
              </label>
            </div>
          </div><br/>
          <div class="row">
            <div class="col-md-10">
              <center><button class="btn btn-primary capital" type="submit"> PRINT</button></center>
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>
<div id="dataModal" class="modal fade">
  <div class="modal-dialog modal-md">
    <div class="modal-content">
       <div class="modal-body" id="employee_detail"></div>
       <div class="modal-footer hidden-print">  
          <!-- <label class="radio-inline fontradiobuttion">
          <input type="checkbox" ata-checkbox="icheckbox_flat" class="icheck" id="header-checkbox" checked=""> <span class="label-text">INCLUDE HEADER</span>
          </label>
          <button type="button" onclick="window.print();" class="btn btn-primary"><i class="fa fa-print" aria-hidden="true"></i></button>  -->
          <button type="button" class="btn btn-danger" data-dismiss="modal"><i class="fa fa-times-circle-o" aria-hidden="true"></i></button>
       </div>
    </div>
  </div>
</div>
<script type="text/javascript">
jQuery(document).ready(function() {
  $('#start_invoice').select2();
  $('#end_invoice').select2();
  loadDataTable();
});

function loadDataTable(){
  if(typeof dTable!='undefined'){dTable.fnDestroy();}

  dTable=jQuery('.list_data').dataTable({
      "bProcessing"       : true,
      "order"             : [ [2, 'desc'] ],
      "aLengthMenu"       : [ [5, 10, 15, 20, 25, 50, 100], [5, 10, 15, 20, 25, 50, 100] ],
      'iDisplayLength'    : 50,
      "aoColumns"         : [
          {"sClass": "text-center"}, {"sClass": "text-center"}, {"sClass": "text-center"}, {"sClass": "text-center"}, {"sClass": "text-center"}, {"sClass": "text-center"}, {"sClass": "text-center"}, {"sClass": "text-center"}
      ],
      "bServerSide"       : true,
      "sAjaxSource"       : 'bill/fetch.php',
  });
}

function confirmDelete(e) {
  var $i = $(e).attr("id");
  console.log($i);
  swal({
      title: "Are you sure?",
      text: "You will not be able to recover this imaginary file!",
      type: "warning",
      showCancelButton: true,
      confirmButtonColor: "#DD6B55",
      confirmButtonText: "Yes, delete it!",
      closeOnConfirm: false
  }, function (isConfirm) {
      if (!isConfirm) return;
      $.ajax({
          url: "bill/delete.php",
          type: "POST",
          data: 'delete='+$i,
          dataType: "json",
          success: function () {
            swal({title: "Deleted!", text: "Data successfully Deleted!", type: "success"},
              function(){ 
                location.reload();
              }
            );
          },

          error: function (xhr, ajaxOptions, thrownError) {
            swal("Error deleting!", "Please try again", "error");
          }
      });
  });
}

function BillCopyPrint(e){
  var $i = $(e).attr("id");
  $.ajax({  
    url:"bill/bill_copy_print.php",  
    method:"post",  
    data:{id:$i},  
    success:function(data){  
      $('#bill_print').html(data);  
      $('#bill_print_model').modal("show");  
    }  
  });
}

function CancleFlag(e) {
  var $i = $(e).attr("id");
  swal({
      title: "Are you sure?",
      text: "You will not be able to recover this imaginary file!",
      type: "warning",
      showCancelButton: true,
      confirmButtonColor: "#DD6B55",
      confirmButtonText: "Yes, Cancel it!",
      closeOnConfirm: false
  }, function (isConfirm) {
      if (!isConfirm) return;
      $.ajax({
          url: "bill/cancel_flag.php",
          type: "POST",
          data: 'delete='+$i,
          dataType: "json",
          Error: function () {
            swal("Error canceling!", "Please try again", "error");
          },

          success: function (xhr, ajaxOptions, thrownError) {
            swal({title: "Canceled!", text: "Data successfully Canceled!", type: "success"},
              function(){ 
                location.reload();
              }
            );
          }
      });
  });
}

function billPrint(e){
  var $i = $(e).attr("id");
  $.ajax({  

    url:"bill/print_modal.php",  
    method:"post",  
    data:{id:$i},  
    success:function(data){  
      jQuery('#bill_print').html(data);  
      jQuery('#bill_print_model').modal("show");
    }  
  });
}
</script>
