 


 

	<p id="success"></p>
 
<div class="container-fluid">
	 <div class="col-lg-12 col-12 mt-3 d-flex align-items-center" style="flex-direction: row;justify-content: center;">
    <!-- Search Input -->
	<input type="text" id="search" class="form-control w-50" placeholder="Search Account..." onkeyup="handleSearch()">

    <!-- Toggle Switch -->
    <label class="form-check form-switch ms-3 mb-0">
        <input class="form-check-input" type="checkbox" id="toggleSwitch" checked>
        <label class="form-check-label" for="toggleSwitch">Switch View</label>
    </label>
</div>

   <section class="content">




<div class="container-fluid">
  


        <!-- Table Container (Initially Hidden) -->
        <div id="table-container" style="display: none;">
            <div class="col-lg-12 col-12 mt-3">
                <div class="small-box  shadow_block" style="height: auto; width: 100%;">
                    <div class="inner">
                        <h4>Manage <b>Accounts</b></h4>
                <table  style="font-size: 12px;" id="example1"  class="table table-striped table-bordered table-sm table-hover" cellspacing="0" width="100%">
			 
                <thead>
                    <tr  >
						<!--<th>
							<span class="custom-checkbox">
								<input type="checkbox" id="selectAll">
								<label for="selectAll"></label>
							</span>
						</th>  -->
				 
                 
						<th   width="2" >Account</th>	
                      <th width="15">Name</th>
					  <th width="10%">Water Bill</th> 
					 <!-- <th width="10">Bigas Bill</th> -->
					  <th width="10"> Meter Device </th>
					 
                                     
                       <th width="5%">Status</th>
                      <th width="20%" ></th>
                    </tr>
                </thead>
				<tbody>
				
				<?php
				$result = mysqli_query($conn,"SELECT * FROM tbl_user_account order by unique_id desc;");
					$i=1;
					while($justrow = mysqli_fetch_array($result)) {
				?>
				<tr 
				<?php

$unique_id=  $justrow['unique_id'] ;

$sql_cntallread= "SELECT count(*) as totalreadingcnt from tbl_members_report where account_no = '$unique_id'";
$result_cntallread= mysqli_query($conn, $sql_cntallread);
$row_totalcnt = mysqli_fetch_assoc($result_cntallread);
$totalreadingcount =  $row_totalcnt ["totalreadingcnt"];	
//4



$sql_cntallread2= "SELECT count(*) as totalpaymentmade FROM `tbl_payments` where unique_id = '$unique_id'";
$result_cntallread2= mysqli_query($conn, $sql_cntallread2);
$row_totalcnt2 = mysqli_fetch_assoc($result_cntallread2);
$totalpaymentmade =  $row_totalcnt2 ["totalpaymentmade"];	
//4 

if ($totalreadingcount >= 3) {

	//check if bayad n sya ng tatlo muna 
	if ($totalreadingcount  > $totalpaymentmade ) {
		
		$count_difference = $totalreadingcount -$totalpaymentmade;
			if ($count_difference == 1 ) {
			echo 'style="background: #fff;color: #000;';
			}elseif ($count_difference == 2 ) {
				echo 'style="background: #118134;color: #fff;';
			}elseif ($count_difference >= 3 ) {
				echo 'style="background: #c23d3d;color: #fff;';
		 
			}else{

			}

	//	echo $totalreadingcount .' - '. $totalpaymentmade;
	}else {
		//echo $totalreadingcount .' - '. $totalpaymentmade;
	}



} 
?>
id="<?php echo $justrow["id"]; ?>">
				
 
   <!--START OLD TABLE-->
   <td><?php echo $justrow['unique_id'] ?>
 </td>      
                      <td  >   <?php echo $justrow['acct_holder'] ?>   </td>
					  <td>  <?php

$unique_id=  $justrow['unique_id'] ;

//get bill payments  records
$sql_user_billrecord= "SELECT SUM(amount) as billspaidbalances FROM tbl_payments where unique_id = '$unique_id' and payment_type = 'WB'";
$result_user_billpay= mysqli_query($conn, $sql_user_billrecord);
$row_user_billpay = mysqli_fetch_assoc($result_user_billpay);

$sql_user_readings1 = "SELECT sum(billing_amount) as totalbills FROM tbl_members_report WHERE account_no = '$unique_id' order by reading_date desc";
$result_user_details2 = mysqli_query($conn, $sql_user_readings1);
$row_user_readings3 = mysqli_fetch_assoc($result_user_details2);
$wpblance = $row_user_readings3['totalbills'] ;

$sql_user_bklogs = "SELECT sum(amount) as bklogtotal FROM tbl_backlogs_collection where account_no = '$unique_id' ";
$result_bl_details2 = mysqli_query($conn, $sql_user_bklogs);
$row_user_bklogread = mysqli_fetch_assoc($result_bl_details2);
$bklog = $row_user_bklogread['bklogtotal'];


$remainingbalance = $wpblance  + $bklog;
$billspayment = $row_user_billpay['billspaidbalances'];

$totalunpaidbill = $remainingbalance - $billspayment;
if ($totalunpaidbill > 0) {

echo ' <b >&#8369;'.$totalunpaidbill.  '</b>';



}else {

 echo ' <b text-sucess >&#8369; 0.00 </b>';


}

					
					?> 
					 </td>

					 <!-- <td  >  
						<?php
						 //bigas computation
$unique_id=  $justrow['unique_id'] ;
$sql_sumorder= "SELECT sum(bigas_credit_amount) as billbigas FROM `tbl_bigasrecord`  where unique_id = '$unique_id' and transaction_type  = 'order'";
$result_sumorder = mysqli_query($conn, $sql_sumorder);
$row_orders = mysqli_fetch_assoc($result_sumorder);
$totalbigas_credit = $row_orders['billbigas'];

						?>
					 
   <b text-sucess >&#8369; <? echo $totalbigas_credit ; ?></b></td> -->
                  
                     <td>
						<!-- METER DEVICE BALANCE -->
						  <?php
						$unique_id=  $justrow['unique_id'] ;
						$sql_user_billrecord= "SELECT SUM(amount) as feepaidbalances FROM tbl_payments where payment_type = 'MD' and unique_id = '$unique_id'";
						$result_user_billpay= mysqli_query($conn, $sql_user_billrecord);
						$row_user_billpay_summary = mysqli_fetch_assoc($result_user_billpay);
						
						$sql_user_readings1 = "SELECT sum(fee) as totalfee FROM tbl_meterdevice where account_no = '$unique_id' ";
						$result_user_details2 = mysqli_query($conn, $sql_user_readings1);
						$row_user_readings_summary = mysqli_fetch_assoc($result_user_details2);
						
						$remainingbalance_summary = $row_user_readings_summary['totalfee'];
						$billspayment_summary = $row_user_billpay_summary['feepaidbalances'];
						
						$totalmeterfee = $remainingbalance_summary - $billspayment_summary;
						
								if ($totalmeterfee > 0) {
								
									echo ' <b >&#8369;'.$totalmeterfee.  '</b>';
								
								
									
								}else {
								
									echo ' <b text-sucess >&#8369; 0.00 </b>';
								
								
								}
														
						?>
					</td>
                     
                   
                   

                         <td>

                         <?php
                         $linestatus = $justrow['line_status'] ;
                         if ($linestatus == 0) {
 
 echo '<a href="upgrade.php?mid='.$justrow['id'].'&&pkg='.$justrow['package'].'" class="btn btn-outline-success btn-radius" style="color: #fff;border-color: #ffffff; background: #df2525;
    border-radius: 10px; font-weight: bold; font-size: 10px;    ">Disconnected</a>';



}elseif  ($linestatus== 1) {
  echo '<a href="#" class="btn btn-outline-success btn-radius" style="color: #fff;border-color: #ffffff; background: #03c500;
  border-radius: 10px; font-weight: bold; font-size: 10px;    ">Connected</a>';


}
  ?>
  </td>
                     
                      <td>
                     
 
                  
		 


					  <a href="#addmetertModal" class="addmeterreading btn  btn-outline-dark"
					     	data-accountno="<?php echo $justrow["unique_id"]; ?>"
							data-name="<?php echo $justrow["acct_holder"]; ?>"
							data-prevreading="<?php echo $justrow['pres_reading']; ?>"	
							data-wbbalancer="<?php
									 
								//get bill payments  records
								$sql_user_billrecord= "SELECT SUM(amount) as billspaidbalances FROM tbl_payments where unique_id = '$unique_id' and payment_type = 'WB'";
								$result_user_billpay= mysqli_query($conn, $sql_user_billrecord);
								$row_user_billpay = mysqli_fetch_assoc($result_user_billpay);

								$sql_user_readings1 = "SELECT sum(billing_amount) as totalbills FROM tbl_members_report WHERE account_no = '$unique_id' order by reading_date desc";
								$result_user_details2 = mysqli_query($conn, $sql_user_readings1);
								$row_user_readings3 = mysqli_fetch_assoc($result_user_details2);
								$wpblance = $row_user_readings3['totalbills'] ;

								//$sql_user_bklogs = "SELECT sum(amount) as bklogtotal FROM tbl_backlogs_collection where account_no = '$unique_id' ";
								//$result_bl_details2 = mysqli_query($conn, $sql_user_bklogs);
								//$row_user_bklogread = mysqli_fetch_assoc($result_bl_details2);
								//$bklog = $row_user_bklogread['bklogtotal'];


								$remainingbalance = $wpblance   ;
								$billspayment = $row_user_billpay['billspaidbalances'];

								
								$billspayment = $row_user_billpay['billspaidbalances'];

								$totalunpaidbill = $remainingbalance - $billspayment;
								if ($totalunpaidbill > 0) {
								
								echo $totalunpaidbill;


								
								}else {

								echo '0.00';


								}
								?>"

 							title="addmeterreading1"
							 data-toggle="modal"> 
							<i class="fas fa-tachometer-alt"data-toggle="tooltip"></i> 
						
					</a>  
	 
<!--WATER BILL START HERE-->
					 		 <a href="#addPaymentModal" class="addpayment btn  btn-outline-info" 
					  		data-id="<?php echo $justrow["unique_id"]; ?>"
							data-name="<?php echo $justrow["acct_holder"]; ?>"
								data-wbbalance="<?php
									 
								//get bill payments  records
								$sql_user_billrecord= "SELECT SUM(amount) as billspaidbalances FROM tbl_payments where unique_id = '$unique_id' and payment_type = 'WB'";
								$result_user_billpay= mysqli_query($conn, $sql_user_billrecord);
								$row_user_billpay = mysqli_fetch_assoc($result_user_billpay);

								$sql_user_readings1 = "SELECT sum(billing_amount) as totalbills FROM tbl_members_report WHERE account_no = '$unique_id' order by reading_date desc";
								$result_user_details2 = mysqli_query($conn, $sql_user_readings1);
								$row_user_readings3 = mysqli_fetch_assoc($result_user_details2);
								$wpblance = $row_user_readings3['totalbills'] ;

								//$sql_user_bklogs = "SELECT sum(amount) as bklogtotal FROM tbl_backlogs_collection where account_no = '$unique_id' ";
								//$result_bl_details2 = mysqli_query($conn, $sql_user_bklogs);
								//$row_user_bklogread = mysqli_fetch_assoc($result_bl_details2);
								//$bklog = $row_user_bklogread['bklogtotal'];


								$remainingbalance = $wpblance   ;
								$billspayment = $row_user_billpay['billspaidbalances'];

								
								$billspayment = $row_user_billpay['billspaidbalances'];

								$totalunpaidbill = $remainingbalance - $billspayment;
								if ($totalunpaidbill > 0) {
								
								echo 'Water: &#8369;'.$totalunpaidbill;


								
								}else {

								echo 'Water:  &#8369; 0.00';


								}
								?>"
						 	title="addpayment1" 
							data-toggle="modal"> 
							<i class="fas fa-tint"></i> 
						</a>  
      

							<!--WATER METER DEVICE PAYMENT-->
							
 
						<a href="#addDevicePaymentModal" class="addDevicePaymentModal btn  btn-outline-danger" 
					  		data-deviceaccountno="<?php echo $justrow["unique_id"]; ?>"
							data-devicename="<?php echo $justrow["acct_holder"]; ?>"
							data-devicebalance="
							<?php 
								$unique_id=  $justrow['unique_id'] ;
								$sql_user_billrecord= "SELECT SUM(amount) as feepaidbalances FROM tbl_payments where payment_type = 'MD' and unique_id = '$unique_id'";
						$result_user_billpay= mysqli_query($conn, $sql_user_billrecord);
						$row_user_billpay_summary = mysqli_fetch_assoc($result_user_billpay);
						
						$sql_user_readings1 = "SELECT sum(fee) as totalfee FROM tbl_meterdevice where account_no = '$unique_id' ";
						$result_user_details2 = mysqli_query($conn, $sql_user_readings1);
						$row_user_readings_summary = mysqli_fetch_assoc($result_user_details2);
						
						$remainingbalance_summary = $row_user_readings_summary['totalfee'];
						$billspayment_summary = $row_user_billpay_summary['feepaidbalances'];
						
						$totalmeterfee = $remainingbalance_summary - $billspayment_summary;
						
								if ($totalmeterfee > 0) {
								
									echo $totalmeterfee;
								
								
									
								}else {
								
									echo $totalmeterfee;
								
								
								}
									
								?>"
						 	title="addpayment1" 
							data-toggle="modal"> 
							<i class="fas fa-toolbox"></i>
						</a> 	
						
		
						<a href="#addBigasPayment" class="addBigasPayment btn btn-outline-warning" 
						data-bigasaccountno="<?php echo $justrow['unique_id']; ?>"
						data-bigasname="<?php echo $justrow['acct_holder']; ?>"
						data-bigasbalance="<?php 
							$unique_id = $justrow['unique_id'];
							$sql_user_billrecord = "SELECT SUM(amount) as billspaidbalances FROM tbl_bigasrecord WHERE unique_id = '$unique_id'";
							$result_user_billpay = mysqli_query($conn, $sql_user_billrecord);
							$row_user_billpay = mysqli_fetch_assoc($result_user_billpay);
							$billspayment = $row_user_billpay['billspaidbalances'] ?? 0;
							echo '₱' . number_format($billspayment, 2);
						?>"
						data-toggle="modal">
						<i class="fas fa-bold"></i>
</a>

</td>


                      <!--END OLD TABLE-->
					 
				</tr>
				<?php
				$i++;
				}
				?>
				</tbody>
			</table>
	 
        </div>
    </div>
	 <?php include 'add_devicepayment.php';?>
	<?php include 'add_meterreading.php';?> 
 <?php include 'add_billspayment.php';?>

 <?php include 'add_bigaspayment.php';?>

 </section><center>
<div class="col-lg-12 col-12 mt-3" id="mainmenus">
  <div class="container mt-4">
    <div class="row g-3"> <!-- Adjusted spacing between columns -->
      
      <div class="col-md-4 col-6 text-center">
        <a href="printlogs_reading.php" class="btn btn-primary btn-lg w-100 d-flex flex-column align-items-center py-3">
          <i class="fas fa-book fa-2x"></i>
          <span class="mt-2">Reading</span>
        </a>
      </div>

      <div class="col-md-4 col-6 text-center">
        <a href="printlogs.php" class="btn btn-success btn-lg w-100 d-flex flex-column align-items-center py-3">
          <i class="fas fa-credit-card fa-2x"></i>
          <span class="mt-2">Payments</span>
        </a>
      </div>

      <div class="col-md-4 col-6 text-center">
        <a href="sortreadings.php" class="btn btn-warning btn-lg w-100 d-flex flex-column align-items-center py-3">
          <i class="fas fa-history fa-2x"></i>
          <span class="mt-2">History</span>
        </a>
      </div>

      <div class="col-md-4 col-6 text-center">
        <button class="btn btn-danger btn-lg w-100 d-flex flex-column align-items-center py-3">
          <i class="fas fa-cog fa-2x"></i>
          <span class="mt-2">Settings</span>
        </button>
      </div>

      <div class="col-md-4 col-6 text-center">
        <a href="#" data-toggle="modal" data-target="#pinModal" class="btn btn-info btn-lg w-100 d-flex flex-column align-items-center py-3">
          <i class="fas fa-user-shield fa-2x"></i>
          <span class="mt-2">Admin</span>
        </a>
      </div>

      <div class="col-md-4 col-6 text-center">
        <a href="bigasallrecords.php" class="btn btn-primary btn-lg w-100 d-flex flex-column align-items-center py-3">
        
		  <i class="fas fa-concierge-bell fa-2x"></i>
          <span class="mt-2">Rice</span>
        </a>
      </div>


	 <!-- Branch Login Button -->
<div class="col-md-4 col-6 text-center">
 
    <a href="#" data-toggle="modal" data-target="#branchLoginModal" class="btn btn-info btn-lg w-100 d-flex flex-column align-items-center py-3">
          <i class="fas fa-sign-in-alt fa-2x"></i>
          <span class="mt-2">Branch Login</span>
        </a>


</div>


    </div>
  </div>
</div>

<br>
<!-- Button to trigger modal -->



</center>

		
		 
<script>
function handleSearch() {
    var input = document.getElementById("search").value.trim();
    var tableContainer = document.getElementById("table-container");
	var mainmenus = document.getElementById("mainmenus");

    // Show/hide table based on input length
    tableContainer.style.display = input.length > 0 ? "block" : "none";
	mainmenus.style.display = input.length > 0 ? "none" : "block";

    // Sync with DataTables search input
    let bootstrapSearch = document.querySelector("input[type='search'][aria-controls='example1']");
    if (bootstrapSearch) {
        bootstrapSearch.value = input;
        bootstrapSearch.dispatchEvent(new Event('input')); // Trigger filtering
    }

	  // Ensure the DataTable updates responsiveness after searching
	  let dataTable = $('#example1').DataTable();
    dataTable.columns.adjust().responsive.recalc();

}
</script>

<script>
    $(document).ready(function () {
        $("#toggleSwitch").change(function () {
            if ($(this).is(":checked")) {
                $("#mainmenus").show();
                $("#table-container").hide();
            } else {
                $("#mainmenus").hide();
                $("#table-container").show();
				  // Ensure the DataTable updates responsiveness after searching
	  let dataTable = $('#example1').DataTable();
    dataTable.columns.adjust().responsive.recalc();
	
            }
        });
    });
</script>
 