<?php
session_start();
error_reporting(0);

// Check if logged in
if (!isset($_SESSION['agent_id'])) {
    header("Location: index.php");
    exit();
}

include_once('../include/db_config.php');

$agentId = $_SESSION['agent_id'];

// Get agent vouchers
try {
    $conn = getDBConnection();
    $stmt = $conn->prepare("SELECT * FROM agent_vouchers WHERE agent_id = :agent_id ORDER BY created_at DESC");
    $stmt->execute([':agent_id' => $agentId]);
    $vouchers = $stmt->fetchAll();
} catch (Exception $e) {
    $vouchers = [];
}

// Handle logout
if (isset($_GET['logout'])) {
    session_destroy();
    header("Location: index.php");
    exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Vouchers - MikhMon</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="agent.css">
    <style>
        .voucher-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
            gap: 20px;
            margin-top: 20px;
        }
        .voucher-card {
            border: 1px solid #e0e0e0;
            border-radius: 10px;
            padding: 20px;
            text-align: center;
            background: white;
            box-shadow: 0 2px 5px rgba(0,0,0,0.05);
        }
        .voucher-username {
            font-weight: bold;
            font-size: 18px;
            margin-bottom: 5px;
            color: #333;
        }
        .voucher-password {
            font-family: monospace;
            background: #f8f9fa;
            padding: 8px;
            border-radius: 5px;
            margin: 10px 0;
            font-size: 16px;
        }
        .voucher-profile {
            color: #666;
            font-size: 14px;
            margin: 5px 0;
        }
        .voucher-status {
            display: inline-block;
            padding: 4px 12px;
            border-radius: 12px;
            font-size: 12px;
            font-weight: 600;
            margin-top: 10px;
        }
        .status-active {
            background: #d1fae5;
            color: #065f46;
        }
        .status-used {
            background: #dbeafe;
            color: #1e40af;
        }
        .status-expired {
            background: #fee2e2;
            color: #991b1b;
        }
        .voucher-date {
            font-size: 12px;
            color: #999;
            margin-top: 10px;
        }
        .filter-form {
            display: flex;
            gap: 15px;
            margin-bottom: 20px;
            flex-wrap: wrap;
        }
        .filter-form .form-group {
            margin-bottom: 0;
            flex: 1;
            min-width: 200px;
        }
        @media (max-width: 768px) {
            .filter-form {
                flex-direction: column;
            }
        }
    </style>
</head>
<body>
    <div class="header">
        <h1><i class="fa fa-ticket"></i> My Vouchers</h1>
        <nav>
            <a href="dashboard.php"><i class="fa fa-dashboard"></i> Dashboard</a>
            <a href="?logout=1"><i class="fa fa-sign-out"></i> Logout</a>
        </nav>
    </div>
    
    <div class="container">
        <div class="card">
            <div class="card-header">
                <h2>Generated Vouchers</h2>
                <div>Total Vouchers: <?= count($vouchers); ?></div>
            </div>
            
            <?php if (!empty($vouchers)): ?>
            <div class="voucher-grid">
                <?php foreach ($vouchers as $voucher): ?>
                <div class="voucher-card">
                    <div class="voucher-username"><?= $voucher['username']; ?></div>
                    <div class="voucher-password"><?= $voucher['password']; ?></div>
                    <div class="voucher-profile"><?= $voucher['profile_name']; ?></div>
                    <div class="voucher-profile">Price: Rp <?= number_format($voucher['sell_price'], 0, ',', '.'); ?></div>
                    <div class="voucher-status status-<?= $voucher['status']; ?>">
                        <?= ucfirst($voucher['status']); ?>
                    </div>
                    <div class="voucher-date">
                        <?= date('d M Y H:i', strtotime($voucher['created_at'])); ?>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
            <?php else: ?>
            <p>No vouchers found.</p>
            <?php endif; ?>
        </div>
    </div>
</body>
</html>