<?php require_once('../auth/auth.php');?>
<?php
   $search     = $_GET['search'];
   $bbid       = $_GET['bbid'];
   $bbrid      = $_GET['bbrid'];
   $ins_type   = $_GET['ins_type'];
   $ins_acid   = $_GET['ins_acid'];
   $nrow       = $_GET['nrow'];
   $fdate      = $_GET['fdate'];
   $edate      = $_GET['edate'];
   $ins_status = $_GET['ins_status'];

   $co_rs = mysqli_query($conn,"SELECT co_name,owner_name,co_address,co_phone,co_email FROM company WHERE coid='$coid'") or die (mysqli_error($conn));
   $cor= mysqli_fetch_assoc($co_rs);

    if ($bbid=='0') {
       $bbidcond = '';
    }elseif ($bbid >0){
        $bbidcond= " AND intallment.bid=$bbid";
    }

    if ($bbrid=='0') {
       $bbridcond = '';
       $branch_name = 'All Branch';       
    }elseif ($bbrid >0){
        $bbridcond= " AND intallment.brid=$bbrid";
        $branch_name = mysqli_fetch_assoc(mysqli_query($conn,"SELECT branch_name FROM branch WHERE coid='$coid' AND brid='$bbrid'"))['branch_name'];
    }

    if ($ins_type=='0') {
       $ins_typecond = '';
    }elseif ($ins_type !=='0'){
       $ins_typecond= " AND intallment.ins_type='$ins_type' ";
    }

    if ($ins_acid=='0') {
       $ins_acidcond = '';
    }elseif ($ins_acid >0){
        $ins_acidcond= " AND intallment.acid=$ins_acid";
    }


        if ($ins_status=='0') {
       $ins_statuscond = '';
    }elseif ($ins_status == 'paid'){
        $ins_statuscond= " AND intallment.status='paid'  ";
    }elseif ($ins_status == 'unpaid'){
        $ins_statuscond= " AND intallment.status='unpaid' ";
    }elseif ($ins_status == 'partial'){
        $ins_statuscond= " AND intallment.due_ins > 0 ";
    }



    if ($search =='0') {
         $blacksearch='';
      } else {
         $blacksearch = " AND (intallment.reference_name LIKE '%$search%' || intallment.service_name LIKE '%$search%' || accounts.ac_name LIKE '%$search%' || accounts.ac_number LIKE '%$search%' || accounts.ac_alise LIKE '%$search%')";
    }


     $inv_query= mysqli_query($conn,"SELECT *,accounts.ac_name,accounts.acid,accounts.ac_alise,accounts.ac_number,accounts.balance,accounts.outlet_id,ins_chat.subdate,intallment.insid FROM (intallment
INNER JOIN accounts ON intallment.acid = accounts.acid
LEFT JOIN ins_chat ON intallment.insid = ins_chat.insid)

WHERE intallment.coid='$coid' $bbidcond $bbridcond $ins_typecond $ins_acidcond  $ins_statuscond  $blacksearch AND ins_date BETWEEN '$fdate'  AND '$edate' ORDER BY intallment.ins_date  ASC LIMIT $nrow;") or die (mysqli_error($conn));
     $dataa = [];
     while ($invrow= mysqli_fetch_assoc($inv_query)){ $dataa[]=$invrow;}
?>

 <table class="ttable border-bottom-0 d-none" id="header" cellpadding="0" cellspacing="0">
    <thead>
       <tr>
          <th rowspan="3" class="thborder border-bottom-0 text-center" style="width:10%;">
             <img class="profile-user-img img-fluid img-circle" src="../../dist/img/a.png" style="width:80px;height:77px;">
          </th>
          <th rowspan="3" class="thborder border-bottom-0" style="width:70%;">
                <dt style="font-size:18px;font-weight: 600;"><?php echo $cor['co_name'];?></dt>
                <dt><?php echo $cor['owner_name'];?></dt>
                <dt><?php echo $cor['co_address'];?></dt>
                <dt><?php echo $cor['co_phone'];?>#<?php echo $cor['co_email'];?></dt>
          </th>
          <th class="thborder text-center" style="width:20%;"><?php echo $branch_name;?></th>
       </tr>
       <tr>
          <th class="thborder text-center"><?php echo language(313);?></th>
       </tr>
       <tr>
          <th class="thborder text-center border-bottom-0"><?php echo date('d/m/y', strtotime($fdate));?> To <?php echo date('d/m/y', strtotime($edate));?>
          </th>
       </tr>
    </thead>
 </table>
<table id="pu_main_table" class="ttable" cellpadding="0" cellspacing="0">
<thead>
    <tr class="sticky-top">
        <th class="text-center" style="width:2%;"><?php echo language(15);?></th>
        <th title="Installment Date" class="text-center" style="width:7%;"><?php echo language(362);?></th>
        <th title="Next date " class="text-center" style="width:8%;"><?php echo language(366);?></th>
        <th title="service Name" class="text-center" style="width:5%;"><?php echo language(151);?></th>
        <th title="service Name" class="text-center" style="width:5%;"><?php echo language(353);?></th>
        <th title="Customer Name" class="text-center" style="width:8%;"><?php echo language(164);?></th>
        <th title="Outlet " class="text-center" style="width:7%;"><?php echo language(118);?></th>
        <th title="Guarantor Name" class="text-center" style="width:8%;"><?php echo language(354);?></th>
        <th title="Disburse date" class="text-center" style="width:8%;"><?php echo language(363);?></th>
        <th  title="INS Method" class="text-center" style="width:13%;"><?php echo language(349);?> </th>
        <th title="No. of ins" class="text-center" style="width:5%;"><?php echo language(360);?></th>
        <th title="Per Ins" class="text-center" style="width:5%;"><?php echo language(357);?></th>
        <th  title="Payment" class="text-center" style="width:5%;"><?php echo language(146);?></th>
        <th title="Due ins" class="text-center" style="width:5%;"><?php echo language(348);?></th>
        <th title="status" class="text-center" style="width:5%;"><?php echo language(90);?></th>
        <th title="Remark" class="text-center" style="width:5%;"><?php echo language(361);?></th>

    </tr>
</thead>
 <tbody id="ins_show"></tbody>
     <tfoot> 
        <tr class="sticky-bottom">
            <th class="text-right" colspan="13"><?php echo language(233);?></th>
                  <th class="text-right"><dt id="due_insdd"></dt></th>

    
        </tr>                                 
     </tfoot>   
</table>
<script id="invr_script" type="text/javascript">
   $(document).ready(function(){
       let dataa      = '';
       let i          = 0;
       let due_ins = 0;

       $.each(<?php echo json_encode($dataa);?>, function(key,value){

        due_ins      += Number(value.due_ins);

        
       dataa += '<tr>';    
       dataa += '<td class="text-center">'+ ++i +'</td>';
       dataa += '<td class="text-center">' + value.ins_date + '</td>';
       dataa += '<td class="text-center">' + value.subdate + '</td>';
       dataa += '<td class="text-center btn-outline-dark" ">' + (value.invoice).split("-")[5] + '</td>';
       dataa += '<td class="text-center">' + value.service_name + '</td>';
       dataa += '<td class="btn-outline-dark" onclick="acprofile('+ value.acid +')">'+ value.ac_name  +' - '+ value.ac_number +' </td>';
       dataa += '<td class="text-center">'+ value.outlet_id +'   </td>';
       dataa += '<td class="text-left">' + value.reference_name + ' - '+ value.reference_number +' </td>';
       dataa += '<td class="text-center"> '+ value.ins_active_date +' </td>';
       dataa += '<td class="text-center">  '+ value.in_type +'  ('+value.ins_type+')     </td>';
       dataa += '<td class="text-center">  '+ value.ins_qty +' / ' + value.ins_qty_total +  '</td>';
       dataa += '<td class="text-center">' + value.per_ins +  '</td>';

       if (value.payment >0) {
       dataa += '<td class="text-center"   onclick="vprintgg(this)" re_invoice="'+value.re_invoice+'">  ' + bden.format(value.payment) +' </td>';
       }else{
         dataa += '<td class="text-center">  ' + bden.format(value.payment) +' </td>';
       }

        if (value.due_ins > 0 ) {
           dataa += '<td class="text-center" style="background:#898636;color:white">' + value.due_ins + ' </td>';
        }else{
           dataa += '<td class="text-center">' + bden.format(Number(value.due_ins)) + '</td>';
        }

        if (value.status=='paid'){
           dataa += '<td class="text-center bg-danger" >' + value.status    + '  </td>';
        }else{
           dataa += '<td class="text-center"  onclick="vprint(this)" invoice="'+value.invoice+'" >' + value.status + ' </td>';
        }

       dataa += '<td class="text-center btn-outline-dark" onclick="ins_remark('+value.insid+')">'+  '<i class="fa fa-comments" ></i>'+' </td>';
       dataa += '</tr>';
   });
      $("#ins_show").html(dataa);
       $("#due_insdd").text(bden.format(due_ins));
      $("#invr_script").remove();
   });
</script>



<script>
  function vprint(invoiceid){
      $("#modal-lg").modal({backdrop: "static"});
      pagecall('../print_setup/op_ins_preview.php?ins_invoice='+ $(invoiceid).attr('invoice'),'#mdview');
  }
</script>
<script>
  function vprintgg(re_invoice){
      $("#modal-lg").modal({backdrop: "static"});
      pagecall('../print_setup/ri_ins_preview.php?ri_ins_invoice='+ $(re_invoice).attr('re_invoice'),'#mdview');
        }
</script>



<script type="text/javascript">
   function acprofile(acid){    
     $("#modal-xl").modal({backdrop: "static"});
     pagecall('../accounts/ac_profile.php?acid='+acid,'#xlmdview');   
   }  
</script>




<script>
 function ins_remark(insid){    
   $("#modal-lg").modal({backdrop: "static"});
    pagecall('../remark/ins_remark_index.php?insid='+insid,'#mdview');   
   } 
</script>

