<?php
session_start();
if (!isset($_SESSION['admin_id']) && $_SESSION['role'] != 'admin') {
    header("Location: login.php");
    exit();
}
include '../include/config.php';

$payment_links = $conn->query("
    SELECT pl.*, pi.item_name 
    FROM payment_links pl
    LEFT JOIN payment_items pi ON pl.item_type = pi.item_type
    ORDER BY pl.created_at DESC
");

$page_title = 'Payment Links | Admin';
include '../include/header.php';
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Payment Links - Admin</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-4">
    <div class="d-flex justify-content-between mb-4">
        <h2><i class="fas fa-link me-2"></i> Payment Links</h2>
        <a href="payment_settings.php" class="btn btn-secondary">← Back to Settings</a>
    </div>

    <div class="card shadow rounded-4">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead class="table-light">
                        <tr><th>Token</th><th>Item</th><th>Amount</th><th>Customer</th><th>Status</th><th>Created</th><th>Action</th></tr>
                    </thead>
                    <tbody>
                        <?php while($link = $payment_links->fetch_assoc()): ?>
                        <tr>
                            <td><code><?= $link['link_token'] ?></code></td
                            <td><?= htmlspecialchars($link['item_name'] ?? $link['item_type']) ?></td
                            <td class="fw-bold">₦<?= number_format($link['amount'], 2) ?></td
                            <td><?= htmlspecialchars($link['customer_email'] ?? 'N/A') ?></td
                            <td>
                                <span class="badge bg-<?= $link['status'] == 'paid' ? 'success' : ($link['status'] == 'pending' ? 'warning' : 'secondary') ?>">
                                    <?= ucfirst($link['status']) ?>
                                </span>
                             </td
                            <td><?= date('M d, Y', strtotime($link['created_at'])) ?></td
                            <td>
                                <button class="btn btn-sm btn-info" onclick="copyLink('https://uhd.com.ng/payment/pay.php?token=<?= $link['link_token'] ?>')">Copy</button>
                                <?php if($link['status'] == 'pending'): ?>
                                    <button class="btn btn-sm btn-danger" onclick="cancelLink(<?= $link['id'] ?>)">Cancel</button>
                                <?php endif; ?>
                             </td
                          </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<script>
function copyLink(url) {
    navigator.clipboard.writeText(url);
    alert('Link copied!');
}
function cancelLink(id) {
    if(confirm('Cancel this payment link?')) {
        fetch('api/cancel_link.php', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ id: id })
        }).then(() => location.reload());
    }
}
</script>
<?php include '../include/footer.php'; ?>