<?php
/**
 * Pure Chemical Admin
 * Product Specifications Manager
 *
 * File:
 * admin/product_specifications.php
 *
 * Uses existing tables:
 * - products
 * - product_specifications
 *
 * No new table required.
 */

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

/* ---------------------------------------------------------
   CORE INCLUDES
--------------------------------------------------------- */

$baseDir = __DIR__;

require_once $baseDir . '/includes/db.php';

if (file_exists($baseDir . '/includes/auth.php')) {
    require_once $baseDir . '/includes/auth.php';
}

if (file_exists($baseDir . '/includes/functions.php')) {
    require_once $baseDir . '/includes/functions.php';
}

if (file_exists($baseDir . '/includes/csrf.php')) {
    require_once $baseDir . '/includes/csrf.php';
}

/* ---------------------------------------------------------
   AUTH FALLBACK
--------------------------------------------------------- */

if (function_exists('requireAdmin')) {
    requireAdmin();
} else {
    if (
        empty($_SESSION['admin_id']) &&
        empty($_SESSION['admin_logged_in'])
    ) {
        header('Location: login.php');
        exit;
    }
}

/* ---------------------------------------------------------
   HELPERS
--------------------------------------------------------- */

if (!function_exists('e')) {
    function e($value): string
    {
        return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
    }
}

if (!function_exists('csrf_field')) {
    function csrf_field(): string
    {
        if (empty($_SESSION['_csrf_token'])) {
            $_SESSION['_csrf_token'] = bin2hex(random_bytes(32));
        }

        return '<input type="hidden" name="csrf_token" value="' .
            e($_SESSION['_csrf_token']) .
            '">';
    }
}

if (!function_exists('csrf_verify')) {
    function csrf_verify(): bool
    {
        if (empty($_POST['csrf_token']) || empty($_SESSION['_csrf_token'])) {
            return false;
        }

        return hash_equals(
            $_SESSION['_csrf_token'],
            $_POST['csrf_token']
        );
    }
}

/* ---------------------------------------------------------
   VARIABLES
--------------------------------------------------------- */

$page_title = 'Product Specifications';
$active_menu = 'product_specifications';

$message = '';
$error = '';

$editSpecification = null;

/* ---------------------------------------------------------
   CSRF CHECK
--------------------------------------------------------- */

function verifyPostCsrf(): bool
{
    if (function_exists('csrf_verify')) {
        return csrf_verify();
    }

    return !empty($_POST['csrf_token']) &&
        !empty($_SESSION['_csrf_token']) &&
        hash_equals(
            $_SESSION['_csrf_token'],
            $_POST['csrf_token']
        );
}

/* ---------------------------------------------------------
   DELETE
--------------------------------------------------------- */

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_id'])) {

    if (!verifyPostCsrf()) {
        $error = 'Security verification failed. Please refresh the page and try again.';
    } else {

        $deleteId = (int)$_POST['delete_id'];

        if ($deleteId > 0) {
            try {

                $stmt = $pdo->prepare("
                    DELETE FROM product_specifications
                    WHERE id = ?
                    LIMIT 1
                ");

                $stmt->execute([$deleteId]);

                if ($stmt->rowCount() > 0) {
                    $message = 'Specification deleted successfully.';
                } else {
                    $error = 'Specification not found.';
                }

            } catch (PDOException $e) {
                error_log('Product Specification Delete Error: ' . $e->getMessage());
                $error = 'Unable to delete specification.';
            }
        }
    }
}

/* ---------------------------------------------------------
   SAVE / UPDATE
--------------------------------------------------------- */

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_specification'])) {

    if (!verifyPostCsrf()) {
        $error = 'Security verification failed. Please refresh the page and try again.';
    } else {

        $id = (int)($_POST['id'] ?? 0);

        $productId = (int)($_POST['product_id'] ?? 0);

        $specificationGroup = trim(
            $_POST['specification_group'] ?? ''
        );

        $specificationKey = trim(
            $_POST['specification_key'] ?? ''
        );

        $specificationValue = trim(
            $_POST['specification_value'] ?? ''
        );

        $unit = trim(
            $_POST['unit'] ?? ''
        );

        $sortOrder = (int)($_POST['sort_order'] ?? 0);

        if ($productId <= 0) {
            $error = 'Please select a product.';
        } elseif ($specificationKey === '') {
            $error = 'Specification key is required.';
        } else {

            try {

                /* ---------------------------------------------
                   PRODUCT EXISTS
                --------------------------------------------- */

                $productCheck = $pdo->prepare("
                    SELECT id
                    FROM products
                    WHERE id = ?
                    LIMIT 1
                ");

                $productCheck->execute([$productId]);

                if (!$productCheck->fetch()) {
                    throw new RuntimeException('Selected product does not exist.');
                }

                /* ---------------------------------------------
                   DUPLICATE CHECK
                --------------------------------------------- */

                if ($id > 0) {

                    $duplicateStmt = $pdo->prepare("
                        SELECT id
                        FROM product_specifications
                        WHERE product_id = ?
                          AND specification_key = ?
                          AND (
                              specification_group = ?
                              OR (
                                  specification_group IS NULL
                                  AND ? = ''
                              )
                          )
                          AND id != ?
                        LIMIT 1
                    ");

                    $duplicateStmt->execute([
                        $productId,
                        $specificationKey,
                        $specificationGroup,
                        $specificationGroup,
                        $id
                    ]);

                } else {

                    $duplicateStmt = $pdo->prepare("
                        SELECT id
                        FROM product_specifications
                        WHERE product_id = ?
                          AND specification_key = ?
                          AND (
                              specification_group = ?
                              OR (
                                  specification_group IS NULL
                                  AND ? = ''
                              )
                          )
                        LIMIT 1
                    ");

                    $duplicateStmt->execute([
                        $productId,
                        $specificationKey,
                        $specificationGroup,
                        $specificationGroup
                    ]);
                }

                if ($duplicateStmt->fetch()) {
                    throw new RuntimeException(
                        'This specification key already exists for the selected product and group.'
                    );
                }

                /* ---------------------------------------------
                   EMPTY OPTIONAL VALUES -> NULL
                --------------------------------------------- */

                $groupValue = $specificationGroup !== ''
                    ? $specificationGroup
                    : null;

                $valueValue = $specificationValue !== ''
                    ? $specificationValue
                    : null;

                $unitValue = $unit !== ''
                    ? $unit
                    : null;

                /* ---------------------------------------------
                   UPDATE
                --------------------------------------------- */

                if ($id > 0) {

                    $stmt = $pdo->prepare("
                        UPDATE product_specifications
                        SET
                            product_id = ?,
                            specification_group = ?,
                            specification_key = ?,
                            specification_value = ?,
                            unit = ?,
                            sort_order = ?
                        WHERE id = ?
                        LIMIT 1
                    ");

                    $stmt->execute([
                        $productId,
                        $groupValue,
                        $specificationKey,
                        $valueValue,
                        $unitValue,
                        $sortOrder,
                        $id
                    ]);

                    $message = 'Product specification updated successfully.';

                } else {

                    /* -----------------------------------------
                       INSERT
                    ----------------------------------------- */

                    $stmt = $pdo->prepare("
                        INSERT INTO product_specifications
                        (
                            product_id,
                            specification_group,
                            specification_key,
                            specification_value,
                            unit,
                            sort_order
                        )
                        VALUES
                        (?, ?, ?, ?, ?, ?)
                    ");

                    $stmt->execute([
                        $productId,
                        $groupValue,
                        $specificationKey,
                        $valueValue,
                        $unitValue,
                        $sortOrder
                    ]);

                    $message = 'Product specification added successfully.';
                }

            } catch (RuntimeException $e) {
                $error = $e->getMessage();

            } catch (PDOException $e) {
                error_log(
                    'Product Specification Save Error: ' .
                    $e->getMessage()
                );

                $error = 'Unable to save product specification.';
            }
        }
    }
}

/* ---------------------------------------------------------
   EDIT RECORD
--------------------------------------------------------- */

if (isset($_GET['edit'])) {

    $editId = (int)$_GET['edit'];

    if ($editId > 0) {

        try {

            $stmt = $pdo->prepare("
                SELECT
                    ps.*,
                    p.name AS product_name,
                    p.slug AS product_slug
                FROM product_specifications ps
                INNER JOIN products p
                    ON p.id = ps.product_id
                WHERE ps.id = ?
                LIMIT 1
            ");

            $stmt->execute([$editId]);

            $editSpecification = $stmt->fetch();

            if (!$editSpecification) {
                $error = 'Specification not found.';
            }

        } catch (PDOException $e) {
            error_log(
                'Product Specification Edit Error: ' .
                $e->getMessage()
            );

            $error = 'Unable to load specification.';
        }
    }
}

/* ---------------------------------------------------------
   PRODUCTS
--------------------------------------------------------- */

$products = [];

try {

    $productStmt = $pdo->query("
        SELECT
            id,
            name,
            slug,
            product_code
        FROM products
        ORDER BY name ASC
    ");

    $products = $productStmt->fetchAll();

} catch (PDOException $e) {

    error_log(
        'Product Specification Product List Error: ' .
        $e->getMessage()
    );

    $error = 'Unable to load products.';
}

/* ---------------------------------------------------------
   FILTERS
--------------------------------------------------------- */

$search = trim($_GET['search'] ?? '');

$filterProduct = (int)($_GET['product_id'] ?? 0);

$filterGroup = trim($_GET['group'] ?? '');

$page = max(1, (int)($_GET['page'] ?? 1));

$perPage = 12;

$offset = ($page - 1) * $perPage;

/* ---------------------------------------------------------
   GROUP LIST
--------------------------------------------------------- */

$groups = [];

try {

    $groupStmt = $pdo->query("
        SELECT DISTINCT specification_group
        FROM product_specifications
        WHERE specification_group IS NOT NULL
          AND specification_group != ''
        ORDER BY specification_group ASC
    ");

    $groups = $groupStmt->fetchAll();

} catch (PDOException $e) {

    $groups = [];
}

/* ---------------------------------------------------------
   STATISTICS
--------------------------------------------------------- */

$stats = [
    'total' => 0,
    'products' => 0,
    'groups' => 0,
    'with_unit' => 0
];

try {

    $stats['total'] = (int)$pdo
        ->query("
            SELECT COUNT(*)
            FROM product_specifications
        ")
        ->fetchColumn();

    $stats['products'] = (int)$pdo
        ->query("
            SELECT COUNT(DISTINCT product_id)
            FROM product_specifications
        ")
        ->fetchColumn();

    $stats['groups'] = (int)$pdo
        ->query("
            SELECT COUNT(DISTINCT specification_group)
            FROM product_specifications
            WHERE specification_group IS NOT NULL
              AND specification_group != ''
        ")
        ->fetchColumn();

    $stats['with_unit'] = (int)$pdo
        ->query("
            SELECT COUNT(*)
            FROM product_specifications
            WHERE unit IS NOT NULL
              AND unit != ''
        ")
        ->fetchColumn();

} catch (PDOException $e) {
    /* Keep defaults */
}

/* ---------------------------------------------------------
   BUILD FILTER QUERY
--------------------------------------------------------- */

$where = [];

$params = [];

if ($search !== '') {

    $where[] = "
        (
            ps.specification_key LIKE ?
            OR ps.specification_value LIKE ?
            OR ps.specification_group LIKE ?
            OR p.name LIKE ?
            OR p.product_code LIKE ?
        )
    ";

    $searchLike = '%' . $search . '%';

    $params[] = $searchLike;
    $params[] = $searchLike;
    $params[] = $searchLike;
    $params[] = $searchLike;
    $params[] = $searchLike;
}

if ($filterProduct > 0) {

    $where[] = "ps.product_id = ?";

    $params[] = $filterProduct;
}

if ($filterGroup !== '') {

    $where[] = "ps.specification_group = ?";

    $params[] = $filterGroup;
}

$whereSql = '';

if (!empty($where)) {
    $whereSql = 'WHERE ' . implode(' AND ', $where);
}

/* ---------------------------------------------------------
   TOTAL FILTERED RECORDS
--------------------------------------------------------- */

$totalFiltered = 0;

try {

    $countSql = "
        SELECT COUNT(*)
        FROM product_specifications ps
        INNER JOIN products p
            ON p.id = ps.product_id
        {$whereSql}
    ";

    $countStmt = $pdo->prepare($countSql);

    $countStmt->execute($params);

    $totalFiltered = (int)$countStmt->fetchColumn();

} catch (PDOException $e) {

    error_log(
        'Product Specification Count Error: ' .
        $e->getMessage()
    );
}

$totalPages = max(
    1,
    (int)ceil($totalFiltered / $perPage)
);

if ($page > $totalPages) {
    $page = $totalPages;
    $offset = ($page - 1) * $perPage;
}

/* ---------------------------------------------------------
   FETCH SPECIFICATIONS
--------------------------------------------------------- */

$specifications = [];

try {

    $sql = "
        SELECT
            ps.id,
            ps.product_id,
            ps.specification_group,
            ps.specification_key,
            ps.specification_value,
            ps.unit,
            ps.sort_order,
            ps.created_at,

            p.name AS product_name,
            p.slug AS product_slug,
            p.product_code

        FROM product_specifications ps

        INNER JOIN products p
            ON p.id = ps.product_id

        {$whereSql}

        ORDER BY
            p.name ASC,
            COALESCE(ps.specification_group, '') ASC,
            ps.sort_order ASC,
            ps.id DESC

        LIMIT {$perPage} OFFSET {$offset}
    ";

    $stmt = $pdo->prepare($sql);

    $stmt->execute($params);

    $specifications = $stmt->fetchAll();

} catch (PDOException $e) {

    error_log(
        'Product Specification List Error: ' .
        $e->getMessage()
    );

    $error = 'Unable to load product specifications.';
}

/* ---------------------------------------------------------
   HEADER
--------------------------------------------------------- */

$headerPath = '';

if (file_exists($baseDir . '/includes/header.php')) {
    $headerPath = $baseDir . '/includes/header.php';
} elseif (file_exists($baseDir . '/header.php')) {
    $headerPath = $baseDir . '/header.php';
}

/*
 * IMPORTANT:
 * Existing admin/includes/header.php already contains the
 * admin sidebar/topbar in the current project.
 *
 * We do NOT include sidebar twice.
 */
if ($headerPath) {
    require_once $headerPath;
}

?>

<style>
/* =========================================================
   PRODUCT SPECIFICATIONS - PREMIUM ADMIN UI
========================================================= */

:root {
    --pc-primary: #2563eb;
    --pc-primary-dark: #1d4ed8;
    --pc-primary-soft: #eff6ff;

    --pc-success: #059669;
    --pc-success-soft: #ecfdf5;

    --pc-warning: #d97706;
    --pc-warning-soft: #fffbeb;

    --pc-danger: #dc2626;
    --pc-danger-soft: #fef2f2;

    --pc-text: #0f172a;
    --pc-muted: #64748b;
    --pc-border: #e2e8f0;
    --pc-bg: #f8fafc;
    --pc-card: #ffffff;

    --pc-radius: 18px;
    --pc-shadow:
        0 10px 30px rgba(15, 23, 42, .06);
}

.pc-spec-page {
    min-height: 100vh;
    background:
        radial-gradient(
            circle at top right,
            rgba(37, 99, 235, .06),
            transparent 32%
        ),
        var(--pc-bg);

    padding: 28px;
}

.pc-spec-container {
    width: 100%;
    max-width: 1600px;
    margin: 0 auto;
}

/* ---------------------------------------------------------
   HEADER
--------------------------------------------------------- */

.pc-page-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 20px;
    margin-bottom: 24px;
}

.pc-title-wrap {
    display: flex;
    gap: 16px;
    align-items: flex-start;
}

.pc-title-icon {
    width: 54px;
    height: 54px;
    flex: 0 0 54px;

    display: flex;
    align-items: center;
    justify-content: center;

    border-radius: 16px;

    background:
        linear-gradient(
            135deg,
            #2563eb,
            #4f46e5
        );

    color: #fff;

    box-shadow:
        0 12px 25px rgba(37, 99, 235, .20);
}

.pc-title-icon svg {
    width: 26px;
    height: 26px;
}

.pc-page-title {
    margin: 0;
    color: var(--pc-text);
    font-size: 27px;
    line-height: 1.2;
    font-weight: 800;
    letter-spacing: -.5px;
}

.pc-page-subtitle {
    margin: 7px 0 0;
    color: var(--pc-muted);
    font-size: 14px;
}

/* ---------------------------------------------------------
   BUTTONS
--------------------------------------------------------- */

.pc-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;

    min-height: 42px;
    padding: 0 16px;

    border-radius: 11px;
    border: 1px solid transparent;

    font-size: 13px;
    font-weight: 700;

    text-decoration: none;
    cursor: pointer;

    transition:
        transform .2s ease,
        box-shadow .2s ease,
        background .2s ease,
        border-color .2s ease;
}

.pc-btn:hover {
    transform: translateY(-1px);
}

.pc-btn-primary {
    color: #fff;
    background: linear-gradient(
        135deg,
        #2563eb,
        #4f46e5
    );

    box-shadow:
        0 8px 20px rgba(37, 99, 235, .18);
}

.pc-btn-primary:hover {
    color: #fff;
    box-shadow:
        0 12px 25px rgba(37, 99, 235, .24);
}

.pc-btn-light {
    color: #334155;
    background: #fff;
    border-color: var(--pc-border);
}

.pc-btn-light:hover {
    color: #0f172a;
    border-color: #cbd5e1;
}

.pc-btn-danger {
    color: #b91c1c;
    background: var(--pc-danger-soft);
    border-color: #fecaca;
}

.pc-btn-danger:hover {
    color: #991b1b;
}

.pc-btn svg {
    width: 16px;
    height: 16px;
}

/* ---------------------------------------------------------
   ALERTS
--------------------------------------------------------- */

.pc-alert {
    display: flex;
    align-items: flex-start;
    gap: 12px;

    padding: 14px 16px;

    margin-bottom: 20px;

    border-radius: 14px;
    border: 1px solid;

    font-size: 13px;
    font-weight: 600;
}

.pc-alert-success {
    color: #047857;
    background: var(--pc-success-soft);
    border-color: #a7f3d0;
}

.pc-alert-error {
    color: #b91c1c;
    background: var(--pc-danger-soft);
    border-color: #fecaca;
}

.pc-alert-icon {
    width: 18px;
    height: 18px;
    flex: 0 0 18px;
}

/* ---------------------------------------------------------
   STATS
--------------------------------------------------------- */

.pc-stat-grid {
    display: grid;
    grid-template-columns:
        repeat(4, minmax(0, 1fr));

    gap: 16px;

    margin-bottom: 20px;
}

.pc-stat-card {
    position: relative;
    overflow: hidden;

    background: var(--pc-card);

    border: 1px solid var(--pc-border);

    border-radius: var(--pc-radius);

    padding: 20px;

    box-shadow: var(--pc-shadow);
}

.pc-stat-card::after {
    content: "";

    position: absolute;

    width: 90px;
    height: 90px;

    right: -30px;
    bottom: -35px;

    border-radius: 50%;

    background: var(--pc-primary-soft);
}

.pc-stat-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.pc-stat-label {
    color: var(--pc-muted);
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .6px;
}

.pc-stat-value {
    margin-top: 7px;

    color: var(--pc-text);

    font-size: 28px;
    line-height: 1;

    font-weight: 800;
}

.pc-stat-icon {
    width: 42px;
    height: 42px;

    display: flex;
    align-items: center;
    justify-content: center;

    border-radius: 13px;

    color: var(--pc-primary);
    background: var(--pc-primary-soft);
}

.pc-stat-icon svg {
    width: 20px;
    height: 20px;
}

/* ---------------------------------------------------------
   CARD
--------------------------------------------------------- */

.pc-card {
    background: var(--pc-card);
    border: 1px solid var(--pc-border);
    border-radius: var(--pc-radius);
    box-shadow: var(--pc-shadow);

    overflow: hidden;
}

.pc-card-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 18px;

    padding: 19px 20px;

    border-bottom: 1px solid var(--pc-border);
}

.pc-card-title {
    margin: 0;

    color: var(--pc-text);

    font-size: 15px;
    font-weight: 800;
}

.pc-card-description {
    margin: 4px 0 0;

    color: var(--pc-muted);

    font-size: 12px;
}

/* ---------------------------------------------------------
   FORM
--------------------------------------------------------- */

.pc-form-grid {
    display: grid;
    grid-template-columns:
        repeat(12, minmax(0, 1fr));

    gap: 16px;

    padding: 20px;
}

.pc-field {
    grid-column: span 6;
}

.pc-field-full {
    grid-column: span 12;
}

.pc-field-third {
    grid-column: span 4;
}

.pc-label {
    display: block;

    margin-bottom: 7px;

    color: #334155;

    font-size: 12px;
    font-weight: 800;
}

.pc-required {
    color: #dc2626;
}

.pc-input,
.pc-select,
.pc-textarea {
    width: 100%;

    border: 1px solid #dbe3ee;

    border-radius: 11px;

    background: #fff;

    color: #0f172a;

    font-family: inherit;

    font-size: 13px;

    outline: none;

    transition:
        border-color .2s ease,
        box-shadow .2s ease;
}

.pc-input,
.pc-select {
    height: 44px;

    padding: 0 13px;
}

.pc-textarea {
    min-height: 108px;

    padding: 12px 13px;

    resize: vertical;
}

.pc-input:focus,
.pc-select:focus,
.pc-textarea:focus {
    border-color: #93c5fd;

    box-shadow:
        0 0 0 4px rgba(37, 99, 235, .08);
}

.pc-help {
    margin-top: 6px;

    color: #94a3b8;

    font-size: 11px;
}

.pc-form-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;

    gap: 10px;

    padding: 16px 20px;

    border-top: 1px solid var(--pc-border);

    background: #fbfdff;
}

/* ---------------------------------------------------------
   FILTERS
--------------------------------------------------------- */

.pc-filter-grid {
    display: grid;

    grid-template-columns:
        minmax(220px, 1.7fr)
        minmax(190px, 1fr)
        minmax(180px, 1fr)
        auto;

    gap: 12px;

    padding: 16px 20px;

    border-bottom: 1px solid var(--pc-border);

    background: #fbfdff;
}

.pc-filter-actions {
    display: flex;
    align-items: flex-end;
    gap: 8px;
}

/* ---------------------------------------------------------
   TABLE
--------------------------------------------------------- */

.pc-table-wrap {
    overflow-x: auto;
}

.pc-table {
    width: 100%;

    border-collapse: collapse;

    min-width: 980px;
}

.pc-table th {
    padding: 13px 16px;

    text-align: left;

    color: #64748b;

    background: #f8fafc;

    border-bottom: 1px solid var(--pc-border);

    font-size: 10px;

    text-transform: uppercase;

    letter-spacing: .7px;

    font-weight: 800;
}

.pc-table td {
    padding: 15px 16px;

    border-bottom: 1px solid #eef2f7;

    vertical-align: middle;

    color: #334155;

    font-size: 13px;
}

.pc-table tbody tr {
    transition: background .15s ease;
}

.pc-table tbody tr:hover {
    background: #fafcff;
}

.pc-product {
    display: flex;
    align-items: center;

    gap: 11px;
}

.pc-product-icon {
    width: 38px;
    height: 38px;

    flex: 0 0 38px;

    display: flex;
    align-items: center;
    justify-content: center;

    border-radius: 11px;

    color: #2563eb;

    background: #eff6ff;
}

.pc-product-icon svg {
    width: 18px;
    height: 18px;
}

.pc-product-name {
    color: #0f172a;

    font-weight: 800;

    line-height: 1.25;
}

.pc-product-code {
    margin-top: 3px;

    color: #94a3b8;

    font-size: 11px;
}

.pc-spec-group {
    display: inline-flex;

    align-items: center;

    padding: 5px 9px;

    border-radius: 999px;

    color: #4338ca;

    background: #eef2ff;

    font-size: 11px;

    font-weight: 800;
}

.pc-spec-key {
    color: #0f172a;

    font-weight: 800;
}

.pc-spec-value {
    color: #475569;

    max-width: 310px;

    line-height: 1.45;

    white-space: normal;
}

.pc-unit {
    display: inline-flex;

    padding: 5px 8px;

    border-radius: 8px;

    color: #0369a1;

    background: #f0f9ff;

    font-size: 11px;

    font-weight: 800;
}

.pc-sort {
    display: inline-flex;

    min-width: 30px;
    height: 28px;

    align-items: center;
    justify-content: center;

    padding: 0 8px;

    border-radius: 8px;

    background: #f1f5f9;

    color: #475569;

    font-size: 11px;

    font-weight: 800;
}

.pc-actions {
    display: flex;

    align-items: center;

    justify-content: flex-end;

    gap: 7px;
}

.pc-icon-btn {
    width: 35px;
    height: 35px;

    display: inline-flex;

    align-items: center;
    justify-content: center;

    border-radius: 9px;

    border: 1px solid var(--pc-border);

    background: #fff;

    color: #475569;

    cursor: pointer;

    text-decoration: none;

    transition: all .18s ease;
}

.pc-icon-btn:hover {
    color: #2563eb;

    border-color: #bfdbfe;

    background: #eff6ff;
}

.pc-icon-btn.danger:hover {
    color: #dc2626;

    border-color: #fecaca;

    background: #fef2f2;
}

.pc-icon-btn svg {
    width: 15px;
    height: 15px;
}

/* ---------------------------------------------------------
   EMPTY
--------------------------------------------------------- */

.pc-empty {
    text-align: center;

    padding: 60px 25px;
}

.pc-empty-icon {
    width: 58px;
    height: 58px;

    margin: 0 auto 14px;

    display: flex;

    align-items: center;
    justify-content: center;

    border-radius: 17px;

    background: #eff6ff;

    color: #2563eb;
}

.pc-empty-icon svg {
    width: 27px;
    height: 27px;
}

.pc-empty-title {
    margin: 0;

    color: #0f172a;

    font-size: 16px;

    font-weight: 800;
}

.pc-empty-text {
    margin: 7px auto 0;

    max-width: 480px;

    color: #64748b;

    font-size: 13px;

    line-height: 1.6;
}

/* ---------------------------------------------------------
   PAGINATION
--------------------------------------------------------- */

.pc-pagination {
    display: flex;

    align-items: center;

    justify-content: space-between;

    gap: 15px;

    padding: 16px 20px;

    border-top: 1px solid var(--pc-border);
}

.pc-page-info {
    color: #64748b;

    font-size: 12px;

    font-weight: 600;
}

.pc-page-links {
    display: flex;

    align-items: center;

    gap: 5px;
}

.pc-page-link {
    min-width: 34px;
    height: 34px;

    padding: 0 9px;

    display: inline-flex;

    align-items: center;
    justify-content: center;

    border-radius: 9px;

    color: #475569;

    background: #fff;

    border: 1px solid var(--pc-border);

    text-decoration: none;

    font-size: 12px;

    font-weight: 700;
}

.pc-page-link:hover {
    color: #2563eb;

    border-color: #bfdbfe;

    background: #eff6ff;
}

.pc-page-link.active {
    color: #fff;

    border-color: #2563eb;

    background: #2563eb;
}

/* ---------------------------------------------------------
   RESPONSIVE
--------------------------------------------------------- */

@media (max-width: 1100px) {

    .pc-stat-grid {
        grid-template-columns:
            repeat(2, minmax(0, 1fr));
    }

    .pc-filter-grid {
        grid-template-columns:
            repeat(2, minmax(0, 1fr));
    }

    .pc-filter-actions {
        align-items: center;
    }
}

@media (max-width: 800px) {

    .pc-spec-page {
        padding: 18px 14px;
    }

    .pc-page-head {
        flex-direction: column;
    }

    .pc-title-wrap {
        width: 100%;
    }

    .pc-page-head > .pc-btn {
        width: 100%;
    }

    .pc-stat-grid {
        grid-template-columns:
            1fr 1fr;
    }

    .pc-form-grid {
        grid-template-columns: 1fr;
    }

    .pc-field,
    .pc-field-full,
    .pc-field-third {
        grid-column: span 1;
    }

    .pc-filter-grid {
        grid-template-columns: 1fr;
    }

    .pc-pagination {
        flex-direction: column;
        align-items: stretch;
    }

    .pc-page-links {
        justify-content: center;
        flex-wrap: wrap;
    }
}

@media (max-width: 520px) {

    .pc-stat-grid {
        grid-template-columns: 1fr;
    }

    .pc-page-title {
        font-size: 22px;
    }

    .pc-title-icon {
        width: 46px;
        height: 46px;
        flex-basis: 46px;
    }

    .pc-form-actions {
        flex-direction: column-reverse;
    }

    .pc-form-actions .pc-btn {
        width: 100%;
    }
}
</style>

<div class="pc-spec-page">
    <div class="pc-spec-container">

        <!-- =================================================
             PAGE HEADER
        ================================================== -->

        <div class="pc-page-head">

            <div class="pc-title-wrap">

                <div class="pc-title-icon">
                    <svg
                        viewBox="0 0 24 24"
                        fill="none"
                        stroke="currentColor"
                        stroke-width="2"
                        stroke-linecap="round"
                        stroke-linejoin="round"
                    >
                        <path d="M9 3h6"/>
                        <path d="M10 3v6.5L5.5 17a4 4 0 0 0 3.4 4h6.2a4 4 0 0 0 3.4-4L14 9.5V3"/>
                        <path d="M8 15h8"/>
                    </svg>
                </div>

                <div>
                    <h1 class="pc-page-title">
                        Product Specifications
                    </h1>

                    <p class="pc-page-subtitle">
                        Manage technical specifications, values, units and specification groups for your products.
                    </p>
                </div>

            </div>

            <a
                href="#specification-form"
                class="pc-btn pc-btn-primary"
            >
                <svg
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="2"
                    stroke-linecap="round"
                    stroke-linejoin="round"
                >
                    <path d="M12 5v14"/>
                    <path d="M5 12h14"/>
                </svg>

                Add Specification
            </a>

        </div>

        <!-- =================================================
             ALERTS
        ================================================== -->

        <?php if ($message): ?>

            <div class="pc-alert pc-alert-success">

                <svg
                    class="pc-alert-icon"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="2"
                    stroke-linecap="round"
                    stroke-linejoin="round"
                >
                    <path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/>
                    <path d="m9 11 3 3L22 4"/>
                </svg>

                <span>
                    <?= e($message) ?>
                </span>

            </div>

        <?php endif; ?>

        <?php if ($error): ?>

            <div class="pc-alert pc-alert-error">

                <svg
                    class="pc-alert-icon"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="2"
                    stroke-linecap="round"
                    stroke-linejoin="round"
                >
                    <circle cx="12" cy="12" r="10"/>
                    <line x1="12" y1="8" x2="12" y2="12"/>
                    <line x1="12" y1="16" x2="12.01" y2="16"/>
                </svg>

                <span>
                    <?= e($error) ?>
                </span>

            </div>

        <?php endif; ?>

        <!-- =================================================
             STATISTICS
        ================================================== -->

        <div class="pc-stat-grid">

            <div class="pc-stat-card">

                <div class="pc-stat-top">

                    <div>
                        <div class="pc-stat-label">
                            Total Specifications
                        </div>

                        <div class="pc-stat-value">
                            <?= number_format($stats['total']) ?>
                        </div>
                    </div>

                    <div class="pc-stat-icon">
                        <svg
                            viewBox="0 0 24 24"
                            fill="none"
                            stroke="currentColor"
                            stroke-width="2"
                        >
                            <path d="M4 5h16"/>
                            <path d="M4 12h16"/>
                            <path d="M4 19h16"/>
                        </svg>
                    </div>

                </div>

            </div>

            <div class="pc-stat-card">

                <div class="pc-stat-top">

                    <div>
                        <div class="pc-stat-label">
                            Products Covered
                        </div>

                        <div class="pc-stat-value">
                            <?= number_format($stats['products']) ?>
                        </div>
                    </div>

                    <div class="pc-stat-icon">
                        <svg
                            viewBox="0 0 24 24"
                            fill="none"
                            stroke="currentColor"
                            stroke-width="2"
                        >
                            <rect x="3" y="4" width="18" height="16" rx="2"/>
                            <path d="M7 8h10"/>
                            <path d="M7 12h6"/>
                            <path d="M7 16h8"/>
                        </svg>
                    </div>

                </div>

            </div>

            <div class="pc-stat-card">

                <div class="pc-stat-top">

                    <div>
                        <div class="pc-stat-label">
                            Specification Groups
                        </div>

                        <div class="pc-stat-value">
                            <?= number_format($stats['groups']) ?>
                        </div>
                    </div>

                    <div class="pc-stat-icon">
                        <svg
                            viewBox="0 0 24 24"
                            fill="none"
                            stroke="currentColor"
                            stroke-width="2"
                        >
                            <path d="M4 6h16"/>
                            <path d="M4 12h16"/>
                            <path d="M4 18h10"/>
                        </svg>
                    </div>

                </div>

            </div>

            <div class="pc-stat-card">

                <div class="pc-stat-top">

                    <div>
                        <div class="pc-stat-label">
                            With Unit
                        </div>

                        <div class="pc-stat-value">
                            <?= number_format($stats['with_unit']) ?>
                        </div>
                    </div>

                    <div class="pc-stat-icon">
                        <svg
                            viewBox="0 0 24 24"
                            fill="none"
                            stroke="currentColor"
                            stroke-width="2"
                        >
                            <circle cx="12" cy="12" r="9"/>
                            <path d="M8 12h8"/>
                            <path d="M12 8v8"/>
                        </svg>
                    </div>

                </div>

            </div>

        </div>

        <!-- =================================================
             ADD / EDIT FORM
        ================================================== -->

        <div
            class="pc-card"
            id="specification-form"
            style="margin-bottom:20px;"
        >

            <div class="pc-card-head">

                <div>
                    <h2 class="pc-card-title">
                        <?= $editSpecification
                            ? 'Edit Product Specification'
                            : 'Add Product Specification'
                        ?>
                    </h2>

                    <p class="pc-card-description">
                        <?= $editSpecification
                            ? 'Update the selected technical specification.'
                            : 'Create a specification entry for any product.'
                        ?>
                    </p>
                </div>

                <?php if ($editSpecification): ?>

                    <a
                        href="product_specifications.php"
                        class="pc-btn pc-btn-light"
                    >
                        Cancel Edit
                    </a>

                <?php endif; ?>

            </div>

            <form method="post">

                <?= csrf_field() ?>

                <input
                    type="hidden"
                    name="save_specification"
                    value="1"
                >

                <input
                    type="hidden"
                    name="id"
                    value="<?= (int)($editSpecification['id'] ?? 0) ?>"
                >

                <div class="pc-form-grid">

                    <!-- PRODUCT -->

                    <div class="pc-field">

                        <label class="pc-label">
                            Product
                            <span class="pc-required">*</span>
                        </label>

                        <select
                            name="product_id"
                            class="pc-select"
                            required
                        >

                            <option value="">
                                Select Product
                            </option>

                            <?php foreach ($products as $product): ?>

                                <option
                                    value="<?= (int)$product['id'] ?>"
                                    <?= (
                                        (int)($editSpecification['product_id'] ?? 0)
                                        === (int)$product['id']
                                    ) ? 'selected' : '' ?>
                                >

                                    <?= e($product['name']) ?>

                                    <?php if (!empty($product['product_code'])): ?>
                                        — <?= e($product['product_code']) ?>
                                    <?php endif; ?>

                                </option>

                            <?php endforeach; ?>

                        </select>

                        <div class="pc-help">
                            Select the product to which this specification belongs.
                        </div>

                    </div>

                    <!-- GROUP -->

                    <div class="pc-field">

                        <label class="pc-label">
                            Specification Group
                        </label>

                        <input
                            type="text"
                            name="specification_group"
                            class="pc-input"
                            maxlength="150"
                            list="specificationGroups"
                            placeholder="e.g. Chemical Properties"
                            value="<?= e(
                                $editSpecification['specification_group'] ?? ''
                            ) ?>"
                        >

                        <datalist id="specificationGroups">

                            <?php foreach ($groups as $group): ?>

                                <option
                                    value="<?= e($group['specification_group']) ?>"
                                >

                            <?php endforeach; ?>

                        </datalist>

                        <div class="pc-help">
                            Group related specifications together.
                        </div>

                    </div>

                    <!-- KEY -->

                    <div class="pc-field">

                        <label class="pc-label">
                            Specification Key
                            <span class="pc-required">*</span>
                        </label>

                        <input
                            type="text"
                            name="specification_key"
                            class="pc-input"
                            maxlength="255"
                            required
                            placeholder="e.g. Purity"
                            value="<?= e(
                                $editSpecification['specification_key'] ?? ''
                            ) ?>"
                        >

                    </div>

                    <!-- UNIT -->

                    <div class="pc-field">

                        <label class="pc-label">
                            Unit
                        </label>

                        <input
                            type="text"
                            name="unit"
                            class="pc-input"
                            maxlength="50"
                            placeholder="e.g. %, g/mol, °C"
                            value="<?= e(
                                $editSpecification['unit'] ?? ''
                            ) ?>"
                        >

                    </div>

                    <!-- VALUE -->

                    <div class="pc-field-full">

                        <label class="pc-label">
                            Specification Value
                        </label>

                        <textarea
                            name="specification_value"
                            class="pc-textarea"
                            placeholder="Enter specification value..."
                        ><?= e(
                            $editSpecification['specification_value'] ?? ''
                        ) ?></textarea>

                    </div>

                    <!-- SORT ORDER -->

                    <div class="pc-field-third">

                        <label class="pc-label">
                            Sort Order
                        </label>

                        <input
                            type="number"
                            name="sort_order"
                            class="pc-input"
                            min="0"
                            step="1"
                            value="<?= (int)(
                                $editSpecification['sort_order'] ?? 0
                            ) ?>"
                        >

                        <div class="pc-help">
                            Lower number appears first.
                        </div>

                    </div>

                </div>

                <div class="pc-form-actions">

                    <?php if ($editSpecification): ?>

                        <a
                            href="product_specifications.php"
                            class="pc-btn pc-btn-light"
                        >
                            Cancel
                        </a>

                    <?php endif; ?>

                    <button
                        type="submit"
                        class="pc-btn pc-btn-primary"
                    >

                        <svg
                            viewBox="0 0 24 24"
                            fill="none"
                            stroke="currentColor"
                            stroke-width="2"
                            stroke-linecap="round"
                            stroke-linejoin="round"
                        >
                            <path d="M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2z"/>
                            <polyline points="17 21 17 13 7 13 7 21"/>
                            <polyline points="7 3 7 8 15 8"/>
                        </svg>

                        <?= $editSpecification
                            ? 'Update Specification'
                            : 'Save Specification'
                        ?>

                    </button>

                </div>

            </form>

        </div>

        <!-- =================================================
             SPECIFICATIONS LIST
        ================================================== -->

        <div class="pc-card">

            <div class="pc-card-head">

                <div>
                    <h2 class="pc-card-title">
                        Specification Records
                    </h2>

                    <p class="pc-card-description">
                        <?= number_format($totalFiltered) ?>
                        specification record(s) found.
                    </p>
                </div>

            </div>

            <!-- FILTERS -->

            <form method="get">

                <div class="pc-filter-grid">

                    <div>

                        <label class="pc-label">
                            Search
                        </label>

                        <input
                            type="search"
                            name="search"
                            class="pc-input"
                            placeholder="Search product, key, value..."
                            value="<?= e($search) ?>"
                        >

                    </div>

                    <div>

                        <label class="pc-label">
                            Product
                        </label>

                        <select
                            name="product_id"
                            class="pc-select"
                        >

                            <option value="">
                                All Products
                            </option>

                            <?php foreach ($products as $product): ?>

                                <option
                                    value="<?= (int)$product['id'] ?>"
                                    <?= $filterProduct === (int)$product['id']
                                        ? 'selected'
                                        : '' ?>
                                >
                                    <?= e($product['name']) ?>
                                </option>

                            <?php endforeach; ?>

                        </select>

                    </div>

                    <div>

                        <label class="pc-label">
                            Specification Group
                        </label>

                        <select
                            name="group"
                            class="pc-select"
                        >

                            <option value="">
                                All Groups
                            </option>

                            <?php foreach ($groups as $group): ?>

                                <option
                                    value="<?= e($group['specification_group']) ?>"
                                    <?= $filterGroup === $group['specification_group']
                                        ? 'selected'
                                        : '' ?>
                                >
                                    <?= e($group['specification_group']) ?>
                                </option>

                            <?php endforeach; ?>

                        </select>

                    </div>

                    <div class="pc-filter-actions">

                        <button
                            type="submit"
                            class="pc-btn pc-btn-primary"
                        >
                            <svg
                                viewBox="0 0 24 24"
                                fill="none"
                                stroke="currentColor"
                                stroke-width="2"
                            >
                                <circle cx="11" cy="11" r="7"/>
                                <path d="m20 20-4-4"/>
                            </svg>

                            Filter
                        </button>

                        <a
                            href="product_specifications.php"
                            class="pc-btn pc-btn-light"
                            title="Reset filters"
                        >
                            Reset
                        </a>

                    </div>

                </div>

            </form>

            <?php if (!empty($specifications)): ?>

                <div class="pc-table-wrap">

                    <table class="pc-table">

                        <thead>

                            <tr>

                                <th>
                                    Product
                                </th>

                                <th>
                                    Group
                                </th>

                                <th>
                                    Specification
                                </th>

                                <th>
                                    Value
                                </th>

                                <th>
                                    Unit
                                </th>

                                <th>
                                    Order
                                </th>

                                <th style="text-align:right;">
                                    Actions
                                </th>

                            </tr>

                        </thead>

                        <tbody>

                        <?php foreach ($specifications as $spec): ?>

                            <tr>

                                <!-- PRODUCT -->

                                <td>

                                    <div class="pc-product">

                                        <div class="pc-product-icon">

                                            <svg
                                                viewBox="0 0 24 24"
                                                fill="none"
                                                stroke="currentColor"
                                                stroke-width="2"
                                            >
                                                <path d="M9 3h6"/>
                                                <path d="M10 3v6.5L5.5 17a4 4 0 0 0 3.4 4h6.2a4 4 0 0 0 3.4-4L14 9.5V3"/>
                                            </svg>

                                        </div>

                                        <div>

                                            <div class="pc-product-name">
                                                <?= e($spec['product_name']) ?>
                                            </div>

                                            <?php if (!empty($spec['product_code'])): ?>

                                                <div class="pc-product-code">
                                                    <?= e($spec['product_code']) ?>
                                                </div>

                                            <?php endif; ?>

                                        </div>

                                    </div>

                                </td>

                                <!-- GROUP -->

                                <td>

                                    <?php if (!empty($spec['specification_group'])): ?>

                                        <span class="pc-spec-group">
                                            <?= e($spec['specification_group']) ?>
                                        </span>

                                    <?php else: ?>

                                        <span style="color:#94a3b8;">
                                            —
                                        </span>

                                    <?php endif; ?>

                                </td>

                                <!-- KEY -->

                                <td>

                                    <div class="pc-spec-key">
                                        <?= e($spec['specification_key']) ?>
                                    </div>

                                </td>

                                <!-- VALUE -->

                                <td>

                                    <div class="pc-spec-value">

                                        <?php
                                        $value = trim(
                                            (string)($spec['specification_value'] ?? '')
                                        );

                                        if ($value !== '') {
                                            echo nl2br(e($value));
                                        } else {
                                            echo '<span style="color:#94a3b8;">—</span>';
                                        }
                                        ?>

                                    </div>

                                </td>

                                <!-- UNIT -->

                                <td>

                                    <?php if (!empty($spec['unit'])): ?>

                                        <span class="pc-unit">
                                            <?= e($spec['unit']) ?>
                                        </span>

                                    <?php else: ?>

                                        <span style="color:#94a3b8;">
                                            —
                                        </span>

                                    <?php endif; ?>

                                </td>

                                <!-- SORT -->

                                <td>

                                    <span class="pc-sort">
                                        <?= (int)$spec['sort_order'] ?>
                                    </span>

                                </td>

                                <!-- ACTIONS -->

                                <td>

                                    <div class="pc-actions">

                                        <a
                                            href="?edit=<?= (int)$spec['id'] ?>"
                                            class="pc-icon-btn"
                                            title="Edit specification"
                                        >

                                            <svg
                                                viewBox="0 0 24 24"
                                                fill="none"
                                                stroke="currentColor"
                                                stroke-width="2"
                                            >
                                                <path d="M12 20h9"/>
                                                <path d="M16.5 3.5a2.12 2.12 0 0 1 3 3L8 18l-4 1 1-4Z"/>
                                            </svg>

                                        </a>

                                        <form
                                            method="post"
                                            style="display:inline;"
                                            onsubmit="return confirm('Are you sure you want to delete this specification?');"
                                        >

                                            <?= csrf_field() ?>

                                            <input
                                                type="hidden"
                                                name="delete_id"
                                                value="<?= (int)$spec['id'] ?>"
                                            >

                                            <button
                                                type="submit"
                                                class="pc-icon-btn danger"
                                                title="Delete specification"
                                            >

                                                <svg
                                                    viewBox="0 0 24 24"
                                                    fill="none"
                                                    stroke="currentColor"
                                                    stroke-width="2"
                                                >
                                                    <polyline points="3 6 5 6 21 6"/>
                                                    <path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6"/>
                                                    <path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/>
                                                    <line x1="10" y1="11" x2="10" y2="17"/>
                                                    <line x1="14" y1="11" x2="14" y2="17"/>
                                                </svg>

                                            </button>

                                        </form>

                                    </div>

                                </td>

                            </tr>

                        <?php endforeach; ?>

                        </tbody>

                    </table>

                </div>

                <!-- PAGINATION -->

                <?php if ($totalPages > 1): ?>

                    <div class="pc-pagination">

                        <div class="pc-page-info">

                            Showing
                            <?= $totalFiltered > 0
                                ? ($offset + 1)
                                : 0
                            ?>

                            to

                            <?= min(
                                $offset + $perPage,
                                $totalFiltered
                            ) ?>

                            of
                            <?= number_format($totalFiltered) ?>
                            records

                        </div>

                        <div class="pc-page-links">

                            <?php

                            $queryParams = [];

                            if ($search !== '') {
                                $queryParams['search'] = $search;
                            }

                            if ($filterProduct > 0) {
                                $queryParams['product_id'] = $filterProduct;
                            }

                            if ($filterGroup !== '') {
                                $queryParams['group'] = $filterGroup;
                            }

                            $startPage = max(
                                1,
                                $page - 2
                            );

                            $endPage = min(
                                $totalPages,
                                $page + 2
                            );

                            ?>

                            <?php if ($page > 1): ?>

                                <?php
                                $queryParams['page'] = $page - 1;
                                ?>

                                <a
                                    href="?<?= http_build_query($queryParams) ?>"
                                    class="pc-page-link"
                                >
                                    ‹
                                </a>

                            <?php endif; ?>

                            <?php for ($i = $startPage; $i <= $endPage; $i++): ?>

                                <?php
                                $queryParams['page'] = $i;
                                ?>

                                <a
                                    href="?<?= http_build_query($queryParams) ?>"
                                    class="pc-page-link <?= $i === $page ? 'active' : '' ?>"
                                >
                                    <?= $i ?>
                                </a>

                            <?php endfor; ?>

                            <?php if ($page < $totalPages): ?>

                                <?php
                                $queryParams['page'] = $page + 1;
                                ?>

                                <a
                                    href="?<?= http_build_query($queryParams) ?>"
                                    class="pc-page-link"
                                >
                                    ›
                                </a>

                            <?php endif; ?>

                        </div>

                    </div>

                <?php endif; ?>

            <?php else: ?>

                <div class="pc-empty">

                    <div class="pc-empty-icon">

                        <svg
                            viewBox="0 0 24 24"
                            fill="none"
                            stroke="currentColor"
                            stroke-width="2"
                        >
                            <path d="M4 5h16"/>
                            <path d="M4 12h16"/>
                            <path d="M4 19h16"/>
                        </svg>

                    </div>

                    <h3 class="pc-empty-title">
                        No specifications found
                    </h3>

                    <p class="pc-empty-text">
                        There are no product specifications matching your current filters.
                        Add a specification above or reset the filters.
                    </p>

                </div>

            <?php endif; ?>

        </div>

    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function () {

    const form = document.querySelector(
        '#specification-form form'
    );

    if (!form) {
        return;
    }

    form.addEventListener('submit', function () {

        const submitButton = form.querySelector(
            'button[type="submit"]'
        );

        if (!submitButton) {
            return;
        }

        submitButton.disabled = true;

        submitButton.style.opacity = '0.7';

        submitButton.style.cursor = 'wait';

    });

});
</script>

<?php

/* ---------------------------------------------------------
   FOOTER
--------------------------------------------------------- */

$footerPath = '';

if (file_exists($baseDir . '/includes/footer.php')) {
    $footerPath = $baseDir . '/includes/footer.php';
} elseif (file_exists($baseDir . '/footer.php')) {
    $footerPath = $baseDir . '/footer.php';
}

if ($footerPath) {
    require_once $footerPath;
}
?>