<?php
require_once 'auth.php';
ensureAdminAuth();
// admin/locations.php
require_once '../config/database.php';
require_once '../config/location_validator.php';

$database = new Database();
$db = $database->getConnection();
$validator = new LocationValidator($db);

// Handle form submissions
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    if(isset($_POST['action'])) {
        switch($_POST['action']) {
            case 'add_location':
                $query = "INSERT INTO locations (location_name, address) VALUES (:name, :address)";
                $stmt = $db->prepare($query);
                $stmt->execute([
                    ':name' => $_POST['location_name'],
                    ':address' => $_POST['address']
                ]);
                $location_id = $db->lastInsertId();
                
                // Add zip codes
                if(!empty($_POST['zip_codes'])) {
                    $zip_codes = explode(',', str_replace(' ', '', $_POST['zip_codes']));
                    $zip_query = "INSERT INTO zip_codes (location_id, zip_code) VALUES (:location_id, :zip_code)";
                    $zip_stmt = $db->prepare($zip_query);
                    
                    foreach($zip_codes as $zip) {
                        $zip_stmt->execute([
                            ':location_id' => $location_id,
                            ':zip_code' => $zip
                        ]);
                    }
                }
                break;
                
            case 'toggle_location':
                $query = "UPDATE locations SET is_active = NOT is_active WHERE id = :id";
                $stmt = $db->prepare($query);
                $stmt->execute([':id' => $_POST['location_id']]);
                break;
        }
    }
}

// Get all locations with their zip codes
$query = "SELECT l.*, 
          GROUP_CONCAT(z.zip_code ORDER BY z.zip_code) as zip_codes,
          COUNT(z.id) as zip_count
          FROM locations l
          LEFT JOIN zip_codes z ON l.id = z.location_id
          GROUP BY l.id
          ORDER BY l.location_name";
$locations = $db->query($query)->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage Locations - Admin</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<link rel="stylesheet" href="../assets/theme.css">
<link rel="stylesheet" href="../assets/portal-dark.css">
    <link rel="stylesheet" href="../assets/ushs-global-theme.css">
    <link rel="stylesheet" href="../assets/admin-sidebar.css">
</head>
<body>
<?php require_once __DIR__ . '/admin_sidebar.php'; render_admin_sidebar(); ?>
    <main class="main-content">
    <div class="container-fluid py-4">
        <h2 class="mb-4">Manage Service Locations</h2>
        
        <!-- Add Location Form -->
        <div class="card mb-4">
            <div class="card-header">
                <h5>Add New Location</h5>
            </div>
            <div class="card-body">
                <form method="POST">
                    <input type="hidden" name="action" value="add_location">
                    
                    <div class="mb-3">
                        <label class="form-label">Location Name</label>
                        <input type="text" name="location_name" class="form-control" required>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Address (Optional)</label>
                        <textarea name="address" class="form-control" rows="2"></textarea>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">ZIP Codes (comma-separated)</label>
                        <input type="text" name="zip_codes" class="form-control" 
                               placeholder="e.g., 30303, 30308, 30309" required>
                        <small class="text-muted">Enter ZIP codes separated by commas</small>
                    </div>
                    
                    <button type="submit" class="btn btn-primary">Add Location</button>
                </form>
            </div>
        </div>
        
        <!-- Locations List -->
        <div class="card">
            <div class="card-header">
                <h5>Service Locations</h5>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>Location</th>
                                <th>ZIP Codes</th>
                                <th>Count</th>
                                <th>Status</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach($locations as $location): ?>
                            <tr>
                                <td><?php echo $location['location_name']; ?></td>
                                <td><?php echo $location['zip_codes']; ?></td>
                                <td><?php echo $location['zip_count']; ?></td>
                                <td>
                                    <span class="badge bg-<?php echo $location['is_active'] ? 'success' : 'secondary'; ?>">
                                        <?php echo $location['is_active'] ? 'Active' : 'Inactive'; ?>
                                    </span>
                                </td>
                                <td>
                                    <form method="POST" style="display: inline;">
                                        <input type="hidden" name="action" value="toggle_location">
                                        <input type="hidden" name="location_id" value="<?php echo $location['id']; ?>">
                                        <button type="submit" class="btn btn-sm btn-warning">
                                            <?php echo $location['is_active'] ? 'Deactivate' : 'Activate'; ?>
                                        </button>
                                    </form>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    </main>
</body>
</html>