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

$sectors = getData('sectors');

// Handle add
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['name']) && empty($_POST['edit_id'])) {
    $sectors[] = [
        'id'          => generateId($sectors),
        'icon'        => trim($_POST['icon'] ?? '🏭'),
        'name'        => trim($_POST['name'] ?? ''),
        'description' => trim($_POST['description'] ?? ''),
    ];
    saveData('sectors', $sectors);
    setFlash('success', 'Sector added');
    header('Location: sectors.php');
    exit;
}

// Handle edit
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['edit_id'])) {
    foreach ($sectors as &$s) {
        if ($s['id'] == $_POST['edit_id']) {
            $s['icon'] = trim($_POST['icon'] ?? $s['icon']);
            $s['name'] = trim($_POST['name'] ?? $s['name']);
            $s['description'] = trim($_POST['description'] ?? $s['description']);
            break;
        }
    }
    saveData('sectors', $sectors);
    setFlash('success', 'Sector updated');
    header('Location: sectors.php');
    exit;
}

// Handle delete
if (!empty($_GET['delete'])) {
    $sectors = array_values(array_filter($sectors, fn($s) => $s['id'] != $_GET['delete']));
    saveData('sectors', $sectors);
    setFlash('success', 'Sector deleted');
    header('Location: sectors.php');
    exit;
}

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

<div class="admin-header">
    <h1 class="admin-title">Sectors</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 Sector</h3>
    <form method="POST" class="row g-3">
        <div class="col-md-2">
            <input type="text" name="icon" class="form-control" placeholder="Icon (emoji)" value="🏭" style="background:var(--bg2);color:var(--text);border-color:var(--border);font-size:1rem;text-align:center;">
        </div>
        <div class="col-md-3">
            <input type="text" name="name" class="form-control" placeholder="Sector Name *" required style="background:var(--bg2);color:var(--text);border-color:var(--border);">
        </div>
        <div class="col-md-5">
            <input type="text" name="description" class="form-control" placeholder="Description" style="background:var(--bg2);color:var(--text);border-color:var(--border);">
        </div>
        <div class="col-md-2">
            <button type="submit" class="btn-admin-primary w-100" style="font-size:0.8rem;padding:0.5rem;">Add</button>
        </div>
    </form>
</div>

<!-- Sectors List -->
<div class="admin-card">
    <?php if (!empty($sectors)): ?>
    <div class="table-responsive">
        <table class="admin-table">
            <thead>
                <tr>
                    <th>Icon</th>
                    <th>Name</th>
                    <th>Description</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($sectors as $sector): ?>
                <tr>
                    <td style="font-size:1.5rem;"><?php echo $sector['icon']; ?></td>
                    <td><strong style="font-size:0.88rem;"><?php echo e($sector['name']); ?></strong></td>
                    <td style="font-size:0.82rem;color:var(--muted);"><?php echo e(truncateText($sector['description'], 60)); ?></td>
                    <td>
                        <button class="btn-admin-outline" style="font-size:0.7rem;padding:0.3rem 0.6rem;" data-bs-toggle="collapse" data-bs-target="#edit<?php echo $sector['id']; ?>">Edit</button>
                        <a href="sectors.php?delete=<?php echo e($sector['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 sector?');">Delete</a>
                    </td>
                </tr>
                <tr>
                    <td colspan="4" class="p-0">
                        <div class="collapse" id="edit<?php echo $sector['id']; ?>">
                            <div style="padding:1rem;background:var(--bg2);border-top:1px solid var(--border);">
                                <form method="POST" class="row g-3 align-items-end">
                                    <input type="hidden" name="edit_id" value="<?php echo e($sector['id']); ?>">
                                    <div class="col-md-2">
                                        <input type="text" name="icon" class="form-control" value="<?php echo e($sector['icon']); ?>" style="background:var(--bg);color:var(--text);border-color:var(--border);font-size:1rem;text-align:center;">
                                    </div>
                                    <div class="col-md-3">
                                        <input type="text" name="name" class="form-control" value="<?php echo e($sector['name']); ?>" required style="background:var(--bg);color:var(--text);border-color:var(--border);">
                                    </div>
                                    <div class="col-md-5">
                                        <input type="text" name="description" class="form-control" value="<?php echo e($sector['description']); ?>" style="background:var(--bg);color:var(--text);border-color:var(--border);">
                                    </div>
                                    <div class="col-md-2">
                                        <button type="submit" class="btn-admin-primary w-100" style="font-size:0.8rem;padding:0.5rem;">Save</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <p style="color:var(--muted);text-align:center;padding:2rem;">No sectors yet. Add your first sector above.</p>
    <?php endif; ?>
</div>

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