<?php
declare(strict_types=1);

/* ================= AUTH ================= */
require_once __DIR__ . '/../security/auth_guard_web.php';
require_once __DIR__ . '/../partials/main.php';
require_once __DIR__ . '/../config/database.php';

/* ================= VALIDATION ================= */
$id = (int)($_GET['id'] ?? 0);
if ($id <= 0) {
    header('Location: index.php');
    exit;
}

$db = Database::getInstance();
$stmt = $db->prepare("SELECT * FROM hospitality_products WHERE id = :id");
$stmt->execute(['id' => $id]);
$product = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$product) {
    header('Location: index.php');
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php includeFileWithVariables('../partials/title-meta.php', ['title' => 'Edit Hospitality Product']); ?>

<link rel="stylesheet" href="<?= ADMIN_BASE_URL ?>assets/libs/quill/quill.snow.css">
<?php include '../partials/head-css.php'; ?>

<style>
.media-box{
    aspect-ratio:4/3;
    background:#f8f9fa;
    border:1px solid #dee2e6;
    border-radius:.5rem;
    overflow:hidden;
}
.media-box img{
    width:100%;
    height:100%;
    object-fit:cover;
}
</style>
</head>

<body>
<div id="layout-wrapper">
<?php include '../partials/header.php'; ?>
<?php include '../partials/sidebar.php'; ?>
<?php include '../partials/horizontal.php'; ?>

<main class="app-wrapper">
<div class="container-fluid">

<?php includeFileWithVariables('../partials/page-title.php', [
    'title_sub'=>'Hospitality',
    'title'=>'Edit Hospitality Product'
]); ?>

<form id="editForm" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?= $product['id'] ?>">

<!-- ================= IMAGES ================= -->
<div class="row g-4 mb-4">
<?php
$images = [
    'Cover Image'   => ['cover_image', $product['cover_image']],
    'Desktop Banner'=> ['banner_desktop', $product['banner_image_desktop']],
    'Mobile Banner' => ['banner_mobile', $product['banner_image_mobile']]
];
foreach ($images as $label => [$name, $src]): ?>
<div class="col-md-4">
<div class="card shadow-sm">
<div class="card-header"><strong><?= $label ?></strong></div>
<div class="card-body">
<div class="media-box mb-2">
<?php if ($src): ?><img src="<?= $src ?>"><?php endif; ?>
</div>
<input type="file" name="<?= $name ?>" class="form-control">
</div>
</div>
</div>
<?php endforeach; ?>
</div>

<!-- ================= PRODUCT DETAILS ================= -->
<div class="card shadow-sm mb-4">
<div class="card-header"><strong>Product Details</strong></div>
<div class="card-body">

<div class="row g-3">
<div class="col-md-6">
<label class="form-label">Title *</label>
<input name="title" class="form-control" value="<?= htmlspecialchars($product['title']) ?>" required>
</div>

<div class="col-md-6">
<label class="form-label">Slug</label>
<input class="form-control bg-light" value="<?= htmlspecialchars($product['slug']) ?>" readonly>
</div>

<div class="col-md-6">
<label class="form-label">Product Type</label>
<select name="product_type" class="form-select">
<?php foreach(['Hotel','Resort','Hospital','Hostel'] as $t): ?>
<option value="<?= $t ?>" <?= $product['product_type']===$t?'selected':'' ?>><?= $t ?></option>
<?php endforeach; ?>
</select>
</div>

<div class="col-md-6">
<label class="form-label">Usage Category</label>
<select name="usage_category" class="form-select">
<option value="">--</option>
<?php foreach(['Luxury','Premium','Standard','Economy'] as $u): ?>
<option value="<?= $u ?>" <?= $product['usage_category']===$u?'selected':'' ?>><?= $u ?></option>
<?php endforeach; ?>
</select>
</div>

<div class="col-12">
<label class="form-label">Short Description</label>
<textarea name="short_description" class="form-control" rows="3"><?= htmlspecialchars($product['short_description']) ?></textarea>
</div>

<div class="col-12">
<label class="form-label">Full Description</label>
<div id="editor" style="height:300px"><?= $product['description'] ?></div>
</div>

<div class="col-md-3">
<label class="form-label">Priority</label>
<input type="number" name="priority" class="form-control" value="<?= (int)$product['priority'] ?>">
</div>

<div class="col-md-3">
<label class="form-label">Status</label>
<select name="status" class="form-select">
<?php foreach(['Draft','Active','InActive'] as $s): ?>
<option value="<?= $s ?>" <?= $product['status']===$s?'selected':'' ?>><?= $s ?></option>
<?php endforeach; ?>
</select>
</div>

<div class="col-md-3">
<label class="form-label">Approval</label>
<select name="approval_status" class="form-select">
<?php foreach(['Pending','Approved','Rejected'] as $a): ?>
<option value="<?= $a ?>" <?= $product['approval_status']===$a?'selected':'' ?>><?= $a ?></option>
<?php endforeach; ?>
</select>
</div>
</div>

</div>
</div>

<!-- ================= SEO ================= -->
<div class="card shadow-sm mb-4">
<div class="card-header"><strong>SEO & Schema</strong></div>
<div class="card-body row g-3">

<div class="col-md-6">
<input name="meta_title" class="form-control" placeholder="Meta Title" value="<?= htmlspecialchars($product['meta_title']) ?>">
</div>

<div class="col-md-6">
<input name="canonical_url" class="form-control" placeholder="Canonical URL" value="<?= htmlspecialchars($product['canonical_url']) ?>">
</div>

<div class="col-12">
<textarea name="meta_description" class="form-control" rows="3" placeholder="Meta Description"><?= htmlspecialchars($product['meta_description']) ?></textarea>
</div>

<div class="col-12">
<textarea name="schema_json" class="form-control font-monospace" rows="5" placeholder="Schema JSON"><?= htmlspecialchars($product['schema_json']) ?></textarea>
</div>

</div>
</div>

<!-- ================= ACTIONS ================= -->
<div class="card">
<div class="card-body d-flex justify-content-between">
<a href="index.php" class="btn btn-light">Cancel</a>
<button class="btn btn-primary">
<i class="bi bi-save me-1"></i> Save Changes
</button>
</div>
</div>

</form>

</div>
</main>

<?php include '../partials/footer.php'; ?>
</div>

<!-- ================= SUCCESS MODAL ================= -->
<div class="modal fade" id="successModal">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-body text-center">
<i class="bi bi-check-circle text-success fs-1"></i>
<p class="mt-3 mb-0">Product updated successfully</p>
</div>
</div>
</div>
</div>

<?php include '../partials/vendor-scripts.php'; ?>
<script src="<?= ADMIN_BASE_URL ?>assets/libs/quill/quill.js"></script>

<script>
const quill = new Quill('#editor', { theme:'snow' });
const API_UPDATE = "<?= ADMIN_BASE_URL ?>api/hospitality-products/update.php";

document.getElementById('editForm').addEventListener('submit', async e => {
    e.preventDefault();
    const fd = new FormData(e.target);
    fd.append('description', quill.root.innerHTML.trim());

    const r = await fetch(API_UPDATE, {
        method:'POST',
        credentials:'include',
        body:fd
    }).then(r=>r.json());

    if(r.status==='success'){
        new bootstrap.Modal(document.getElementById('successModal')).show();
        setTimeout(()=>location.href='list.php',1500);
    }else{
        alert(r.message || 'Update failed');
    }
});
</script>

</body>
</html>
