<?php require_once('../auth/auth.php');?>
<div class="col-md-12">
    <div class="card-body table-responsive cscroll" style="max-height: 450px;">
        <table class="ttable" cellpadding="0" cellspacing="0">
            <thead>
                <tr class="sticky-top">
                    <th class="text-center" style="width:%;"><?php echo language(15); ?></th>
                    <th class="text-center" style="width:%;"><?php echo language(207); ?> </th>
                    <th class="text-center" style="width:%;"><?php echo language(353); ?> </th>
                    <th class="text-center" style="width:%;"><?php echo language(357); ?></th>
                    <th class="text-center" style="width:%;"> <?php echo language(146); ?> </th>
                    <th class="text-center" style="width:%;"><?php echo language(348); ?></th>
                    <th class="text-center" style="width:%;"><?php echo language(349); ?></th>
                    <th class="text-center" style="width:%;"><?php echo language(131); ?></th>
                </tr>
            </thead>
            <tbody id="row_show<?php echo $invoice;?>">
                
                <?php
                    $invoice = $_GET['ins_invoice'];
                    $cr_acid = $_GET['cr_acid'];
                    $accat= mysqli_query($conn,"SELECT *,accounts.ac_name,accounts.acid,accounts.ac_alise,accounts.ac_number,accounts.balance FROM (intallment
                    INNER JOIN accounts ON intallment.acid = accounts.acid)
                    WHERE intallment.coid='$coid' AND intallment.bid='$bid' AND intallment.brid='$brid' AND intallment.acid='$cr_acid' AND intallment.status='unpaid' ORDER BY intallment.ins_date  ") or die(mysqli_error($conn));
                    $i=0;
                    while($orw= mysqli_fetch_assoc($accat)){
                ?>
                <tr>
                    <td class="text-center"><?php echo ++$i;?></td>
                    <td class="text-center"> <?php if ($orw['in_type']=='OPINS') { echo language(49);}else{echo language(43);} ?> </td>
                    <td class="text-center"> <?php echo $orw['service_name'];?> </td>



                    <td class="text-center btn-outline-dark"> <dt onclick="$(this).parent().parent().find('.payment').val(<?php echo $orw['due_ins'];?>);input_cal(this);"> <?php echo $orw['due_ins'];?> </dt></td>

                    <td>
                        <input type="hidden" name="ins_id[]"  value="<?php echo $orw['insid'];?>"  >
                        <input type="number" name="ins_payment[]" oninput="input_cal(this)" max="<?php echo $orw['due_ins'];?>" class="form-control text-center form-control-sm inputselect payment"  value="0" required="true" style="color: white;background: maroon;" >
                    </td>

                    <td class="text-center"> <dt class="due_ins"> <?php echo $orw['due_ins'];?> </dt></td>
                    <td class="text-center"> <dt> <?php echo $orw['ins_type'];?> </dt></td>
                    <?php  if ($orw["ins_date"]==$date) {?>
                    <td class="text-center text-white bg-danger"> <?php echo $orw["ins_date"]; ?>  </td>
                    <?php }else { ?>
                    <td class="text-center"> <?php echo $orw["ins_date"]; ?>  </td>
                    <?php } ?>
                    
                </tr>
                <?php } ?>
            </tbody>
        </table>
    </div>
</div>
<script>
$('select').select2();
function input_cal(abc) {
let targt      = $(abc).parent().parent();
let per_ins    = Number(targt.find('.payment').attr("max"));
let payment    = Number(targt.find('.payment').val());

let ins_discount    = Number($('#ins_discount').val());
targt.find('.due_ins').text(per_ins-payment);
let  paysum=0;
$(".payment").each(function(){
paysum += +Number($(this).val());
});
$('#instotal').text(paysum);
$('#payable').text(paysum-ins_discount);

}
</script>