<?php
include 'includes/auth.php';
include 'db.php';
include 'includes/header.php';

// Handle form submission
if(isset($_POST['add_deposit'])){
    $deposit_date = $_POST['deposit_date'];
    $payment_method = $conn->real_escape_string($_POST['payment_method']);
    $amount = floatval($_POST['amount']);
    $recorded_by = $_SESSION['username']; // assuming session stores username

    if($amount <= 0){
        echo "<script>alert('Amount must be greater than 0');</script>";
    } else {
        $conn->query("INSERT INTO cash_deposits (deposit_date, payment_method, amount, recorded_by) 
                      VALUES ('$deposit_date', '$payment_method', $amount, '$recorded_by')");
        echo "<script>alert('Deposit recorded successfully');</script>";
        echo "<script>window.location='deposits.php';</script>";
        exit;
    }
}

// Fetch deposits
$deposits = $conn->query("SELECT * FROM cash_deposits ORDER BY deposit_date DESC");
?>

<div class="container mt-4">
     <div class="welcome-msg">
        <i class="fa fa-user-circle"></i> Welcome, <strong><?= htmlspecialchars($_SESSION['username'] ?? 'Guest') ?></strong>!
    </div>
    <h3 class="text-center">Record Cash Deposits</h3>
    <hr>

    <!-- Add Deposit Form -->
    <form method="POST" class="row g-3 mb-4">
        <div class="col-md-3">
            <label>Deposit Date</label>
            <input type="date" name="deposit_date" value="<?= date('Y-m-d') ?>" class="form-control" required>
        </div>
        <div class="col-md-3">
            <label>Payment Method</label>
            <select name="payment_method" class="form-control" required>
                <option value="">-- Select Method --</option>
                <option value="Cash">Cash</option>
                <option value="Card">Card</option>
                <option value="Mobile Money">Mobile Money</option>
                <option value="Bank Transfer">Bank Transfer</option>
            </select>
        </div>
        <div class="col-md-3">
            <label>Amount (MK)</label>
            <input type="number" step="0.01" name="amount" class="form-control" required>
        </div>
        <div class="col-md-3 mt-4">
            <button type="submit" name="add_deposit" class="btn btn-success w-100">Record Deposit</button>
        </div>
    </form>

    <hr>

    <!-- Deposit List -->
    <h4>All Deposits</h4>
    <table class="table table-bordered table-striped">
        <thead class="bg-dark text-white">
            <tr>
                <th>#</th>
                <th>Deposit Date</th>
                <th>Payment Method</th>
                <th>Amount (MK)</th>
                <th>Recorded By</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <?php
            $i = 1;
            while($row = $deposits->fetch_assoc()){
                echo "
                <tr>
                    <td>{$i}</td>
                    <td>{$row['deposit_date']}</td>
                    <td>{$row['payment_method']}</td>
                    <td>".number_format($row['amount'],2)."</td>
                    <td>{$row['recorded_by']}</td>
                    <td>
                     
                    </td>
                </tr>
                ";
                $i++;
            }
            ?>
        </tbody>
    </table>
</div>

<?php
// Handle deletion
if(isset($_GET['delete'])){
    $id = intval($_GET['delete']);
    $conn->query("DELETE FROM cash_deposits WHERE id=$id");
    echo "<script>alert('Deposit deleted');</script>";
    echo "<script>window.location='deposits.php';</script>";
    exit;
}
?>

<?php include 'includes/footer.php'; ?>
