<div class="page-header responsive-header">
    <div>
        <a href="<?= APP_URL ?>/reports" class="btn btn-ghost btn-sm mb-3">
            <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">Current stock valuation and cost analysis</div>
    </div>
    <div class="flex gap-2 flex-wrap">
        <a href="<?= APP_URL ?>/reports?report=stock-valuation&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=stock-valuation&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="stock-valuation">
        <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">&nbsp;</label>
                <button type="submit" class="btn btn-primary w-full">Apply Filters</button>
            </div>
            <div class="form-group">
                <label class="form-label">&nbsp;</label>
                <a href="<?= APP_URL ?>/reports?report=stock-valuation" 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;
}

.stock-valuation-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 */
.stock-valuation-table {
    font-size: 0.7rem;
}

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

.stock-valuation-table tbody td,
.stock-valuation-table tfoot td {
    padding: 0.3rem 0.4rem;
    font-size: 0.7rem;
    line-height: 1.3;
}

.stock-valuation-table .cell-value {
    white-space: nowrap;
}

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

/* 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);
    }
    .stock-valuation-summary {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

/* Mobile Responsive */
@media (max-width: 640px) {
    .responsive-grid {
        grid-template-columns: 1fr;
    }
    .stock-valuation-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;
    }
    
    .stock-valuation-table thead th {
        padding: 0.25rem 0.3rem;
        font-size: 0.6rem;
    }
    
    .stock-valuation-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) {
    .stock-valuation-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) {
    .stock-valuation-table thead {
        display: none;
    }

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

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

    .stock-valuation-table tbody td,
    .stock-valuation-table tfoot 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;
    }

    .stock-valuation-table tbody td::before,
    .stock-valuation-table tfoot 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;
    }
    
    .stock-valuation-table tbody td {
        font-size: 0.6rem;
    }
    
    .stock-valuation-table tbody td::before {
        font-size: 0.55rem;
        min-width: 70px;
    }
}
</style>

<!-- Summary Cards -->
<div class="responsive-grid gap-2 mb-3 stock-valuation-summary summary-cards">
    <div class="card compact-card">
        <div class="p-3">
            <div class="text-xs text-muted">Total Products</div>
            <div class="text-xl font-bold"><?= $data['summary']['total_products'] ?></div>
        </div>
    </div>
    <div class="card compact-card">
        <div class="p-3">
            <div class="text-xs text-muted">Total Stock Value</div>
            <div class="text-xl font-bold text-green-600"><?= money($data['summary']['total_stock_value']) ?></div>
        </div>
    </div>
    <div class="card compact-card">
        <div class="p-3">
            <div class="text-xs text-muted">Total Stock Cost</div>
            <div class="text-xl font-bold text-red-600"><?= money($data['summary']['total_stock_cost']) ?></div>
        </div>
    </div>
    <div class="card compact-card">
        <div class="p-3">
            <div class="text-xs text-muted">Potential Profit</div>
            <div class="text-xl font-bold text-blue-600"><?= money($data['summary']['potential_profit']) ?></div>
        </div>
    </div>
</div>

<!-- Stock Valuation Table -->
<div class="card">
    <div class="card-header">
        <h3 class="card-title">Stock Valuation Details</h3>
    </div>
    <div class="overflow-x-auto">
        <?php if (empty($data['stock_data'])): ?>
        <div class="p-4 text-center text-muted">
            <p>No stock data found. This could mean:</p>
            <ul class="text-left mt-2">
                <li>No products exist in the database</li>
                <li>All products have been filtered out by category or location</li>
                <li>There may be an issue with the data structure</li>
            </ul>
        </div>
        <?php else: ?>
        <table class="data-table stock-valuation-table">
            <thead>
                <tr>
                    <th class="text-left">Category</th>
                    <th class="text-left">Product</th>
                    <th class="text-left">SKU</th>
                    <th class="text-left">Location</th>
                    <th class="text-center">Quantity</th>
                    <th class="text-center">Cost Price</th>
                    <th class="text-center">Selling Price</th>
                    <th class="text-center">Cost Value</th>
                    <th class="text-center">Retail Value</th>
                    <th class="text-center">Potential Profit</th>
                    <th class="text-center">Profit Margin</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($data['stock_data'] as $item): ?>
                <tr>
                    <td class="text-left" data-label="Category"><?= e($item['category']) ?></td>
                    <td class="text-left cell-product" data-label="Product"><?= e($item['product']) ?></td>
                    <td class="text-left" data-label="SKU"><?= e($item['sku']) ?></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 cell-value" data-label="Cost Price"><?= money($item['cost_price']) ?></td>
                    <td class="text-center cell-value" data-label="Selling Price"><?= money($item['selling_price']) ?></td>
                    <td class="text-center text-blue-600 cell-value" data-label="Cost Value"><?= money($item['cost_value']) ?></td>
                    <td class="text-center text-green-600 cell-value" data-label="Retail Value"><?= money($item['retail_value']) ?></td>
                    <td class="text-center <?= $item['potential_profit'] >= 0 ? 'text-green-600' : 'text-red-600' ?> font-bold cell-value" data-label="Potential Profit">
                        <?= money($item['potential_profit']) ?>
                    </td>
                    <td class="text-center cell-value" data-label="Profit Margin">
                        <?php 
                        $margin = $item['selling_price'] > 0 ? (($item['selling_price'] - $item['cost_price']) / $item['selling_price']) * 100 : 0;
echo htmlspecialchars(number_format($margin, 1)) . '%';
                        ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
            <tfoot>
                <tr class="font-bold bg-blue-50 border-t-2 border-blue-200">
                    <td colspan="7" class="text-left">TOTAL</td>
                    <td class="text-center text-blue-600 cell-value" data-label="Cost Value"><?= money($data['summary']['total_cost_value']) ?></td>
                    <td class="text-center text-green-600 cell-value" data-label="Retail Value"><?= money($data['summary']['total_retail_value']) ?></td>
                    <td class="text-center <?= $data['summary']['total_potential_profit'] >= 0 ? 'text-green-600' : 'text-red-600' ?> cell-value" data-label="Potential Profit">
                        <?= money($data['summary']['total_potential_profit']) ?>
                    </td>
                    <td class="text-center cell-value" data-label="Profit Margin">
                        <?php
                        $overall_margin = $data['summary']['total_retail_value'] > 0 ?
                            ($data['summary']['total_potential_profit'] / $data['summary']['total_retail_value']) * 100 : 0;
echo htmlspecialchars(number_format($overall_margin, 1)) . '%';
                        ?>
                    </td>
                </tr>
            </tfoot>
        </table>
        <?php endif; ?>
    </div>
    
    <?php if (isset($data['pagination'])): ?>
    <?= paginationLinks($data['pagination'], APP_URL . '/reports?report=stock-valuation', $data['filters']) ?>
    <?php endif; ?>
</div>
