<?php
/**
 * LICENSE MANAGER ADMIN PANEL
 * 
 * Secure page to manage licenses and activations.
 */
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once __DIR__ . '/config.php';

// SIMPLE PASSWORD PROTECTION
$admin_password = "Meezan@2026"; 

if (isset($_GET['logout'])) {
    session_destroy();
    header("Location: manage.php");
    exit;
}

if (!isset($_SESSION['license_admin_auth'])) {
    if (isset($_POST['password']) && $_POST['password'] === $admin_password) {
        $_SESSION['license_admin_auth'] = true;
        header("Location: manage.php");
        exit;
    }
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>License Admin Login</title>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
        <style>
            body { background: #f8fafc; font-family: 'Inter', sans-serif; display: flex; align-items: center; justify-content: center; height: 100vh; margin: 0; }
            .login-card { width: 100%; max-width: 400px; padding: 2.5rem; border-radius: 1.25rem; box-shadow: 0 20px 25px -5px rgba(0,0,0,0.1), 0 10px 10px -5px rgba(0,0,0,0.04); background: white; border: 1px solid #e2e8f0; }
            .btn-primary { background: #2563eb; border: none; padding: 0.75rem; font-weight: 600; border-radius: 0.75rem; transition: all 0.2s; }
            .btn-primary:hover { background: #1d4ed8; transform: translateY(-1px); }
            .form-control { border-radius: 0.75rem; padding: 0.75rem 1rem; border: 1px solid #e2e8f0; }
            .form-control:focus { box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.1); border-color: #2563eb; }
        </style>
    </head>
    <body>
        <div class="login-card text-center">
            <div class="mb-4">
                <div class="d-inline-flex align-items-center justify-content-center bg-primary bg-opacity-10 text-primary rounded-circle mb-3" style="width: 64px; height: 64px;">
                    <svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="currentColor" class="bi bi-shield-lock" viewBox="0 0 16 16">
                        <path d="M5.338 1.59a.5.5 0 0 1 .42.494v.054a4 4 0 0 0 4 4h.054a.5.5 0 0 1 .494.42 12 12 0 0 1-1.306 9.176.5.5 0 0 1-.444.25h-5.912a.5.5 0 0 1-.444-.25 12 12 0 0 1-1.306-9.176.5.5 0 0 1 .494-.42h.054a4 4 0 0 0 4-4V2.084a.5.5 0 0 1 .42-.494zM8 0c-.69 0-1.382.04-2.074.12A.5.5 0 0 0 5.5 0a.5.5 0 0 0-.5.5v.5a3 3 0 0 1-3 3H1.5a.5.5 0 0 0-.5.5v.5a13 13 0 0 0 1.517 9.873A1.5 1.5 0 0 0 3.794 16h8.412a1.5 1.5 0 0 0 1.277-.627 13 13 0 0 0 1.517-9.873V5.5a.5.5 0 0 0-.5-.5H14a3 3 0 0 1-3-3V1.5a.5.5 0 0 0-.5-.5z"/>
                    </svg>
                </div>
                <h3 class="fw-bold">License Admin</h3>
                <p class="text-muted small">Enter password to manage licenses</p>
            </div>
            <form method="POST">
                <div class="mb-4 text-start">
                    <label class="form-label small fw-bold text-uppercase text-secondary">Password</label>
                    <input type="password" name="password" class="form-control" placeholder="••••••••" required autofocus>
                </div>
                <button type="submit" class="btn btn-primary w-100 mb-3">Login to Dashboard</button>
            </form>
            <?php if (isset($_POST['password'])): ?>
                <div class="text-danger small mt-2">Invalid password. Please try again.</div>
            <?php endif; ?>
        </div>
    </body>
    </html>
    <?php
    exit;
}

try {
    $pdo = db_manager();
} catch (Exception $e) {
    die("Database connection failed: " . $e->getMessage());
}

$message = '';
$error = '';

// Handle Actions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    try {
        if (isset($_POST['action'])) {
            switch ($_POST['action']) {
                case 'toggle_status':
                    $id = (int)$_POST['id'];
                    $new_status = $_POST['status'] === 'active' ? 'suspended' : 'active';
                    $stmt = $pdo->prepare("UPDATE licenses SET status = ? WHERE id = ?");
                    $stmt->execute([$new_status, $id]);
                    $message = "License status updated to $new_status.";
                    break;
                
                case 'delete_license':
                    $id = (int)$_POST['id'];
                    $stmt = $pdo->prepare("DELETE FROM licenses WHERE id = ?");
                    $stmt->execute([$id]);
                    $message = "License deleted successfully.";
                    break;

                case 'issue':
                    $prefix = strtoupper(trim($_POST['prefix'] ?? 'FLAT'));
                    $max_activations = (int)($_POST['max_activations'] ?? 1);
                    $max_counters = (int)($_POST['max_counters'] ?? 1);
                    $owner = trim($_POST['owner'] ?? '');
                    $address = trim($_POST['address'] ?? '');
                    
                    // Generate key
                    $key = $prefix . '-' . strtoupper(bin2hex(random_bytes(2))) . '-' . strtoupper(bin2hex(random_bytes(2))) . '-' . strtoupper(bin2hex(random_bytes(2)));
                    
                    $stmt = $pdo->prepare("INSERT INTO licenses (license_key, max_activations, max_counters, owner, address, status) VALUES (?, ?, ?, ?, ?, 'active')");
                    $stmt->execute([$key, $max_activations, $max_counters, $owner, $address]);
                    $message = "New license issued successfully: <strong>$key</strong>";
                    break;

                case 'update_limits':
                    $id = (int)$_POST['id'];
                    $max_activations = (int)$_POST['max_activations'];
                    $max_counters = (int)$_POST['max_counters'];
                    $owner = trim($_POST['owner'] ?? '');
                    $address = trim($_POST['address'] ?? '');

                    $stmt = $pdo->prepare("UPDATE licenses SET max_activations = ?, max_counters = ?, owner = ?, address = ? WHERE id = ?");
                    $stmt->execute([$max_activations, $max_counters, $owner, $address, $id]);
                    $message = "License limits updated successfully.";
                    break;
            }
            
            // Redirect to prevent re-submission on refresh
            if ($message) {
                $_SESSION['flash_message'] = $message;
                header("Location: manage.php");
                exit;
            }
        }
    } catch (Exception $e) {
        $error = "Operation failed: " . $e->getMessage();
    }
}

// Get flash message from session
if (isset($_SESSION['flash_message'])) {
    $message = $_SESSION['flash_message'];
    unset($_SESSION['flash_message']);
}

// Fetch Licenses
try {
    $licenses = $pdo->query("SELECT * FROM licenses ORDER BY created_at DESC")->fetchAll();
} catch (Exception $e) {
    $licenses = [];
    $error = "Failed to fetch licenses: " . $e->getMessage();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage Licenses | Admin Panel</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <style>
        :root { --primary: #2563eb; --primary-dark: #1d4ed8; --bg: #f8fafc; --card: #ffffff; --border: #e2e8f0; --text-main: #0f172a; --text-muted: #64748b; }
        body { background: var(--bg); font-family: 'Inter', sans-serif; color: var(--text-main); }
        .navbar { background: var(--text-main); padding: 1rem 0; box-shadow: 0 4px 12px rgba(0,0,0,0.1); }
        .navbar-brand { font-weight: 700; font-size: 1.25rem; }
        .dashboard-container { max-width: 1200px; margin: 2rem auto; padding: 0 1rem; }
        .card { background: var(--card); border: 1px solid var(--border); border-radius: 1rem; box-shadow: 0 1px 3px rgba(0,0,0,0.05); overflow: hidden; }
        .card-header { background: #fff; border-bottom: 1px solid var(--border); padding: 1.5rem; display: flex; justify-content: space-between; align-items: center; }
        .table thead th { background: #f1f5f9; text-transform: uppercase; font-size: 0.75rem; font-weight: 700; color: var(--text-muted); padding: 1rem; border: none; }
        .table tbody td { padding: 1rem; vertical-align: middle; border-bottom: 1px solid var(--border); }
        .btn-issue { background: var(--primary); color: white; border: none; padding: 0.625rem 1.25rem; border-radius: 0.75rem; font-weight: 600; transition: all 0.2s; }
        .btn-issue:hover { background: var(--primary-dark); transform: translateY(-1px); color: white; }
        .status-active { background: #dcfce7; color: #15803d; font-size: 0.75rem; font-weight: 600; padding: 0.25rem 0.75rem; border-radius: 2rem; }
        .status-suspended { background: #fee2e2; color: #b91c1c; font-size: 0.75rem; font-weight: 600; padding: 0.25rem 0.75rem; border-radius: 2rem; }
        .key-badge { background: #f1f5f9; font-family: monospace; color: var(--text-main); font-weight: 600; padding: 0.4rem 0.6rem; border-radius: 0.5rem; font-size: 0.9rem; border: 1px solid #e2e8f0; }
        .progress { height: 6px; border-radius: 1rem; background: #f1f5f9; }
        .action-btn { width: 32px; height: 32px; display: inline-flex; align-items: center; justify-content: center; border-radius: 0.5rem; transition: all 0.2s; border: 1px solid var(--border); background: #fff; color: var(--text-muted); }
        .action-btn:hover { background: #f8fafc; color: var(--primary); border-color: var(--primary); }
        .action-btn.delete:hover { color: #dc2626; border-color: #dc2626; }
        .modal-content { border-radius: 1.25rem; border: none; box-shadow: 0 25px 50px -12px rgba(0,0,0,0.25); }
        .form-label { font-weight: 600; color: var(--text-main); font-size: 0.875rem; margin-bottom: 0.5rem; }
        .form-control, .form-select { border-radius: 0.75rem; border: 1px solid var(--border); padding: 0.75rem 1rem; }
        .search-container { position: relative; max-width: 300px; }
        .search-container i { position: absolute; left: 1rem; top: 50%; transform: translateY(-50%); color: var(--text-muted); }
        .search-container input { padding-left: 2.5rem; background: #f8fafc; }
        .extra-small { font-size: 0.7rem; }
    </style>
</head>
<body>

<nav class="navbar navbar-dark">
    <div class="container">
        <a class="navbar-brand d-flex align-items-center" href="#">
            <i class="bi bi-shield-check me-2 fs-4"></i>
            <span>LICENSE MANAGER</span>
        </a>
        <div class="d-flex align-items-center">
            <span class="text-white-50 small me-3 d-none d-md-block">Admin Authenticated</span>
            <a href="?logout=1" class="btn btn-outline-light btn-sm rounded-pill px-3">Logout</a>
        </div>
    </div>
</nav>

<div class="dashboard-container">
    <?php if ($message): ?>
        <div class="alert alert-success alert-dismissible fade show rounded-4 border-0 shadow-sm mb-4" role="alert">
            <i class="bi bi-check-circle-fill me-2"></i> <?= $message ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    <?php endif; ?>

    <?php if ($error): ?>
        <div class="alert alert-danger alert-dismissible fade show rounded-4 border-0 shadow-sm mb-4" role="alert">
            <i class="bi bi-exclamation-triangle-fill me-2"></i> <?= $error ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    <?php endif; ?>

    <div class="card">
        <div class="alert alert-info border-0 rounded-0 m-0 small py-2 px-4">
            <i class="bi bi-info-circle-fill me-2"></i> <strong>Tip:</strong> <u>Machines Limit</u> controls how many separate computers can use the key. <u>Counters Limit</u> is an internal value used by the software features.
        </div>
        <div class="card-header">
            <div class="d-flex align-items-center gap-3">
                <h5 class="mb-0 fw-bold">Licenses Dashboard</h5>
                <div class="search-container d-none d-md-block">
                    <i class="bi bi-search"></i>
                    <input type="text" id="licenseSearch" class="form-control form-control-sm" placeholder="Search by key or owner...">
                </div>
            </div>
            <button class="btn-issue" data-bs-toggle="modal" data-bs-target="#issueModal">
                <i class="bi bi-plus-lg me-1"></i> Issue New Key
            </button>
        </div>
        <div class="table-responsive">
            <table class="table table-hover mb-0" id="licensesTable">
                <thead>
                    <tr>
                        <th>License Key</th>
                        <th>Owner & Notes</th>
                        <th>Machines</th>
                        <th>Counters</th>
                        <th>Status</th>
                        <th>Created</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($licenses as $l): ?>
                    <tr>
                        <td>
                            <span class="key-badge"><?= htmlspecialchars($l['license_key']) ?></span>
                        </td>
                        <td>
                            <div class="fw-semibold"><?= htmlspecialchars($l['owner'] ?: 'Unknown Client') ?></div>
                            <div class="text-muted small text-truncate" style="max-width: 150px;"><?= htmlspecialchars($l['address'] ?: 'No notes') ?></div>
                        </td>
                        <td>
                            <?php
                            try {
                                $stmt = $pdo->prepare("SELECT COUNT(*) FROM activations WHERE license_id = ?");
                                $stmt->execute([$l['id']]);
                                $count = $stmt->fetchColumn();
                            } catch (Exception $e) { $count = 0; }
                            
                            $max = $l['max_activations'] ?: 1;
                            $percent = ($count / $max) * 100;
                            $bar_color = $percent >= 100 ? 'bg-danger' : ($percent >= 80 ? 'bg-warning' : 'bg-success');
                            ?>
                            <div class="d-flex justify-content-between align-items-center mb-1">
                                <span class="small fw-bold"><?= $count ?> / <?= $max ?></span>
                            </div>
                            <div class="progress" style="width: 80px;">
                                <div class="progress-bar <?= $bar_color ?>" style="width: <?= min(100, $percent) ?>%"></div>
                            </div>
                        </td>
                        <td>
                            <span class="badge bg-light text-dark border px-2 py-1"><?= $l['max_counters'] ?? 0 ?> Limit</span>
                        </td>
                        <td>
                            <span class="status-<?= $l['status'] ?>"><?= ucfirst($l['status']) ?></span>
                        </td>
                        <td>
                            <span class="text-muted small"><?= date('M d, Y', strtotime($l['created_at'])) ?></span>
                        </td>
                        <td class="text-end">
                            <div class="d-flex justify-content-end gap-2">
                                <button class="btn btn-sm btn-outline-primary action-btn" title="Edit License" data-bs-toggle="modal" data-bs-target="#editModal-<?= $l['id'] ?>">
                                    <i class="bi bi-pencil-square"></i>
                                </button>
                                <button class="action-btn" title="View Activations" data-bs-toggle="modal" data-bs-target="#modal-<?= $l['id'] ?>">
                                    <i class="bi bi-laptop"></i>
                                </button>
                                <form method="POST" class="d-inline">
                                    <input type="hidden" name="action" value="toggle_status">
                                    <input type="hidden" name="id" value="<?= $l['id'] ?>">
                                    <input type="hidden" name="status" value="<?= $l['status'] ?>">
                                    <button type="submit" class="action-btn" title="Toggle Status">
                                        <i class="bi bi-power"></i>
                                    </button>
                                </form>
                                <form method="POST" class="d-inline" onsubmit="return confirm('Permanently delete this license?');">
                                    <input type="hidden" name="action" value="delete_license">
                                    <input type="hidden" name="id" value="<?= $l['id'] ?>">
                                    <button type="submit" class="action-btn delete" title="Delete">
                                        <i class="bi bi-trash"></i>
                                    </button>
                                </form>
                            </div>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    <?php if (empty($licenses)): ?>
                        <tr><td colspan="7" class="text-center py-5 text-muted">No licenses found in the database.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- Modals Section -->
<?php foreach ($licenses as $l): ?>
    <!-- Edit Modal -->
    <div class="modal fade" id="editModal-<?= $l['id'] ?>" tabindex="-1">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header bg-primary text-white">
                    <h5 class="modal-title fw-bold">Edit License: <?= htmlspecialchars($l['license_key']) ?></h5>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
                </div>
                <form method="POST">
                    <div class="modal-body">
                        <input type="hidden" name="action" value="update_limits">
                        <input type="hidden" name="id" value="<?= $l['id'] ?>">
                        <div class="row g-3">
                            <div class="col-6">
                                <label class="form-label">Machines Limit</label>
                                <input type="number" name="max_activations" class="form-control" value="<?= $l['max_activations'] ?>" min="1">
                                <div class="extra-small text-muted">Number of allowed PCs</div>
                            </div>
                            <div class="col-6">
                                <label class="form-label">Counters Limit</label>
                                <input type="number" name="max_counters" class="form-control" value="<?= $l['max_counters'] ?>" min="1">
                                <div class="extra-small text-muted">Internal software limit</div>
                            </div>
                            <div class="col-12">
                                <label class="form-label">Owner Name</label>
                                <input type="text" name="owner" class="form-control" value="<?= htmlspecialchars($l['owner'] ?? '') ?>">
                            </div>
                            <div class="col-12">
                                <label class="form-label">Address / Notes</label>
                                <textarea name="address" class="form-control" rows="2"><?= htmlspecialchars($l['address'] ?? '') ?></textarea>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer border-0">
                        <button type="button" class="btn btn-light rounded-pill px-4" data-bs-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-primary rounded-pill px-4">Save Changes</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    
    <!-- Activations Modal -->
    <div class="modal fade" id="modal-<?= $l['id'] ?>" tabindex="-1">
        <div class="modal-dialog modal-dialog-centered modal-lg">
            <div class="modal-content">
                <div class="modal-header bg-primary text-white">
                    <h5 class="modal-title fw-bold">Activations: <?= htmlspecialchars($l['license_key']) ?></h5>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body p-0">
                    <?php
                    try {
                        $stmt = $pdo->prepare("SELECT * FROM activations WHERE license_id = ? ORDER BY activated_at DESC");
                        $stmt->execute([$l['id']]);
                        $acts = $stmt->fetchAll();
                    } catch (Exception $e) { $acts = []; }
                    
                    if ($acts):
                    ?>
                    <table class="table table-sm mb-0">
                        <thead>
                            <tr>
                                <th class="ps-4">Fingerprint</th>
                                <th>Domain / Product</th>
                                <th class="pe-4">Activated Date</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($acts as $a): ?>
                            <tr>
                                <td class="ps-4"><code><?= htmlspecialchars($a['fingerprint']) ?></code></td>
                                <td>
                                    <div class="small fw-semibold"><?= htmlspecialchars($a['domain'] ?: '-') ?></div>
                                    <div class="text-muted extra-small"><?= htmlspecialchars($a['product'] ?: '-') ?></div>
                                </td>
                                <td class="pe-4 small text-muted"><?= $a['activated_at'] ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                    <?php else: ?>
                        <div class="text-center py-5">
                            <i class="bi bi-display text-light fs-1"></i>
                            <p class="text-muted mt-2">No machines currently activated.</p>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
<?php endforeach; ?>

<!-- Issue Key Modal -->
<div class="modal fade" id="issueModal" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header bg-primary text-white">
                <h5 class="modal-title fw-bold">Issue New License Key</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST">
                <div class="modal-body">
                    <input type="hidden" name="action" value="issue">
                    <div class="row g-3">
                        <div class="col-6">
                            <label class="form-label">Key Prefix</label>
                            <input type="text" name="prefix" class="form-control" value="FLAT" maxlength="10">
                        </div>
                        <div class="col-6">
                            <label class="form-label">Machines Limit</label>
                            <input type="number" name="max_activations" class="form-control" value="1" min="1">
                            <div class="extra-small text-muted text-primary fw-semibold">Controls how many PCs can activate</div>
                        </div>
                        <div class="col-6">
                            <label class="form-label">Counters Limit</label>
                            <input type="number" name="max_counters" class="form-control" value="1" min="1">
                            <div class="extra-small text-muted">Internal value returned to software</div>
                        </div>
                        <div class="col-6">
                            <label class="form-label">Owner Name</label>
                            <input type="text" name="owner" class="form-control" placeholder="Client name">
                        </div>
                        <div class="col-12">
                            <label class="form-label">Address / Notes</label>
                            <textarea name="address" class="form-control" rows="2" placeholder="Optional details..."></textarea>
                        </div>
                    </div>
                </div>
                <div class="modal-footer border-0">
                    <button type="button" class="btn btn-light rounded-pill px-4" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary rounded-pill px-4">Generate & Issue</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
    // Simple client-side search
    document.getElementById('licenseSearch').addEventListener('keyup', function() {
        let val = this.value.toLowerCase();
        let rows = document.querySelectorAll('#licensesTable tbody tr');
        rows.forEach(row => {
            let text = row.innerText.toLowerCase();
            row.style.display = text.includes(val) ? '' : 'none';
        });
    });
</script>
</body>
</html>
