<?php 
session_start();
require_once '../db_connect.php'; 
if (!isset($_SESSION['admin_logged_in'])) { header("Location: login.php"); exit; }

$msg = "";

// Approve Payment Logic
if (isset($_GET['approve_id'])) {
    $pay_id = (int)$_GET['approve_id'];
    // ১. পেমেন্ট স্ট্যাটাস আপডেট
    $stmt = $pdo->prepare("UPDATE students_payments SET status = 'approved' WHERE id = ?");
    $stmt->execute([$pay_id]);
    
    // ২. স্টুডেন্টের ট্রায়াল পিরিয়ড শেষ করে একাউন্ট ফুল অ্যাক্টিভ করা (ঐচ্ছিক)
    $stmt = $pdo->prepare("SELECT student_id FROM students_payments WHERE id = ?");
    $stmt->execute([$pay_id]);
    $sid = $stmt->fetchColumn();
    $pdo->prepare("UPDATE students SET trial_ends = '2099-12-31' WHERE id = ?")->execute([$sid]);

    $msg = "<div class='alert alert-success fw-bold'>Payment Approved Successfully!</div>";
}

// Reject Payment Logic
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['reject_payment'])) {
    $pay_id = (int)$_POST['pay_id'];
    $note = htmlspecialchars($_POST['rejection_note']);
    $stmt = $pdo->prepare("UPDATE students_payments SET status = 'rejected', note = ? WHERE id = ?");
    $stmt->execute([$note, $pay_id]);
    $msg = "<div class='alert alert-danger fw-bold'>Payment Rejected.</div>";
}

include 'includes/header.php'; 
include 'includes/sidebar.php'; 

$payments = $pdo->query("SELECT p.*, s.name as student_name, s.email FROM students_payments p JOIN students s ON p.student_id = s.id ORDER BY p.id DESC")->fetchAll(PDO::FETCH_ASSOC);
?>

<div class="main-content p-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h4 class="fw-bold text-dark">Verify TrxID Payments</h4>
        <div id="sidebarToggle" class="d-md-none btn btn-light border"><i class="fas fa-bars"></i></div>
    </div>

    <?= $msg ?>

    <div class="card border-0 shadow-sm rounded-4 overflow-hidden">
        <div class="table-responsive">
            <table class="table align-middle mb-0">
                <thead class="bg-light">
                    <tr>
                        <th class="ps-4">Student</th>
                        <th>Amount & Method</th>
                        <th>TrxID</th>
                        <th>Status</th>
                        <th class="text-end pe-4">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach($payments as $p): ?>
                    <tr>
                        <td class="ps-4">
                            <div class="fw-bold"><?= htmlspecialchars($p['student_name']) ?></div>
                            <small class="text-muted"><?= $p['email'] ?></small>
                        </td>
                        <td>
                            <div class="fw-bold text-success">৳<?= number_format($p['amount']) ?></div>
                            <small class="text-muted"><?= $p['method'] ?></small>
                        </td>
                        <td><code class="fw-bold text-primary"><?= $p['trx_id'] ?></code></td>
                        <td>
                            <?php if($p['status'] == 'pending'): ?>
                                <span class="badge bg-warning text-dark px-3 py-2 rounded-pill">Pending</span>
                            <?php elseif($p['status'] == 'approved'): ?>
                                <span class="badge bg-success px-3 py-2 rounded-pill">Approved</span>
                            <?php else: ?>
                                <span class="badge bg-danger px-3 py-2 rounded-pill">Rejected</span>
                            <?php endif; ?>
                        </td>
                        <td class="text-end pe-4">
                            <?php if($p['status'] == 'pending'): ?>
                                <a href="?approve_id=<?= $p['id'] ?>" class="btn btn-sm btn-success rounded-3 me-1" onclick="return confirm('Approve this?')">Approve</a>
                                <button class="btn btn-sm btn-outline-danger rounded-3" onclick="openRejectModal(<?= $p['id'] ?>)">Reject</button>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<div class="modal fade" id="rejectModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <form method="POST" class="modal-content border-0 shadow">
            <div class="modal-body p-4">
                <input type="hidden" name="pay_id" id="reject_pay_id">
                <h5 class="fw-bold mb-3">Reject Payment</h5>
                <label class="small fw-bold text-muted">Reason for Rejection</label>
                <textarea name="rejection_note" class="form-control border-2" rows="3" placeholder="e.g. Invalid TrxID" required></textarea>
                <div class="mt-4 d-flex gap-2">
                    <button type="submit" name="reject_payment" class="btn btn-danger w-100 py-2 fw-bold">REJECT NOW</button>
                    <button type="button" class="btn btn-light w-100 py-2 border" data-bs-dismiss="modal">CANCEL</button>
                </div>
            </div>
        </form>
    </div>
</div>

<script>
function openRejectModal(id) {
    document.getElementById('reject_pay_id').value = id;
    new bootstrap.Modal(document.getElementById('rejectModal')).show();
}
</script>
<?php include 'includes/footer.php'; ?>