<? include "top.php"; ?>
<!DOCTYPE html>
<html>

<head>
   <?php include('include/head.php');
   //  $stdt=$_POST['stdt'];
   //  $enddt=$_POST['enddt'];
   $customer = 1;
   $name = mysqli_real_escape_string($con, $_POST['search']);
   $custid = mysqli_real_escape_string($con, $_POST['search']);
   $curdate = date("Y-m-d");

   ?>
   <title>Due Report Report on <?= $curdate ?></title>

   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

</head>

<body style="background-color: white; padding: 3%;">
   <div style="width:100%;padding:10px;text-align:right;">
      <a href="due_report_excel.php" class="btn btn-primary" target="_blank">Export To Excel</a>
      <button id="print" class="btn btn-primary" onclick="printContent('printoutdiv');">Print</button>

   </div>



   <div id="printoutdiv">
      <style type="text/css">
         th {
            font-size: 13px !important;
            padding: 5px;
         }

         td {
            font-size: 12px !important;
         }

         table {

            width: 100%;

         }

         tr {

            width: 100%;
         }

         thead>tr>td {
            background-color: #bad5e4;

         }

         .address-col {
            width: 200px;
            min-width: 200px;
            max-width: 200px;
            word-wrap: break-word;
            word-break: break-all;
            white-space: normal !important;
         }

         .vc-col {
            width: 120px;
            min-width: 120px;
            max-width: 120px;
            word-wrap: break-word;
            word-break: break-all;
            white-space: normal !important;
         }
      </style>
      <br>
      <center>
         <h4>Due report on <?= $curdate ?></h4>
      </center><br>
      <table class="stripe">
         <thead>
            <tr style="background-color: #bad5e4;">
               <th class="table-plus datatable-nosort">Sl. No</th>
               <th>Customer Name</th>
               <th>A/C No</th>
               <th class="address-col">Address</th>
               <th>Area</th>
               <? if ($customer == '1') { ?>
                  <th class="vc-col">VC NO</th>
                  <th>MSO no</th>
               <? } else { ?>
                  <th>IP</th>
                  <th>userid</th>
               <? } ?>
               <th>Bill Date</th>
               <th>Period</th>
               <th>Start Dt</th>
               <th>End Dt</th>
               <th>Bill Amt</th>
               <th>Status</th>
               <th>Paid(in Rs.)</th>
               <th>Payment Date</th>
               <th>Due(in Rs.)</th>
            </tr>
         </thead>
         <tbody>
            <?

            $getc = mysqli_query($con, "SELECT * FROM adduser");
            while ($arrcust = mysqli_fetch_array($getc)) {

               $row = mysqli_query($con, "SELECT * FROM `cbill` WHERE bl_cid='$arrcust[id]' ORDER BY blid DESC LIMIT 1");


               while ($arrc = mysqli_fetch_array($row)) {
                  $c++;

                  $total = floatval($total) + floatval($arrc['bl_amt']);
                  $paid = floatval($paid) + floatval($arrc['bl_payamt']);

                  $getcust = f(q("SELECT * FROM `adduser` WHERE id='$arrc[bl_cid]'"));
                  $billamt = floatval($arrc['bl_amt']) + floatval($arrc['bl_prevdue']);
                  $billdisc = $arrc['bl_disc'];

                  $bildue = abs(round(floatval($billamt) - (floatval($arrc['bl_payamt']) + floatval($billdisc))));
                  $dues = floatval($dues) + floatval($bildue);


                  ?>
                  <tr
                     style="border-bottom: 1px solid gray;<? if ($arrc['bl_stat'] == 0) { ?>background-color: #f1d4c4;<? } ?>">
                     <td class="table-plus" style="text-align: center;"><?= $c ?></td>
                     <td><?= $arrc['bl_cname'] ?></td>
                     <td><?= $getcust['custid'] ?></td>
                     <td class="address-col"><?= $getcust['address'] ?></td>
                     <td><?= $getcust['area_name'] ?></td>
                     <td class="vc-col"><?= $getcust['vc'] ?></td>
                     <td><?= $getcust['msoacno'] ?></td>
                     <td><?= $arrc['bl_dt'] ?></td>
                     <td><?= $arrc['bl_month'] ?>,<?= $arrc['bl_year'] ?></td>

                     <td><?= $arrc['bl_stdt'] ?></td>
                     <td><?= $arrc['bl_expiry'] ?></td>
                     <td><?= $billamt ?></td>
                     <td><? if ($arrc['bl_stat'] == 1) { ?>Paid<? } else { ?>Unpaid<? } ?></td>
                     <td><?= $arrc['bl_payamt'] ?></td>
                     <td><?= $arrc['bl_paydt'] ?></td>
                     <td><?= $bildue ?></td>
                  </tr>
               <? }
            } ?>
            <!-- <tfoot>
            <tr>
               <td class="table-plus"></td>
               <td></td>
               <td></td>
               <td></td>
               <td></td>
               <td></td>
               <td></td>
               
            </tr>
         </tfoot> -->
         </tbody>
      </table><br>
      <table>
         <tr>
            <td style="width: 65%;"></td>
            <td style="text-align:right;padding-right: 15px;"><b>Total Bill</b></td>
            <td>&#8377; <?= $total ?></td>
            <td></td>
         </tr>
         <tr>
            <td style="width: 65%;"></td>
            <td style="text-align:right;padding-right: 15px;"><b>Total Paid Amount</b></td>

            <td>&#8377; <?= $paid ?></td>
            <td></td>
         </tr>
         <tr>
            <td style="width: 65%;"></td>
            <td style="text-align:right;padding-right: 15px;"><b>Total Due Amount</b></td>

            <td>&#8377; <?= $dues ?></td>
            <td></td>
         </tr>

      </table>
      <br><br>
   </div>

   <?php //include('include/script.php'); ?>
   <script>
      function printContent(el) {
         var restorepage = $('body').html();
         var printcontent = $('#' + el).clone();
         $('body').empty().html(printcontent);
         window.print();
         $('body').html(restorepage);
      }
   </script>
</body>

</html>