<?php
require_once("../php/db_connect.php");

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

if (!isset($_SESSION["user_id"]) || ($_SESSION["role"] ?? "") !== "admin") {
    header("Location: index.php");
    exit();
}

$shop_id = (int)$_SESSION["user_id"];

$stmt = $conn->prepare("
    SELECT *
    FROM services
    WHERE owner_shop_id = ?
      AND status = 'archived'
    ORDER BY id DESC
");

$stmt->bind_param("i", $shop_id);
$stmt->execute();
$archivedServices = $stmt->get_result();
?>

<div class="container-fluid px-0 archived-services-page">

    <div class="d-flex justify-content-between align-items-center flex-wrap gap-3 mb-4">
        <div>
            <h1 class="fw-bold mb-1">Archived Services</h1>
            <p class="text-muted mb-0">View archived services from your shop.</p>
        </div>

        <a href="admin.php?page=services" class="btn btn-outline-dark rounded-pill px-4">
            Back to Services
        </a>
    </div>

    <div class="card border-0 shadow-sm rounded-4 archived-table-card">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0 archived-table">
                    <thead>
                        <tr>
                            <th>Service Name</th>
                            <th>Service Type</th>
                            <th>Status</th>
                            <th>Action</th>
                        </tr>
                    </thead>

                    <tbody>
                        <?php if ($archivedServices && $archivedServices->num_rows > 0): ?>
                            <?php while ($row = $archivedServices->fetch_assoc()): ?>
                                <tr>
                                    <td class="fw-semibold">
                                        <?php echo htmlspecialchars($row["service_name"] ?? "N/A"); ?>
                                    </td>

                                    <td>
                                        <?php echo htmlspecialchars($row["service_type"] ?? "N/A"); ?>
                                    </td>

                                    <td>
                                        <span class="badge bg-secondary-subtle text-secondary rounded-pill px-3 py-2">
                                            Archived
                                        </span>
                                    </td>

                                    <td>
                                        <a 
                                            href="restore-service.php?id=<?php echo (int)$row['id']; ?>" 
                                            class="btn btn-success btn-sm"
                                            onclick="return confirm('Restore this service?');"
                                        >
                                            Restore
                                        </a>
                                    </td>
                                </tr>
                            <?php endwhile; ?>
                        <?php else: ?>
                            <tr>
                                <td colspan="4">
                                    <div class="empty-state">
                                        <i class="fa-solid fa-box-archive"></i>
                                        <p class="mb-0">No archived services found.</p>
                                    </div>
                                </td>
                            </tr>
                        <?php endif; ?>
                    </tbody>

                </table>
            </div>
        </div>
    </div>

</div>

<style>
.archived-services-page {
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
}

.archived-services-page h1 {
    font-size: clamp(26px, 2.2vw, 38px);
    color: #111827;
}

.archived-table-card {
    overflow: hidden;
}

.archived-table {
    min-width: 700px;
}

.archived-table thead th {
    background: #f8fafc;
    color: #111827;
    font-size: 14px;
    font-weight: 700;
    padding: 16px;
    white-space: nowrap;
    border-bottom: 1px solid #e5e7eb;
}

.archived-table tbody td {
    padding: 16px;
    font-size: 14px;
    color: #1f2937;
    border-bottom: 1px solid #f0f0f0;
    white-space: nowrap;
}

.empty-state {
    padding: 40px 10px;
    text-align: center;
    color: #6b7280;
}

.empty-state i {
    font-size: 34px;
    color: #9ca3af;
    margin-bottom: 10px;
}
</style>