<?php require_once('header.php'); ?>

<?php
if (isset($_GET['id'])) {
    $id = $_GET['id'];
    $statement = $pdo->prepare("SELECT * FROM tbl_locations WHERE id = ?");
    $statement->execute([$id]);
    $location = $statement->fetch(PDO::FETCH_ASSOC);

    if (!$location) {
        header("Location: location.php");
        exit;
    }

    if (isset($_POST['form1'])) {
        $valid = 1;
        $error_message = '';
        $success_message = '';

        $location_name = htmlspecialchars($_POST['location_name']);
        $location_description = htmlspecialchars($_POST['location_description']);
        $description = htmlspecialchars($_POST['description']);
        $slug = htmlspecialchars($_POST['slug']);
        $map = htmlspecialchars($_POST['map']);
        $is_active = isset($_POST['is_active']) ? 1 : 0;

        $image = $_FILES['image']['name'];
        $image_tmp = $_FILES['image']['tmp_name'];
        $final_image_name = $location['image'];

        if ($image != '') {
            $ext = pathinfo($image, PATHINFO_EXTENSION);
            if (!in_array(strtolower($ext), ['jpg', 'jpeg', 'png', 'gif', 'webp'])) {
                $valid = 0;
                $error_message .= 'You must upload a jpg, jpeg, png, gif, or webp image for the location.<br>';
            } else {
                $ai_id = uniqid();
                $final_image_name = 'location-' . $ai_id . '.' . $ext;
                move_uploaded_file($image_tmp, 'assets/images/location-images/' . $final_image_name);
            }
        }

        if ($valid == 1) {
            $statement = $pdo->prepare("UPDATE tbl_locations SET location_name = ?, location_description = ?, description = ?, image = ?, map = ?, slug = ?, is_active = ? WHERE id = ?");
            $statement->execute([$location_name, $location_description, $description, $final_image_name, $map, $slug, $is_active, $id]);

            $success_message = 'Location updated successfully.';
        }
    }
}
?>

<section class="content-header">
    <div class="content-header-left">
        <h1>Edit Location</h1>
    </div>
    <div class="content-header-right">
        <a href="location.php" class="btn btn-primary btn-sm">View All Locations</a>
    </div>
</section>

<section class="content">
    <div class="row">
        <div class="col-md-12">
            <?php if ($error_message): ?>
            <div class="callout callout-danger">
                <p><?php echo $error_message; ?></p>
            </div>
            <?php endif; ?>
            <?php if ($success_message): ?>
            <div class="callout callout-success">
                <p><?php echo $success_message; ?></p>
            </div>
            <?php endif; ?>
            <form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
                <div class="box box-info">
                    <div class="box-body">
                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label">Location Name <span>*</span></label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="location_name" value="<?php echo $location['location_name']; ?>" required>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label">Location Description <span>*</span></label>
                            <div class="col-sm-6">
                                <textarea class="form-control" name="location_description" rows="5" required><?php echo $location['location_description']; ?></textarea>
                            </div>
                        </div>
                         <div class="form-group">
                            <label for="" class="col-sm-2 control-label"> Description <span>*</span></label>
                            <div class="col-sm-6">
                                <textarea class="form-control" name="description" id="editor1" rows="5" required><?php echo $location['description']; ?></textarea>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label">Location Image</label>
                            <div class="col-sm-4">
                                <input type="file" class="form-control" name="image">
                            </div>
                        </div>

                        <!-- <div class="form-group">
                            <label for="" class="col-sm-2 control-label">Google Map Embed Code (Optional)</label>
                            <div class="col-sm-6">
                                <textarea class="form-control" name="map" rows="3" placeholder="Paste Google Map Embed code here"><?php echo $location['map']; ?></textarea>
                            </div>
                        </div> -->

                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label">Slug (URL-friendly) <span>*</span></label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="slug" value="<?php echo $location['slug']; ?>" required>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label">Status</label>
                            <div class="col-sm-2">
                                <input type="checkbox" name="is_active" value="1" <?php echo $location['is_active'] ? 'checked' : ''; ?>>
                                <span>Is Active ?</span>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label"></label>
                            <div class="col-sm-6">
                                <button type="submit" class="btn btn-success pull-left" name="form1">Update</button>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</section>

<?php require_once('footer.php'); ?>
