<?php
include 'header.php';
require_once '../db/config.php';

// Ensure the user is a logged-in restaurant owner
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'restaurant_owner') {
    header('Location: ../login.php');
    exit;
}

$owner_id = $_SESSION['user_id'];
$pdo = db();

// Find the restaurant ID managed by the owner
$stmt = $pdo->prepare("SELECT id FROM restaurants WHERE user_id = ?");
$stmt->execute([$owner_id]);
$restaurant = $stmt->fetch();

if (!$restaurant) {
    echo "<div class='alert alert-danger'>You are not associated with any restaurant.</div>";
    include 'footer.php';
    exit;
}
$restaurant_id = $restaurant['id'];

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $_POST['name'] ?? '';
    $cuisine = $_POST['cuisine'] ?? '';
    $address = $_POST['address'] ?? '';
    $phone_number = $_POST['phone_number'] ?? '';
    $image_url = $_POST['image_url'] ?? '';

    if ($name && $cuisine && $address) {
        $update_stmt = $pdo->prepare("UPDATE restaurants SET name = ?, cuisine = ?, address = ?, phone_number = ?, image_url = ? WHERE id = ? AND user_id = ?");
        $update_stmt->execute([$name, $cuisine, $address, $phone_number, $image_url, $restaurant_id, $owner_id]);
        
        // Redirect to the dashboard with a success message
        $_SESSION['success_message'] = "Your restaurant details have been updated successfully!";
        header('Location: index.php');
        exit;
    } else {
        $error = "Name, Cuisine, and Address are required fields.";
    }
}

// Fetch current restaurant details
$stmt = $pdo->prepare("SELECT * FROM restaurants WHERE id = ?");
$stmt->execute([$restaurant_id]);
$restaurant_details = $stmt->fetch();

?>

<div class="container mt-4">
    <h2>Edit Your Restaurant Details</h2>

    <?php if (isset($error)): ?>
        <div class="alert alert-danger"><?php echo $error; ?></div>
    <?php endif; ?>

    <form action="edit_restaurant.php" method="POST">
        <div class="mb-3">
            <label for="name" class="form-label">Restaurant Name</label>
            <input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($restaurant_details['name']); ?>" required>
        </div>
        <div class="mb-3">
            <label for="cuisine" class="form-label">Cuisine Type</label>
            <input type="text" class="form-control" id="cuisine" name="cuisine" value="<?php echo htmlspecialchars($restaurant_details['cuisine']); ?>" required>
        </div>
        <div class="mb-3">
            <label for="address" class="form-label">Address</label>
            <input type="text" class="form-control" id="address" name="address" value="<?php echo htmlspecialchars($restaurant_details['address']); ?>" required>
        </div>
        <div class="mb-3">
            <label for="phone_number" class="form-label">Phone Number</label>
            <input type="text" class="form-control" id="phone_number" name="phone_number" value="<?php echo htmlspecialchars($restaurant_details['phone_number']); ?>">
        </div>
        <div class="mb-3">
            <label for="image_url" class="form-label">Image URL</label>
            <input type="text" class="form-control" id="image_url" name="image_url" value="<?php echo htmlspecialchars($restaurant_details['image_url']); ?>">
            <small class="form-text text-muted">A URL to a publicly accessible image of your restaurant.</small>
        </div>
        <button type="submit" class="btn btn-primary">Save Changes</button>
        <a href="index.php" class="btn btn-secondary">Cancel</a>
    </form>
</div>

<?php include 'footer.php'; ?>
