<?php
include "db.php";
// 1. Database Connection - Apni details yahan update karen
$conn = mysqli_connect("localhost", "root", "", "tameeralmadina_tameer");

// 2. Data handle karna (ID aur Account Type url se aayega)
$acc_id = isset($_GET['acc_id']) ? $_GET['acc_id'] : 1; 
$acc_type = isset($_GET['acc_type']) ? $_GET['acc_type'] : 'CASH';

// Sahi table select karna (Cash ya Bank)
$table = ($acc_type == 'BANK') ? 'bank_accounts' : 'cash_accounts';

// Opening Balance fetch karna
$res = mysqli_query($conn, "SELECT initial_balance FROM $table WHERE id = $acc_id");
$acc_data = mysqli_fetch_assoc($res);
$running_balance = $acc_data ? $acc_data['initial_balance'] : 0;

// Transactions fetch karna
$sql = "SELECT * FROM transactions WHERE reference_id = $acc_id AND amount_type = '$acc_type' ORDER BY date ASC";
$result = mysqli_query($conn, $sql);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Account Ledger</title>
    <!-- Bootstrap 5 CSS for Responsive Design -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body { background-color: #f8f9fa; font-family: sans-serif; }
        .ledger-container { margin-top: 30px; }
        .table-custom { border-radius: 10px; overflow: hidden; }
        .header-bg { background-color: #007bff; color: white; }
    </style>
</head>
<body>

<div class="container ledger-container">
    <div class="card shadow">
        <div class="card-header header-bg">
            <h4 class="mb-0">Ledger View: <?php echo $acc_type; ?></h4>
        </div>
        <div class="card-body">
            <!-- Opening Balance Card -->
            <div class="alert alert-info d-flex justify-content-between">
                <span><strong>Opening Balance:</strong></span>
                <span><?php echo number_format($running_balance, 2); ?></span>
            </div>
            
            <!-- Ledger Table -->
            <div class="table-responsive table-custom">
                <table class="table table-bordered table-hover">
                    <thead class="table-dark">
                        <tr>
                            <th>Date</th>
                            <th>Description</th>
                            <th>Type</th>
                            <th>Amount</th>
                            <th>Running Balance</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        while($row = mysqli_fetch_assoc($result)) {
                            // Running balance logic
                            if($row['type'] == 'CREDIT') { 
                                $running_balance += $row['amount']; 
                                $color = "text-success fw-bold"; 
                            } else { 
                                $running_balance -= $row['amount']; 
                                $color = "text-danger fw-bold"; 
                            }
                            
                            echo "<tr>
                                    <td>{$row['date']}</td>
                                    <td>{$row['description']}</td>
                                    <td>".($row['type'] == 'CREDIT' ? 'Add' : 'Cut')."</td>
                                    <td class='$color'>".number_format($row['amount'], 2)."</td>
                                    <td><strong>".number_format($running_balance, 2)."</strong></td>
                                </tr>";
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

</body>
</html>