<div class="page-header responsive-header">
    <div>
        <a href="<?= APP_URL ?>/reports" class="btn btn-ghost btn-sm mb-3" style="padding-left:0;">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><line x1="19" y1="12" x2="5" y2="12"></line><polyline points="12 19 5 12 12 5"></polyline></svg>
            Back to Reports
        </a>
        <h1><?= $data['title'] ?></h1>
        <div class="page-subtitle">Track products approaching or past their expiry dates</div>
    </div>
    <div class="flex gap-2 flex-wrap">
        <a href="<?= APP_URL ?>/reports?report=expiry&format=csv<?= ($qs = http_build_query($data['filters'])) ? '&' . $qs : '' ?>" class="btn btn-outline btn-sm">
            <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path>
                <polyline points="7 10 12 15 17 10"></polyline>
                <line x1="12" y1="15" x2="12" y2="3"></line>
            </svg>
            Export CSV
        </a>
        <a href="<?= APP_URL ?>/reports?report=expiry&format=print<?= ($qs = http_build_query($data['filters'])) ? '&' . $qs : '' ?>" class="btn btn-secondary btn-sm">
            <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <polyline points="6 9 6 15 18 15"></polyline>
                <path d="M6 4h12v16H6z"></path>
            </svg>
            Print
        </a>
    </div>
</div>

<!-- Filters -->
<div class="card mb-3">
    <div class="card-header">
        <h3 class="card-title">Filters</h3>
    </div>
    <form method="GET" class="p-2">
        <input type="hidden" name="report" value="expiry">
        <div class="responsive-grid gap-2">
            <div class="form-group">
                <label class="form-label">Category</label>
                <select name="category_id" class="form-control">
                    <option value="">All Categories</option>
                    <?php
                    $categories = db()->prepare("SELECT id, name FROM categories WHERE tenant_id = ? ORDER BY name");
                    $categories->execute([Auth::tenantId()]);
                    foreach ($categories->fetchAll() as $cat):
                    ?>
                    <option value="<?= $cat['id'] ?>" <?= $data['filters']['category_id'] == $cat['id'] ? 'selected' : '' ?>>
                        <?= e($cat['name']) ?>
                    </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="form-group">
                <label class="form-label">Location</label>
                <select name="location_id" class="form-control">
                    <option value="">All Locations</option>
                    <?php
                    $locations = db()->prepare("SELECT id, name FROM locations WHERE tenant_id = ? ORDER BY name");
                    $locations->execute([Auth::tenantId()]);
                    foreach ($locations->fetchAll() as $loc):
                    ?>
                    <option value="<?= $loc['id'] ?>" <?= $data['filters']['location_id'] == $loc['id'] ? 'selected' : '' ?>>
                        <?= e($loc['name']) ?>
                    </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="form-group">
                <label class="form-label">Expiry Status</label>
                <select name="status" class="form-control">
                    <option value="">All Statuses</option>
                    <option value="expired" <?= $data['filters']['status'] == 'expired' ? 'selected' : '' ?>>Expired</option>
                    <option value="expiring_soon" <?= $data['filters']['status'] == 'expiring_soon' ? 'selected' : '' ?>>Expiring Soon (0-30 days)</option>
                    <option value="expiring" <?= $data['filters']['status'] == 'expiring' ? 'selected' : '' ?>>Expiring (0-90 days)</option>
                    <option value="safe" <?= $data['filters']['status'] == 'safe' ? 'selected' : '' ?>>Safe (>90 days)</option>
                </select>
            </div>
            <div class="form-group">
                <label class="form-label">&nbsp;</label>
                <button type="submit" class="btn btn-primary w-full">Apply Filters</button>
            </div>
            <div class="form-group">
                <label class="form-label">Expiry Date From</label>
                <input type="date" name="date_from" class="form-control" value="<?= e($data['filters']['date_from']) ?>">
            </div>
            <div class="form-group">
                <label class="form-label">Expiry Date To</label>
                <input type="date" name="date_to" class="form-control" value="<?= e($data['filters']['date_to']) ?>">
            </div>
            <div class="form-group">
                <label class="form-label">&nbsp;</label>
                <a href="<?= APP_URL ?>/reports?report=expiry" class="btn btn-outline w-full">Clear Filters</a>
            </div>
        </div>
    </form>
</div>

<style>
/* Base Compact Styles */
.page-header {
    margin-bottom: 0.75rem;
}

.page-header h1 {
    font-size: 1.25rem;
    margin-bottom: 0.25rem;
}

.page-subtitle {
    font-size: 0.75rem;
}

.card {
    margin-bottom: 0.75rem;
    padding: 0.5rem;
}

.card-header {
    padding: 0.5rem 0.5rem 0.25rem 0.5rem;
}

.card-title {
    font-size: 0.875rem;
    font-weight: 600;
}

/* Responsive Grid Layout */
.responsive-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0.5rem;
}

.expiry-summary {
    grid-template-columns: repeat(4, minmax(0, 1fr));
}

/* Compact Card */
.compact-card {
    padding: 0.5rem;
}

.compact-card .p-3 {
    padding: 0.5rem !important;
}

/* Compact Table */
.expiry-table {
    font-size: 0.7rem;
}

.expiry-table thead th {
    padding: 0.3rem 0.4rem;
    font-size: 0.65rem;
    line-height: 1.1;
    white-space: nowrap;
    font-weight: 600;
}

.expiry-table tbody td {
    padding: 0.3rem 0.4rem;
    font-size: 0.7rem;
    line-height: 1.3;
}

.expiry-table .cell-product {
    min-width: 120px;
}

.expiry-table .cell-batch,
.expiry-table .cell-sku,
.expiry-table .cell-value {
    white-space: nowrap;
}

/* Compact Forms */
.form-group {
    margin-bottom: 0.5rem;
}

.form-label {
    font-size: 0.7rem;
    margin-bottom: 0.25rem;
}

.form-control {
    padding: 0.4rem 0.5rem;
    font-size: 0.75rem;
}

/* Compact Buttons */
.btn-sm {
    padding: 0.35rem 0.6rem;
    font-size: 0.7rem;
}

/* Compact Summary Cards */
.summary-cards .text-xs {
    font-size: 0.65rem;
}

.summary-cards .text-xl {
    font-size: 1.1rem;
}

/* Responsive Header */
.responsive-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.5rem;
}
@media (min-width: 640px) {
    .responsive-header {
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
        gap: 0;
    }
}

/* Tablet Responsive */
@media (max-width: 1024px) {
    .responsive-grid {
        grid-template-columns: repeat(2, 1fr);
    }
    .expiry-summary {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

/* Mobile Responsive */
@media (max-width: 640px) {
    .responsive-grid {
        grid-template-columns: 1fr;
    }
    .expiry-summary {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
    
    .page-header h1 {
        font-size: 1rem;
    }
    
    .page-subtitle {
        font-size: 0.7rem;
    }
    
    .card {
        padding: 0.5rem;
    }
    
    .compact-card {
        padding: 0.4rem;
    }
    
    .expiry-table thead th {
        padding: 0.25rem 0.3rem;
        font-size: 0.6rem;
    }
    
    .expiry-table tbody td {
        padding: 0.25rem 0.3rem;
        font-size: 0.65rem;
    }
    
    .btn-sm {
        padding: 0.3rem 0.5rem;
        font-size: 0.65rem;
    }
    
    .form-control {
        padding: 0.35rem 0.5rem;
        font-size: 0.7rem;
    }
    
    .card-title {
        font-size: 0.8rem;
    }
}

/* Small Mobile */
@media (max-width: 480px) {
    .expiry-summary {
        grid-template-columns: 1fr;
    }
    
    .summary-cards .text-xl {
        font-size: 1rem;
    }
    
    .summary-cards .text-xs {
        font-size: 0.6rem;
    }
    
    .page-header h1 {
        font-size: 0.95rem;
    }
}

/* Card view on mobile for table */
@media (max-width: 768px) {
    .expiry-table thead {
        display: none;
    }

    .expiry-table,
    .expiry-table tbody,
    .expiry-table tr,
    .expiry-table td {
        display: block;
        width: 100%;
    }

    .expiry-table tbody tr {
        border-bottom: 1px solid #e5e7eb;
        padding: 0.35rem 0;
    }

    .expiry-table tbody td {
        display: flex;
        justify-content: space-between;
        gap: 0.75rem;
        align-items: flex-start;
        border: 0;
        padding: 0.25rem 0;
        text-align: right;
        font-size: 0.65rem;
    }

    .expiry-table tbody td::before {
        content: attr(data-label);
        font-weight: 600;
        color: #6b7280;
        text-align: left;
        font-size: 0.6rem;
        min-width: 80px;
    }
}

/* Extra small mobile */
@media (max-width: 360px) {
    .responsive-grid {
        gap: 0.35rem;
    }
    
    .expiry-table tbody td {
        font-size: 0.6rem;
    }
    
    .expiry-table tbody td::before {
        font-size: 0.55rem;
        min-width: 70px;
    }
}
</style>

<!-- Summary Cards -->
<div class="responsive-grid gap-2 mb-3 expiry-summary summary-cards">
    <div class="card compact-card">
        <div class="p-3">
            <div class="text-xs text-muted">Total Batches</div>
            <div class="text-xl font-bold"><?= $data['summary']['total_batches'] ?></div>
        </div>
    </div>
    <div class="card compact-card">
        <div class="p-3">
            <div class="text-xs text-muted">Expired</div>
            <div class="text-xl font-bold text-red-600"><?= $data['summary']['expired_count'] ?></div>
        </div>
    </div>
    <div class="card compact-card">
        <div class="p-3">
            <div class="text-xs text-muted">Expiring Soon (0-30 days)</div>
            <div class="text-xl font-bold text-orange-600"><?= $data['summary']['expiring_soon_count'] ?></div>
        </div>
    </div>
    <div class="card compact-card">
        <div class="p-3">
            <div class="text-xs text-muted">Expiring (0-90 days)</div>
            <div class="text-xl font-bold text-yellow-600"><?= $data['summary']['expiring_count'] ?></div>
        </div>
    </div>
</div>

<!-- Expiry Table -->
<div class="card">
    <div class="card-header">
        <h3 class="card-title">Product Expiry Details</h3>
    </div>
    <div class="overflow-x-auto">
        <table class="data-table expiry-table">
            <thead>
                <tr>
                    <th class="text-left">Batch No</th>
                    <th class="text-left">Product</th>
                    <th class="text-left">SKU</th>
                    <th class="text-left">Category</th>
                    <th class="text-left">Location</th>
                    <th class="text-center">Quantity</th>
                    <th class="text-center">Mfg Date</th>
                    <th class="text-center">Expiry Date</th>
                    <th class="text-center">Days to Expiry</th>
                    <th class="text-center">Status</th>
                    <th class="text-center">Cost Value</th>
                    <th class="text-center">Retail Value</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($data['expiry_data'] as $item): ?>
                <tr>
                    <td class="text-left cell-batch" data-label="Batch No">
                        <code><?= e($item['batch_no']) ?></code>
                    </td>
                    <td class="text-left cell-product" data-label="Product"><?= e($item['product']) ?></td>
                    <td class="text-left cell-sku" data-label="SKU"><?= e($item['sku']) ?></td>
                    <td class="text-left" data-label="Category"><?= e($item['category']) ?></td>
                    <td class="text-left" data-label="Location"><?= e($item['location']) ?></td>
                    <td class="text-center" data-label="Quantity"><?= number_format($item['quantity']) ?></td>
                    <td class="text-center" data-label="Mfg Date"><?= $item['mfg_date'] ? date('M j, Y', strtotime($item['mfg_date'])) : 'N/A' ?></td>
                    <td class="text-center" data-label="Expiry Date"><?= date('M j, Y', strtotime($item['expiry_date'])) ?></td>
                    <td class="text-center font-bold <?= $item['days_to_expiry'] < 0 ? 'text-red-600' : ($item['days_to_expiry'] <= 30 ? 'text-orange-600' : ($item['days_to_expiry'] <= 90 ? 'text-yellow-600' : 'text-green-600')) ?>" data-label="Days to Expiry">
                        <?= $item['days_to_expiry'] < 0 ? abs($item['days_to_expiry']) . ' days ago' : $item['days_to_expiry'] . ' days' ?>
                    </td>
                    <td class="text-center" data-label="Status">
                        <?php
                        $statusColors = [
                            'expired' => 'bg-red-100 text-red-800',
                            'expiring_soon' => 'bg-orange-100 text-orange-800',
                            'expiring' => 'bg-yellow-100 text-yellow-800',
                            'safe' => 'bg-green-100 text-green-800'
                        ];
                        $statusLabels = [
                            'expired' => 'Expired',
                            'expiring_soon' => 'Expiring Soon',
                            'expiring' => 'Expiring',
                            'safe' => 'Safe'
                        ];
                        ?>
                        <span class="px-2 py-1 rounded text-xs font-semibold <?= $statusColors[$item['expiry_status']] ?>">
                            <?= $statusLabels[$item['expiry_status']] ?>
                        </span>
                    </td>
                    <td class="text-center text-blue-600 cell-value" data-label="Cost Value"><?= number_format($item['cost_value'], 2) ?></td>
                    <td class="text-center text-green-600 cell-value" data-label="Retail Value"><?= number_format($item['retail_value'], 2) ?></td>
                </tr>
                <?php endforeach; ?>
                <?php if (empty($data['expiry_data'])): ?>
                <tr>
                    <td colspan="12" class="text-center text-muted py-8">
                        No expiry data found. Products with batch tracking and expiry dates will appear here.
                    </td>
                </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
    
    <?php if (isset($data['pagination'])): ?>
    <?= paginationLinks($data['pagination'], APP_URL . '/reports?report=expiry', $data['filters']) ?>
    <?php endif; ?>
</div>
