<script>
    if (<?php echo json_encode($hidestatus); ?> == 0) {
        document.querySelectorAll('.hidebutton').forEach(div => div.setAttribute('style', 'display:none;'));
    }
</script>



<p id="success"></p>
<section class="content">
  <div class="container-fluid">
    <div class="col-lg-12 col-12">
      <div class="small-box bg-secondry shadow_block" style="height: auto; width: 100%;">
        <div class="inner">
          <h4 style="mx-auto">
            
            Today <b>Payment Collections</b> &#8369;<?php echo number_format(($todaypayreceived)); ?>
          </h4>

          <table id="payment_list_table" class="table table-striped table-bordered table-sm" cellspacing="0" width="100%">
            <thead>
              <tr>
                <th>Ref #:  </th>
                <th width="20%">Name</th>
                <th>Accnt ID</th>
                <th>Type</th>
                <th>Amount Paid</th>
                <th>Payment Date</th>
                <th>Reading Date</th>
                 <th>Encoded</th>
              
                <th width="10%"></th>
                <th width="5%"></th>
                            </tr>
            </thead>
            <tbody>
              <?php
      
              
      $result = mysqli_query($conn, 
      "SELECT 
          tbl_user_account.unique_id, 
          tbl_user_account.acct_holder, 
          tbl_user_account.firstname, 
          tbl_user_account.lastname, 
          tbl_user_account.pres_reading, 
          tbl_payments.referrence_code, 
          tbl_payments.payment_type, 
          tbl_payments.amount, 
          tbl_payments.payment_option, 
          tbl_payments.reading_date, 
          tbl_payments.collection_date, 
             tbl_payments.date_encoded, 
          tbl_payments.payment_date 
       FROM 
          tbl_user_account 
       LEFT JOIN 
          tbl_payments 
       ON 
          tbl_user_account.unique_id = tbl_payments.unique_id 
       WHERE 
          tbl_payments.payment_date LIKE '$newcurrentdate%' 
       ORDER BY 
          tbl_payments.date_encoded DESC"
    );
    
              $i = 1;
              while($row = mysqli_fetch_array($result)) {
                $unique_id = $row['unique_id'];
                $acct_holder = $row['acct_holder'];
                $firstname = $row['firstname'];
                $lastname = $row['lastname'];
                $pres_reading = $row['pres_reading']; 
                $payment_type = $row['payment_type'];
                $amount = $row['amount'];
                $payment_option = $row['payment_option'];
                $reading_date = $row['reading_date'];
                $payment_date = $row['payment_date'];
                $collection_date = $row['collection_date'];
                $referrence_code = $row['referrence_code'];
                $date_encoded = $row['date_encoded'];
              ?>
                <tr id="<?php echo $i; ?>">
				<td><?php echo $referrence_code; ?></td>
                  <td><?php echo $acct_holder; ?></td>
                 
                  <td><?php echo $row['unique_id']; ?></td>
                  <td><?php echo $row['payment_type']; ?></td>
                  <td><?php echo $row['amount']; ?></td>
                  <td><?php echo $row['payment_date']; ?></td>
                  <td><?php echo $row['reading_date']; ?></td>
                   <td><?php echo $row['date_encoded']; ?></td>
                
                  <td>
                    <a href="printreceipt.php?mid=<?php echo urlencode($unique_id); ?>&acctholder=<?php echo urlencode($acct_holder); ?>&fname=<?php echo urlencode($firstname); ?>&lname=<?php echo urlencode($lastname); ?>&pres_reading=<?php echo urlencode($pres_reading); ?>&payment_type=<?php echo urlencode($payment_type); ?>&amount=<?php echo urlencode($amount); ?>&payment_option=<?php echo urlencode($payment_option); ?>&reading_date=<?php echo urlencode($reading_date); ?>&payment_date=<?php echo urlencode($payment_date); ?>&collection_date=<?php echo urlencode($collection_date); ?>&refnum=<?php echo urlencode($referrence_code); ?>">
                      <i class="fas fa-print"></i> Print
                    </a>
                 </td>
                 <td>  <div class="hidebutton" id="hidebutton">
                    <button class="btn btn-danger btn-sm delete-btn" data-unique-id="<?php echo $unique_id; ?>" data-ref-code="<?php echo $referrence_code; ?>"><i class="fas fa-trash-alt"></i> </button>
                     </div>
             </td>
                </tr>
              <?php
              $i++;
              }
              ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
</section>

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script> <script>
 document.querySelectorAll('.delete-btn').forEach(function(button) {
    button.addEventListener('click', function() {
        const uniqueId = button.getAttribute('data-unique-id');
        const refCode = button.getAttribute('data-ref-code');
        const accountName = button.closest('tr').querySelector('td:nth-child(2)').innerText; // Get account name

        Swal.fire({
            title: 'Are you sure?',
            html: `<b>Account ID:</b> ${uniqueId} <br> <b>Account Name:</b> ${accountName} <br> <b>Reference #:</b> ${refCode}`,
            icon: 'warning',
            showCancelButton: true,
            confirmButtonText: 'Yes, delete it!',
            cancelButtonText: 'No, cancel!',
        }).then((result) => {
            if (result.isConfirmed) {
                // Make an AJAX request to delete the record
                fetch('delete_payment.php', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                    body: 'unique_id=' + uniqueId + '&ref_code=' + refCode
                })
                .then(response => response.text())
                .then(data => {
                    if (data === 'success') {
                        Swal.fire(
                            'Deleted!',
                            'The payment record has been deleted.',
                            'success'
                        ).then(() => {
                            location.reload(); // Reload the page after successful deletion
                        });
                    } else {
                        Swal.fire(
                            'Error!',
                            'There was an issue deleting the payment record.',
                            'error'
                        );
                    }
                })
                .catch((error) => {
                    console.error('Error:', error);
                    Swal.fire(
                        'Error!',
                        'An unexpected error occurred.',
                        'error'
                    );
                });
            }
        });
    });
});

</script>

