<?php 
include("includes/db_config.php");
error_reporting(E_ALL);
ini_set('display_errors', 1);


// Fetch vendor details
$sql = "SELECT id, party_name FROM ah_party";
$result = $conn->query($sql);
$vendors = [];
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $vendors[$row['id']] = $row['party_name'];
    }
}

// Fetch transactions
$query = "
    SELECT 'Purchase' AS type, customer_id, receipt_no, receipt_date AS trans_date, transaction_id, bank_name, paid_amount AS amount 
    FROM ah_purchase_payment_out WHERE payment_type = 'bank'
    UNION ALL
    SELECT 'Sale' AS type, customer_id, receipt_no, date AS trans_date, transaction_id, bank_name, received_amonut AS amount
    FROM ah_sales_payment_in WHERE payment_type = 'bank'
    ORDER BY trans_date DESC
";
$result = $conn->query($query);
?>
<!DOCTYPE html>
<html lang="en" dir="ltr" data-theme="light">
<meta http-equiv="content-type" content="text/html;charset=utf-8" />

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <?php include("includes/css.php");?>
    <style>
        .modal-body .form-label{font-weight:600;}.viewptdt .form-label{font-weight:600;}.viewptdt label{margin-top:10px;}
    </style>
</head>

<body class="preload-active aside-active aside-mobile-minimized aside-desktop-maximized">
    <div class="holder">
        <?php include("includes/sidebar.php");?>
        <div class="wrapper">
            <?php include("includes/header.php");?>
            <div class="content">
                <div class="container-fluid g-5">                    
                    <div class="row">
                        <div class="col-12">
                            <div class="portlet">
                                <div class="portlet-header portlet-header-bordered">
                                     <div class="col-md-12">
                                        <h3 class="portlet-title">All Bank Transaction</h3>
                                    </div>
                                 </div>
                                <div class="portlet-body table-responsive">
                                    <table id="datatable-1" class="table table-bordered table-striped table-hover">
                                        <thead>
                                            <tr>
                                                <th>#</th>
                                                <th>Party Name</th>
                                                <th>Type</th>
                                                <th>Receipt No</th>
                                                <th>Transaction Date</th>
                                                <th>Transaction ID</th>
                                                <th>Bank Name</th>
                                                <th>Amount</th>
                                            </tr>
                                        </thead>
                                          <tbody>
                                            <?php if ($result->num_rows > 0) {
                                                $count = 1;
                                                while ($row = $result->fetch_assoc()) { ?>
                                                    <tr>
                                                        <td><?php echo $count++; ?></td>
                                                        <td><?php echo isset($vendors[$row['customer_id']]) ? $vendors[$row['customer_id']] : 'Unknown'; ?></td>
                                                        <td><?php echo $row['type']; ?></td>
                                                        <td><?= htmlspecialchars($row['receipt_no']) ?></td>
                                                        <td><?= htmlspecialchars($row['trans_date']) ?></td>
                                                        <td><?= htmlspecialchars($row['transaction_id']) ?></td>
                                                        <td><?= htmlspecialchars($row['bank_name']) ?></td>
                                                        <td><?= htmlspecialchars(number_format($row['amount'], 2)) ?></td>
                                                    </tr>
                                                <?php } 
                                            } else { ?>
                                                <tr>
                                                    <td colspan="8" class="text-center">No transactions found</td>
                                                </tr>
                                            <?php } ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include("includes/footer.php");?>
        </div>
    </div>
    <?php include("includes/js.php");?>
</body>

</html>