<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">Complete history of stock transactions and movements</div>
    </div>
    <div class="flex gap-2 flex-wrap">
        <a href="<?= APP_URL ?>/reports?report=stock-movement&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-movement&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-4">
    <div class="card-header">
        <h3 class="card-title">Filters</h3>
    </div>
    <form method="GET" class="p-3">
        <input type="hidden" name="report" value="stock-movement">
        <div class="responsive-grid gap-3">
            <div class="form-group">
                <label class="form-label">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">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>
                <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-movement" class="btn btn-outline w-full">Clear Filters</a>
            </div>
        </div>
    </form>
</div>

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

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

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

/* Compact Table */
.stock-movement-table thead th {
    padding: 0.4rem 0.5rem;
    font-size: 0.65rem;
    line-height: 1.1;
    white-space: nowrap;
}

.stock-movement-table tbody td {
    padding: 0.4rem 0.5rem;
    font-size: 0.75rem;
}

.stock-movement-table .cell-date,
.stock-movement-table .cell-sku {
    white-space: nowrap;
}

.stock-movement-table .cell-product {
    min-width: 140px;
}

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

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

@media (max-width: 640px) {
    .responsive-grid {
        grid-template-columns: 1fr;
    }
    .stock-movement-summary {
        grid-template-columns: 1fr;
    }
    
    .page-header h1 {
        font-size: 1.1rem;
    }
    
    .page-subtitle {
        font-size: 0.75rem;
    }
    
    .card {
        padding: 1rem;
    }
    
    .compact-card {
        padding: 0.6rem;
    }
    
    .stock-movement-table thead th {
        padding: 0.35rem 0.4rem;
        font-size: 0.6rem;
    }
    
    .stock-movement-table tbody td {
        padding: 0.35rem 0.4rem;
        font-size: 0.7rem;
    }
    
    .btn-sm {
        padding: 0.4rem 0.75rem;
        font-size: 0.75rem;
    }
    
    .form-control {
        padding: 0.5rem 0.75rem;
        font-size: 0.85rem;
    }
}

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

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

    .stock-movement-table tbody tr {
        border-bottom: 1px solid #e5e7eb;
        padding: 0.5rem 0;
    }

    .stock-movement-table tbody td {
        display: flex;
        justify-content: space-between;
        gap: 1rem;
        align-items: flex-start;
        border: 0;
        padding: 0.35rem 0;
        text-align: right;
        font-size: 0.7rem;
    }

    .stock-movement-table tbody td::before {
        content: attr(data-label);
        font-weight: 600;
        color: #6b7280;
        text-align: left;
        font-size: 0.65rem;
    }
}
</style>

<!-- Summary Cards -->
<div class="responsive-grid gap-3 mb-4 stock-movement-summary summary-cards">
    <div class="card compact-card">
        <div class="p-3">
            <div class="text-xs text-muted">Total Movements</div>
            <div class="text-xl font-bold"><?= $data['summary']['total_movements'] ?></div>
        </div>
    </div>
    <div class="card compact-card">
        <div class="p-3">
            <div class="text-xs text-muted">Sales Transactions</div>
            <div class="text-xl font-bold text-green-600"><?= $data['summary']['total_sales'] ?></div>
        </div>
    </div>
    <div class="card compact-card">
        <div class="p-3">
            <div class="text-xs text-muted">Purchase Transactions</div>
            <div class="text-xl font-bold text-blue-600"><?= $data['summary']['total_purchases'] ?></div>
        </div>
    </div>
    <div class="card compact-card">
        <div class="p-3">
            <div class="text-xs text-muted">Net Movement</div>
            <div class="text-xl font-bold text-purple-600">
                <?= $data['summary']['total_purchases'] - $data['summary']['total_sales'] ?>
            </div>
        </div>
    </div>
</div>

<!-- Stock Movement Table -->
<div class="card">
    <div class="card-header">
        <h3 class="card-title">Stock Movement History</h3>
    </div>
    <div class="overflow-x-auto">
        <table class="data-table stock-movement-table">
            <thead>
                <tr>
                    <th class="text-left">Date</th>
                    <th class="text-center">Type</th>
                    <th class="text-left">Reference</th>
                    <th class="text-left">Product</th>
                    <th class="text-left">SKU</th>
                    <th class="text-center">Quantity</th>
                    <th class="text-left">Location</th>
                    <th class="text-left">Customer/Supplier</th>
                    <th class="text-left">Created By</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($data['movements'] as $movement): ?>
                <tr>
                    <td class="text-left cell-date" data-label="Date"><?= date('M j, Y H:i', strtotime($movement['created_at'])) ?></td>
                    <td class="text-center" data-label="Type">
                        <?php if ($movement['movement_type'] == 'Sale'): ?>
                            <span class="badge bg-red-100 text-red-800">
                                <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" class="inline mr-1">
                                    <polyline points="22 12 18 12 15 21 9 3 6 12 2 12"></polyline>
                                </svg>
                                Sale
                            </span>
                        <?php else: ?>
                            <span class="badge bg-green-100 text-green-800">
                                <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" class="inline mr-1">
                                    <path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z"></path>
                                </svg>
                                Purchase
                            </span>
                        <?php endif; ?>
                    </td>
                    <td class="text-left" data-label="Reference"><?= e($movement['reference']) ?></td>
                    <td class="text-left cell-product" data-label="Product"><?= e($movement['product']) ?></td>
                    <td class="text-left cell-sku" data-label="SKU"><?= e($movement['sku']) ?></td>
                    <td class="text-center font-bold <?= $movement['movement_type'] == 'Sale' ? 'text-red-600' : 'text-green-600' ?>" data-label="Quantity">
                        <?= $movement['movement_type'] == 'Sale' ? '-' : '+' ?><?= number_format($movement['quantity_change']) ?>
                    </td>
                    <td class="text-left" data-label="Location"><?= e($movement['location']) ?></td>
                    <td class="text-left" data-label="Customer/Supplier"><?= e($movement['customer_or_supplier'] ?? 'N/A') ?></td>
                    <td class="text-left" data-label="Created By"><?= e($movement['created_by'] ?? 'System') ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    
    <?php if (isset($data['pagination'])): ?>
    <?= paginationLinks($data['pagination'], APP_URL . '/reports?report=stock-movement', $data['filters']) ?>
    <?php endif; ?>
</div>
