<?php
session_start();
if (!isset($_SESSION['admin_id'])) {
    header("Location: admin-login.php");
    exit;
}

$conn = mysqli_connect("localhost","root","","dubai_treasury_bank");
if (!$conn) die("DB error");

$user_id = $_GET['id'];

if(isset($_POST['post'])){
    $title = $_POST['title'];
    $desc = $_POST['desc'];
    $amount = $_POST['amount'];
    $type = $_POST['type'];

    $insert = "INSERT INTO transactions (user_id, type, title, description, amount) 
               VALUES ('$user_id','$type','$title','$desc','$amount')";
    mysqli_query($conn,$insert);
}

$trans = mysqli_query($conn,"SELECT * FROM transactions WHERE user_id='$user_id' ORDER BY id DESC");
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Admin - User Transactions</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
    <h3>Transaction History (Blog Style)</h3>

    <div class="card">
        <div class="card-body">
            <h4>Post Transaction</h4>
            <form method="POST">
                <div class="row">
                    <div class="col-md-4">
                        <label>Type</label>
                        <select class="form-control" name="type">
                            <option value="credit">Credit</option>
                            <option value="debit">Debit</option>
                        </select>
                    </div>
                    <div class="col-md-4">
                        <label>Title</label>
                        <input class="form-control" name="title" required>
                    </div>
                    <div class="col-md-4">
                        <label>Amount</label>
                        <input class="form-control" name="amount" required>
                    </div>
                    <div class="col-md-12">
                        <label>Description</label>
                        <textarea class="form-control" name="desc" rows="3" required></textarea>
                    </div>
                </div>
                <br>
                <button class="btn btn-primary" name="post">Post Transaction</button>
            </form>
        </div>
    </div>

    <hr>

    <h4>History</h4>

    <?php while($row = mysqli_fetch_assoc($trans)) { ?>
        <div class="card mb-3">
            <div class="card-body">
                <h5><?php echo $row['title']; ?> <span class="badge bg-<?php echo ($row['type']=="credit") ? 'success' : 'danger'; ?>"><?php echo $row['type']; ?></span></h5>
                <p><?php echo $row['description']; ?></p>
                <small><?php echo $row['created_at']; ?></small>
                <h6>AED <?php echo $row['amount']; ?></h6>
            </div>
        </div>
    <?php } ?>
</div>
</body>
</html>
