<?php
require_once __DIR__ . '/auth-check.php';
require_once __DIR__ . '/../includes/functions.php';

$exhibitors = getData('exhibitors');

// Handle add
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['company_name'])) {
    $newEx = [
        'id'           => generateId($exhibitors),
        'company_name' => trim($_POST['company_name'] ?? ''),
        'country'      => trim($_POST['country'] ?? ''),
        'website'      => trim($_POST['website'] ?? ''),
        'logo'         => '',
    ];

    // Handle logo upload
    if (!empty($_FILES['logo']['tmp_name'])) {
        $ext = pathinfo($_FILES['logo']['name'], PATHINFO_EXTENSION);
        $filename = 'exhibitor_' . $newEx['id'] . '_' . time() . '.' . $ext;
        $dest = UPLOADS_PATH . '/logos/' . $filename;
        if (move_uploaded_file($_FILES['logo']['tmp_name'], $dest)) {
            $newEx['logo'] = $filename;
        }
    }

    $exhibitors[] = $newEx;
    saveData('exhibitors', $exhibitors);
    setFlash('success', 'Exhibitor added successfully');
    header('Location: exhibitors.php');
    exit;
}

// Handle delete
if (!empty($_GET['delete'])) {
    $exhibitors = array_filter($exhibitors, function($ex) {
        if ($ex['id'] == ($_GET['delete'] ?? 0)) {
            if (!empty($ex['logo'])) {
                @unlink(UPLOADS_PATH . '/logos/' . $ex['logo']);
            }
            return false;
        }
        return true;
    });
    saveData('exhibitors', array_values($exhibitors));
    setFlash('success', 'Exhibitor deleted');
    header('Location: exhibitors.php');
    exit;
}

$pageTitle = 'Exhibitors';
include __DIR__ . '/sidebar.php';
?>

<div class="admin-header">
    <h1 class="admin-title">Exhibitors</h1>
</div>

<!-- Add Form -->
<div class="admin-card mb-4">
    <h3 style="font-family:var(--font-heading);font-size:0.9rem;text-transform:uppercase;color:var(--text);margin-bottom:1rem;">Add Exhibitor</h3>
    <form method="POST" enctype="multipart/form-data">
        <div class="row g-3">
            <div class="col-md-4">
                <input type="text" name="company_name" class="form-control" placeholder="Company Name *" required style="background:var(--bg2);color:var(--text);border-color:var(--border);">
            </div>
            <div class="col-md-3">
                <input type="text" name="country" class="form-control" placeholder="Country" style="background:var(--bg2);color:var(--text);border-color:var(--border);">
            </div>
            <div class="col-md-3">
                <input type="url" name="website" class="form-control" placeholder="Website" style="background:var(--bg2);color:var(--text);border-color:var(--border);">
            </div>
            <div class="col-md-2">
                <input type="file" name="logo" class="form-control" accept="image/*" style="background:var(--bg2);color:var(--text);border-color:var(--border);font-size:0.8rem;">
            </div>
            <div class="col-12">
                <button type="submit" class="btn-admin-primary" style="font-size:0.8rem;padding:0.5rem 1rem;">Add Exhibitor</button>
            </div>
        </div>
    </form>
</div>

<!-- Exhibitors List -->
<div class="admin-card">
    <?php if (!empty($exhibitors)): ?>
    <div class="table-responsive">
        <table class="admin-table">
            <thead>
                <tr>
                    <th>Logo</th>
                    <th>Company</th>
                    <th>Country</th>
                    <th>Website</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($exhibitors as $ex):
                    $logoUrl = !empty($ex['logo']) ? UPLOADS_URL . '/logos/' . $ex['logo'] : '';
                ?>
                <tr>
                    <td style="width:60px;">
                        <?php if ($logoUrl): ?>
                        <img src="<?php echo $logoUrl; ?>" alt="" style="width:50px;height:40px;object-fit:contain;background:var(--bg2);border-radius:4px;">
                        <?php else: ?>
                        <div style="width:50px;height:40px;background:var(--bg2);border-radius:4px;display:flex;align-items:center;justify-content:center;font-size:0.65rem;color:var(--muted);">NO LOGO</div>
                        <?php endif; ?>
                    </td>
                    <td><strong style="font-size:0.88rem;"><?php echo e($ex['company_name'] ?? ''); ?></strong></td>
                    <td style="font-size:0.82rem;color:var(--muted);"><?php echo e($ex['country'] ?? ''); ?></td>
                    <td>
                        <?php if (!empty($ex['website'])): ?>
                        <a href="<?php echo e($ex['website']); ?>" target="_blank" style="color:var(--green2);font-size:0.8rem;text-decoration:none;"><?php echo e($ex['website']); ?></a>
                        <?php else: ?><span style="color:var(--muted);font-size:0.8rem;">-</span><?php endif; ?>
                    </td>
                    <td>
                        <a href="exhibitors.php?delete=<?php echo e($ex['id']); ?>" class="btn-admin-outline" style="font-size:0.7rem;padding:0.3rem 0.6rem;color:var(--danger);border-color:var(--danger);" onclick="return confirm('Delete this exhibitor?');">Delete</a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <p style="color:var(--muted);text-align:center;padding:2rem;">No exhibitors yet. Add your first exhibitor above.</p>
    <?php endif; ?>
</div>

<?php include __DIR__ . '/../includes/footer.php'; ?>
