<?php
if (!defined('ABSPATH')) exit;

/**
 * Load core plugin
 */
require_once __DIR__ . '/ponyracking.php';

/* ======================================================
 * FRONTEND PAGE ROUTER (PRIMARY CONTROL)
 * ====================================================== */

add_filter('the_content', function ($content) {

    static $running = false;

    if ($running) {
        return $content;
    }

    if (!is_page()) {
        return $content;
    }

    global $post;

    if (!$post || empty($post->post_name)) {
        return $content;
    }

    $running = true;
    $slug = trim($post->post_name);

    switch ($slug) {

        case 'racking':
            return function_exists('pony_racking_render_home')
                ? pony_racking_render_home()
                : $content;

        case 'rackinspection':
        case 'rack-inspection':
            return function_exists('pony_racking_render_inspection_page')
                ? pony_racking_render_inspection_page()
                : $content;

        case 'rack-reports':
            return function_exists('pony_racking_render_reports_table')
                ? pony_racking_render_reports_table()
                : $content;

        case 'rack-record':
        case 'rack-record-2':
            return function_exists('pony_racking_render_single_record')
                ? pony_racking_render_single_record()
                : $content;

        case 'rack-map':
        case 'warehouse-map':
            return function_exists('pony_racking_render_clickable_map')
                ? pony_racking_render_clickable_map()
                : $content;

        case 'rack-damage':
            return function_exists('pony_racking_render_current_damage')
                ? pony_racking_render_current_damage()
                : $content;
    }

    return $content;

}, 20);

.pony-racking-wrap table {
    width: 100%;
    border-collapse: collapse;
    font-size: 14px;
}

.pony-racking-wrap th,
.pony-racking-wrap td {
    padding: 10px 12px;
    text-align: left;
}

/* ======================================================
 * FORM LAYOUT FIX (ENTERPRISE)
 * ====================================================== */

/* 2-column responsive grid */
.pony-racking-grid-2col {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 18px 24px;
    align-items: start;
}

/* Full-width fields */
.pony-racking-full {
    grid-column: 1 / -1;
}

/* Improve field spacing */
.pony-racking-field label {
    display: block;
    font-weight: 600;
    margin-bottom: 6px;
}

.pony-racking-field input,
.pony-racking-field select,
.pony-racking-field textarea {
    width: 100%;
    padding: 10px 12px;
    border-radius: 8px;
    border: 1px solid #cbd5e1;
    font-size: 14px;
}

/* Button spacing */
.pony-racking-btn {
    margin-top: 20px;
    padding: 10px 18px;
    border-radius: 8px;
}

/* Mobile fallback */
@media (max-width: 768px) {
    .pony-racking-grid-2col {
        grid-template-columns: 1fr;
    }
}

/* BULK BAR */
.pony-racking-bulk-bar {
    display:flex;
    gap:12px;
    align-items:center;
    margin-bottom:20px;
}

/* CARD HEADER */
.rack-card-header {
    display:flex;
    align-items:center;
    gap:10px;
    margin-bottom: 6px;
}

/* ======================================================
 * DAMAGE IMAGE GRID (ENTERPRISE CARD LAYOUT)
 * ====================================================== */

.pony-damage-images {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin-top: 10px;
}

echo '<div class="pony-damage-images">';

foreach ($images as $img) {
    echo '<div class="pony-damage-image">';
    echo '<img src="'.esc_url($img).'" />';
    echo '</div>';
}

echo '</div>';


/* ======================================================
 * DAMAGE CARD SYSTEM (FINAL)
 * ====================================================== */


/* HEADER */
.pony-damage-header {
    font-size:20px;
    font-weight:600;
    margin-bottom:10px;
}

/* GRID */
.pony-damage-images {
    display:flex;
    flex-wrap:wrap;
    gap:10px;
}

/* FORCE IMAGE FIT */
.pony-damage-image img {
    width:100% !important;
    height:100% !important;
    object-fit:cover !important;
}

/* ======================================================
 * COMPACT DAMAGE LAYOUT
 * ====================================================== */

.pony-racking-card {
    padding:12px !important;
    margin-bottom:15px !important;
    border-bottom: 1px solid #d1d5db;
}

.pony-racking-wrap {
    max-width: 1100px;
    margin: 0 auto;
}

.pony-racking-cards {
    width: 100%;
}

/* COLUMN WIDTH CONTROL */
.col-id { width:40px; text-align:center; }
.col-location { width:180px; }

/* REMOVE stray page lines */
hr {
    display: none !important;
}

body > div {
    border-bottom: none !important;
}

/* KEEP TABLE STRUCTURE CLEAN */
table {
    border-collapse: collapse;
}

th, td {
    border: 1px solid #000;
}

.pony-racking-other-row {
    margin-top: 6px;
    display: flex;
    align-items: center;
}

.pony-racking-other-row label {
    display: flex;
    align-items: center;
    gap: 6px;
    white-space: nowrap;
}

/* FORCE ACTIONS INTO SINGLE LINE */
.pony-actions {
    display: flex;
    flex-wrap: nowrap;
    gap: 10px;
    align-items: center;
    white-space: nowrap;
}

/* PREVENT CELL WRAPPING */
.pony-actions-cell {
    white-space: nowrap;
    min-width: 260px; /* adjust if needed */
}

/* OPTIONAL: tighten links */
.pony-actions a {
    font-size: 13px;
}

/* OPTIONAL: keep table from crushing */
.pony-racking-table {
    table-layout: auto;
    width: 100%;
}

.pony-actions a {
    padding: 4px 8px;
    border-radius: 4px;
    text-decoration: none;
}

.pony-actions a:hover {
    background: #f0f0f0;
}

/* =========================================
   REPORT ACTIONS — FORCE SINGLE LINE
========================================= */

.pony-actions {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    gap: 8px;
}

.pony-actions-cell {
    white-space: nowrap;
    min-width: 320px;
}

/* Keep buttons tight */
.pony-actions .pony-racking-btn,
.pony-actions .pony-racking-btn-small {
    padding: 4px 8px;
    font-size: 12px;
}

/* ALIGN ALL ACTION LINKS PERFECTLY */
.pony-actions {
    display: flex;
    align-items: center; /* key fix */
    gap: 10px;
}

/* normalize all buttons */
.pony-actions a {
    display: inline-flex;
    align-items: center;
    line-height: 1;
}

/* TIGHTEN TABLE ROW HEIGHT */
.pony-racking-table td,
.pony-racking-table th {
    padding: 6px 10px;  /* was likely 12–16px */
    vertical-align: middle;
}

/* EVEN TIGHTER (optional) */
.pony-racking-table td {
    font-size: 13px;
}

/* tighten action column specifically */
.pony-actions-cell {
    min-width: 260px;
}

/* ======================================================
   CURRENT DAMAGE - COMPACT 3 COLUMN CARD VIEW
   ====================================================== */

.pony-racking-cards {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 18px;
    align-items: start;
}

.pony-damage-card {
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    border: 1px solid #d9d9d9;
    border-radius: 8px;
    background: #fff;
    padding: 8px 10px; /* 🔻 reduced */
    box-sizing: border-box;
    height: auto;
}

.rack-card-header {
    margin-bottom: 10px;
}

.pony-card-title {
    display: flex;
    align-items: center;
    gap: 6px;
    font-weight: 700;
    font-size: 16px; /* 🔻 smaller */
    line-height: 1.1;
}

.pony-damage-row {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
    height: auto;        /* 🔥 prevents stretching */
    display: block; /* was flex — remove side-by-side */
}

.pony-damage-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.pony-damage-content {
    width: 100%;
    font-size: 13px;
    line-height: 1.25;
    margin-bottom: 2px;   /* 🔥 tiny spacing only */
    flex: 0 0 auto;       /* 🔥 CRITICAL */
    margin-top: 5px;
}

.pony-racking-meta {
    margin-bottom: 4px;
}

.pony-damage-actions {
    width: 100%;
    display: flex;
    gap: 6px;
    margin-top: 0;        /* 🔥 remove push */
    flex: 0 0 auto;       /* 🔥 CRITICAL */
}

.pony-damage-actions .pony-racking-btn {
    flex: 1;
    text-align: center;
    padding: 6px 8px;   /* 🔻 smaller */
    font-size: 12px;
    line-height: 1.1;
}

/* Tablet */
@media (max-width: 1100px) {
    .pony-racking-cards {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

/* Mobile */
@media (max-width: 700px) {
    .pony-racking-cards {
        grid-template-columns: 1fr;
    }
}

.pony-racking-meta strong {
    display: inline-block;
    min-width: 70px;
}

.pony-damage-content .pony-racking-meta:last-child {
    margin-bottom: 2px; /* 🔻 tighten final block */
}

.pony-thumb {
    width: 60px;
    height: 60px;
    object-fit: cover;
    border-radius: 6px;
    cursor: pointer;
    display: block;
}

.pony-no-photo {
    width: 60px;
    height: 60px;
    background: #eee;
    border-radius: 6px;
}

.pony-photo-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin-top: 10px;
}

.pony-photo-thumb {
    position: relative;
    width: 90px;
    height: 90px;
}

.pony-photo-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 6px;
}

.pony-remove-photo {
    position: absolute;
    top: -6px;
    right: -6px;
    background: red;
    color: #fff;
    border: none;
    border-radius: 50%;
    width: 22px;
    height: 22px;
    cursor: pointer;
}

/* =========================================
   PHOTO PREVIEW GRID (UPLOAD + RECORD)
========================================= */

.pony-photo-preview-wrap {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin-top: 10px;
}

.pony-photo-item {
    position: relative;
    width: 80px;
    height: 80px;
    border-radius: 8px;
    overflow: hidden;
    background: #f4f4f4;
    border: 1px solid #ddd;
}

.pony-photo-item img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* REMOVE BUTTON */
.pony-photo-remove {
    position: absolute;
    top: -6px;
    right: -6px;
    background: #e74c3c;
    color: #fff;
    border-radius: 50%;
    width: 18px;
    height: 18px;
    font-size: 12px;
    text-align: center;
    line-height: 18px;
    cursor: pointer;
}

/* =========================================
   RECORD PAGE PHOTO GRID
========================================= */

.pony-photo-preview-wrap {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin-top: 10px;
}

.pony-photo-item {
    position: relative;
    width: 80px;
    height: 80px;
    border-radius: 8px;
    overflow: hidden;
    border: 1px solid #ddd;
    background: #f4f4f4;
}

.pony-photo-item img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.pony-photo-remove {
    position: absolute;
    top: 4px;
    right: 4px;
    width: 20px;
    height: 20px;
    border: none;
    border-radius: 50%;
    background: rgba(220, 53, 69, 0.95);
    color: #fff;
    font-size: 14px;
    line-height: 20px;
    text-align: center;
    cursor: pointer;
    padding: 0;
}

.pony-racking-success {
    background: #d4edda;
    color: #155724;
    border: 1px solid #c3e6cb;
    padding: 12px 15px;
    margin-bottom: 15px;
    font-weight: bold;
    border-radius: 6px;
}

/* DAMAGE GRID PHOTO FIX */
.rack-damage-image-row {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 8px;
}

.rack-damage-thumb {
    width: 70px;
    height: 70px;
    object-fit: cover;
    border-radius: 4px;
    border: 1px solid #ccc;
}

.pony-thumb-group {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
}

.pony-thumb-group img {
    width: 60px;
    height: 60px;
    object-fit: cover;
    border-radius: 4px;
}

/* ===== MULTI IMAGE FIX (FINAL) ===== */

.pony-damage-image {
    display: flex !important;
    flex-wrap: wrap;
    flex-direction: column !important;
    gap: 6px !important;
    align-items: flex-start !important;
    margin-bottom: 10px;
}

.pony-damage-image {
    width: 60px;   /* 🔻 was ~78 */
    height: 60px;
    border-radius: 6px;
    overflow: hidden;
    background: #f3f3f3;
    border: 1px solid #ddd;
    flex: 0 0 auto;
}

.pony-damage-image img {
    width: 60px !important;
    height: 60px !important;
    object-fit: cover !important;
    display: block !important;
    position: relative !important;
}

.pony-damage-image,
.pony-damage-image * {
    position: relative !important;
    overflow: visible !important;
}

input[readonly] {
    background: #f5f5f5;
    pointer-events: none;
}