<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">Detailed sales analysis and performance metrics</div>
    </div>
    <div class="flex gap-2 flex-wrap">
        <a href="<?= APP_URL ?>/reports?report=sales&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=sales&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="sales">
        <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">Customer</label>
                <select name="customer_id" class="form-control">
                    <option value="">All Customers</option>
                    <?php
                    $customers = db()->prepare("SELECT id, name FROM customers WHERE tenant_id = ? ORDER BY name");
                    $customers->execute([Auth::tenantId()]);
                    foreach ($customers->fetchAll() as $customer):
                    ?>
                    <option value="<?= $customer['id'] ?>" <?= $data['filters']['customer_id'] == $customer['id'] ? 'selected' : '' ?>>
                        <?= e($customer['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">Status</label>
                <select name="status" class="form-control">
                    <option value="">All Status</option>
                    <option value="completed" <?= $data['filters']['status'] == 'completed' ? 'selected' : '' ?>>Completed</option>
                    <option value="cancelled" <?= $data['filters']['status'] == 'cancelled' ? 'selected' : '' ?>>Cancelled</option>
                    <option value="refunded" <?= $data['filters']['status'] == 'refunded' ? 'selected' : '' ?>>Refunded</option>
                </select>
            </div>
            <div class="form-group">
                <label class="form-label">User</label>
                <select name="user_id" class="form-control">
                    <option value="">All Users</option>
                    <?php
                    $users = db()->prepare("SELECT id, name FROM users WHERE tenant_id = ? ORDER BY name");
                    $users->execute([Auth::tenantId()]);
                    foreach ($users->fetchAll() as $user):
                    ?>
                    <option value="<?= $user['id'] ?>" <?= ($data['filters']['user_id'] ?? '') == $user['id'] ? 'selected' : '' ?>>
                        <?= e($user['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=sales" 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;
}

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

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

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

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

.sales-table .cell-date,
.sales-table .cell-value {
    white-space: nowrap;
}

.sales-table .cell-customer {
    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);
    }
    .sales-summary {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (max-width: 640px) {
    .responsive-grid {
        grid-template-columns: 1fr;
    }
    .sales-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;
    }
    
    .sales-table thead th {
        padding: 0.35rem 0.4rem;
        font-size: 0.6rem;
    }
    
    .sales-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) {
    .sales-table thead {
        display: none;
    }

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

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

    .sales-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;
    }

    .sales-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 sales-summary summary-cards">
    <div class="card compact-card">
        <div class="p-3">
            <div class="text-xs text-muted">Total Sales</div>
            <div class="text-xl font-bold"><?= $data['summary']['total_sales'] ?></div>
        </div>
    </div>
    <div class="card compact-card">
        <div class="p-3">
            <div class="text-xs text-muted">Total Revenue</div>
            <div class="text-xl font-bold text-green-600"><?= money($data['summary']['total_revenue']) ?></div>
        </div>
    </div>
    <div class="card compact-card">
        <div class="p-3">
            <div class="text-xs text-muted">Items Sold</div>
            <div class="text-xl font-bold"><?= number_format($data['summary']['total_items_sold']) ?></div>
        </div>
    </div>
    <div class="card compact-card">
        <div class="p-3">
            <div class="text-xs text-muted">Average Sale</div>
            <div class="text-xl font-bold"><?= money($data['summary']['average_sale_value']) ?></div>
        </div>
    </div>
</div>

<!-- Sales Table -->
<div class="card">
    <div class="card-header">
        <h3 class="card-title">Sales Details</h3>
    </div>
    <div class="overflow-x-auto">
        <table class="data-table sales-table">
            <thead>
                <tr>
                    <th class="text-left">Sale No</th>
                    <th class="text-left">Date</th>
                    <th class="text-left">Customer</th>
                    <th class="text-left">Location</th>
                    <th class="text-left">User</th>
                    <th class="text-center">Items</th>
                    <th class="text-center">Discount</th>
                    <th class="text-center">Total</th>
                    <th class="text-center">Payment</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($data['sales'] as $sale): ?>
                <tr>
                    <td class="text-left cell-value" data-label="Sale No"><?= e($sale['sale_no']) ?></td>
                    <td class="text-left cell-date" data-label="Date"><?= date('M j, Y H:i', strtotime($sale['created_at'])) ?></td>
                    <td class="text-left cell-customer" data-label="Customer"><?= e($sale['customer'] ?? 'Walk-in') ?></td>
                    <td class="text-left" data-label="Location"><?= e($sale['location']) ?></td>
                    <td class="text-left" data-label="User"><?= e($sale['created_by'] ?? 'System') ?></td>
                    <td class="text-center" data-label="Items"><?= number_format($sale['item_count']) ?></td>
                    <td class="text-center cell-value" data-label="Discount"><?= money($sale['discount_amount']) ?></td>
                    <td class="text-center font-bold cell-value" data-label="Total"><?= money($sale['total']) ?></td>
                    <td class="text-center" data-label="Payment">
                        <span class="badge bg-blue-100 text-blue-800">
                            <?= e($sale['payment_method']) ?>
                        </span>
                    </td>
                </tr>
                <?php endforeach; ?>
                <tr class="font-bold bg-blue-50 border-t-2 border-blue-200">
                    <td colspan="7" class="text-right">TOTALS:</td>
                    <td class="text-center"><?= money($data['summary']['total_revenue']) ?></td>
                    <td class="text-center"></td>
                </tr>
            </tbody>
        </table>
    </div>
    
    <?php if (isset($data['pagination'])): ?>
    <?= paginationLinks($data['pagination'], APP_URL . '/reports?report=sales', $data['filters']) ?>
    <?php endif; ?>
</div>
