<?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';

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

$db = Database::getInstance();
$stmt = $db->prepare("
    SELECT *
    FROM offers
    WHERE id = :id AND deleted_at IS NULL
");
$stmt->execute(['id' => $id]);
$offer = $stmt->fetch();

if (!$offer) {
    header('Location: index.php');
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php includeFileWithVariables('../partials/title-meta.php', ['title' => 'View Offer']); ?>
<?php include '../partials/head-css.php'; ?>
</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' => 'CMS',
    'title'     => 'Offer Details'
]); ?>

<div class="card">
<div class="card-body">

<div class="row g-4">

<!-- ================= BASIC INFO ================= -->
<div class="col-md-6">
    <h6 class="text-muted">Title</h6>
    <p class="fw-semibold"><?= htmlspecialchars($offer['title']) ?></p>
</div>

<div class="col-md-6">
    <h6 class="text-muted">Status</h6>
    <span class="badge bg-<?= $offer['status'] === 'active' ? 'success' : 'secondary' ?>">
        <?= ucfirst($offer['status']) ?>
    </span>
</div>

<div class="col-md-6">
    <h6 class="text-muted">Offer Price</h6>
    <p>₹<?= number_format((float)$offer['offer_price'], 2) ?></p>
</div>

<div class="col-md-6">
    <h6 class="text-muted">Redirect URL</h6>
    <p>
        <a href="<?= htmlspecialchars($offer['redirect_url']) ?>" target="_blank">
            <?= htmlspecialchars($offer['redirect_url']) ?>
        </a>
    </p>
</div>

<!-- ================= DATES ================= -->
<div class="col-md-6">
    <h6 class="text-muted">Start Date</h6>
    <p><?= $offer['start_date'] ?></p>
</div>

<div class="col-md-6">
    <h6 class="text-muted">End Date</h6>
    <p><?= $offer['end_date'] ?></p>
</div>

<!-- ================= DESCRIPTIONS ================= -->
<div class="col-md-12">
    <h6 class="text-muted">Short Description</h6>
    <p><?= htmlspecialchars($offer['short_description']) ?></p>
</div>

<div class="col-md-12">
    <h6 class="text-muted">Description</h6>
    <div class="border rounded p-3">
        <?= nl2br(htmlspecialchars($offer['description'])) ?>
    </div>
</div>

<!-- ================= IMAGES ================= -->
<div class="col-md-6">
    <h6 class="text-muted">Desktop Image</h6>
    <img src="<?= $offer['desktop_image'] ?>"
         class="img-fluid rounded border">
</div>

<div class="col-md-6">
    <h6 class="text-muted">Mobile Image</h6>
    <img src="<?= $offer['mobile_image'] ?>"
         class="img-fluid rounded border">
</div>

<!-- ================= META ================= -->
<div class="col-md-6">
    <h6 class="text-muted">Created At</h6>
    <p><?= $offer['created_at'] ?></p>
</div>

<?php if (!empty($offer['updated_at'])): ?>
<div class="col-md-6">
    <h6 class="text-muted">Last Updated</h6>
    <p><?= $offer['updated_at'] ?></p>
</div>
<?php endif; ?>

</div>

</div>

<div class="card-footer text-end">
    <a href="<?= ADMIN_BASE_URL ?>offers/list.php"
       class="btn btn-light me-2">
        Back
    </a>
    <a href="<?= ADMIN_BASE_URL ?>offers/edit.php?id=<?= $offer['id'] ?>"
       class="btn btn-warning">
        <i class="bi bi-pencil-square me-1"></i> Edit
    </a>
</div>

</div>

</div>
</main>

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

<?php include '../partials/vendor-scripts.php'; ?>

</body>
</html>
