<?php
require_once '../config.php';

// Ensure session is started
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Check if user is logged in
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header('Location: ' . BASE_URL . '/admin/login');
    exit;
}

$conn = get_db_connection();
$message = '';
$message_type = '';

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $map_latitude = trim($_POST['map_latitude'] ?? '32.7766642');
    $map_longitude = trim($_POST['map_longitude'] ?? '-96.7969879');
    $map_zoom = intval($_POST['map_zoom'] ?? '9');
    $map_address_query = trim($_POST['map_address_query'] ?? '');

    $stmt = $conn->prepare("INSERT INTO map_settings (id, latitude, longitude, zoom, address_query) VALUES (1, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE latitude = ?, longitude = ?, zoom = ?, address_query = ?");
    $stmt->bind_param("ddisddis", $map_latitude, $map_longitude, $map_zoom, $map_address_query, $map_latitude, $map_longitude, $map_zoom, $map_address_query);

    if ($stmt->execute()) {
        $message = 'Location settings updated successfully!';
        $message_type = 'success';
    } else {
        $message = 'Error updating location settings: ' . $stmt->error;
        $message_type = 'danger';
    }
    $stmt->close();
}

// Get current map settings
$current_map = get_map_settings();

$page_title = 'Our Location';
require_once 'includes/admin-header.php';
?>

<?php if ($message): ?>
    <div class="alert alert-<?php echo $message_type; ?>">
        <i class="fas fa-<?php echo $message_type === 'success' ? 'check-circle' : 'exclamation-circle'; ?>"></i>
        <?php echo htmlspecialchars($message); ?>
    </div>
<?php endif; ?>

<div class="card">
    <div class="card-header">
        <h2 class="card-title">Google Maps Location Settings</h2>
    </div>

    <form method="POST" action="">
        <div class="dashboard-grid">
            <div class="left-col">
                <div class="form-row">
                    <div class="form-group" style="flex: 1;">
                        <label for="map_latitude">Latitude</label>
                        <input type="text" id="map_latitude" name="map_latitude" class="form-control"
                            value="<?php echo htmlspecialchars($current_map['latitude']); ?>" required>
                    </div>
                    <div class="form-group" style="flex: 1;">
                        <label for="map_longitude">Longitude</label>
                        <input type="text" id="map_longitude" name="map_longitude" class="form-control"
                            value="<?php echo htmlspecialchars($current_map['longitude']); ?>" required>
                    </div>
                </div>

                <div class="form-group">
                    <label for="map_zoom">Zoom Level (1-21)</label>
                    <input type="number" id="map_zoom" name="map_zoom" class="form-control" min="1" max="21"
                        value="<?php echo htmlspecialchars($current_map['zoom']); ?>" required>
                </div>

                <div class="form-group">
                    <label for="map_address_query">Address Query (Recommended)</label>
                    <p class="text-muted small mb-2">Google Maps will use this text to find the location. If empty, it
                        will use the coordinates above.</p>
                    <input type="text" id="map_address_query" name="map_address_query" class="form-control"
                        value="<?php echo htmlspecialchars($current_map['address_query']); ?>"
                        placeholder="e.g. 1234 Main St, City, ST">
                </div>

                <button type="submit" class="btn btn-primary" style="margin-top: 10px;">
                    <i class="fas fa-save"></i> Save Location Settings
                </button>
            </div>

            <div class="right-col">
                <div class="form-group">
                    <label>Map Preview</label>
                    <div
                        style="height: 350px; border-radius: var(--radius-md); overflow: hidden; border: 1px solid var(--border);">
                        <?php
                        $map_q = !empty($current_map['address_query']) ? $current_map['address_query'] : $current_map['latitude'] . ',' . $current_map['longitude'];
                        ?>
                        <iframe width="100%" height="100%" frameborder="0" style="border:0"
                            src="https://www.google.com/maps?q=<?php echo urlencode($map_q); ?>&hl=en&z=<?php echo $current_map['zoom']; ?>&output=embed"
                            allowfullscreen>
                        </iframe>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>

<?php require_once 'includes/admin-footer.php'; ?>