<div>
    <x-layouts.app title="{{ __('Session Report') }}">
        <!-- Page Header -->
        <div class="page-header d-flex justify-content-between align-items-center">
            <div>
                <h1 class="page-title">{{ __('Session Report') }}</h1>
                <p class="text-muted mb-0">{{ __('POS session history and cash tracking') }}</p>
            </div>
            <div class="d-flex gap-2">
                <button wire:click="export" class="btn btn-outline-success">
                    <i class="fas fa-file-excel me-2"></i>{{ __('Export') }}
                </button>
                <a href="{{ route('pdf.report.sessions', ['date_from' => $dateFrom, 'date_to' => $dateTo, 'user_id' => $userFilter, 'status' => $statusFilter]) }}" class="btn btn-outline-info">
                    <i class="fas fa-download me-2"></i>{{ __('Download PDF') }}
                </a>
                <a href="{{ route('pdf.report.sessions', ['date_from' => $dateFrom, 'date_to' => $dateTo, 'user_id' => $userFilter, 'status' => $statusFilter, 'preview' => true]) }}" target="_blank" class="btn btn-outline-secondary">
                    <i class="fas fa-print me-2"></i>{{ __('Print') }}
                </a>
            </div>
        </div>

        <!-- Summary Cards -->
        <div class="row g-3 mb-4">
            <div class="col-md-3">
                <div class="card bg-primary text-white h-100">
                    <div class="card-body text-center">
                        <h6 class="text-white-50">{{ __('Total Sessions') }}</h6>
                        <h2 class="mb-0">{{ $totals['total_sessions'] }}</h2>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card bg-success text-white h-100">
                    <div class="card-body text-center">
                        <h6 class="text-white-50">{{ __('Total Sales') }}</h6>
                        <h4 class="mb-0">{{ $currency }} {{ number_format($totals['total_sales'], 0) }}</h4>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card bg-info text-white h-100">
                    <div class="card-body text-center">
                        <h6 class="text-white-50">{{ __('Cash Sales') }}</h6>
                        <h4 class="mb-0">{{ $currency }} {{ number_format($totals['total_cash_sales'], 0) }}</h4>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card {{ $totals['short_over'] < 0 ? 'bg-danger' : ($totals['short_over'] > 0 ? 'bg-warning' : 'bg-secondary') }} text-white h-100">
                    <div class="card-body text-center">
                        <h6 class="{{ $totals['short_over'] > 0 ? 'text-dark' : 'text-white-50' }}">{{ __('Short/Over') }}</h6>
                        <h4 class="mb-0 {{ $totals['short_over'] > 0 ? 'text-dark' : '' }}">
                            {{ $totals['short_over'] >= 0 ? '+' : '' }}{{ $currency }} {{ number_format($totals['short_over'], 2) }}
                        </h4>
                    </div>
                </div>
            </div>
        </div>

        <!-- Filters -->
        <div class="card mb-4">
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-md-3">
                        <label class="form-label">{{ __('From Date') }}</label>
                        <input type="date" class="form-control" wire:model.live="dateFrom">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">{{ __('To Date') }}</label>
                        <input type="date" class="form-control" wire:model.live="dateTo">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">{{ __('User') }}</label>
                        <select class="form-select" wire:model.live="userFilter">
                            <option value="">{{ __('All') }}</option>
                            @foreach($users as $user)
                                <option value="{{ $user->id }}">{{ $user->name }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">{{ __('Status') }}</label>
                        <select class="form-select" wire:model.live="statusFilter">
                            <option value="">{{ __('All') }}</option>
                            <option value="open">{{ __('Open') }}</option>
                            <option value="closed">{{ __('Closed') }}</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>

        <!-- Sessions Table -->
        <div class="card">
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead class="table-light">
                            <tr>
                                <th>{{ __('Date') }}</th>
                                <th>{{ __('User') }}</th>
                                <th>{{ __('Branch') }}</th>
                                <th class="text-end">{{ __('Opening') }}</th>
                                <th class="text-end">{{ __('Cash Sales') }}</th>
                                <th class="text-end">{{ __('Expected') }}</th>
                                <th class="text-end">{{ __('Actual') }}</th>
                                <th class="text-end">{{ __('Diff') }}</th>
                                <th>{{ __('Status') }}</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($sessions as $session)
                                @php
                                    $diff = ($session->actual_closing_cash ?? 0) - ($session->expected_closing_cash ?? 0);
                                @endphp
                                <tr wire:key="sess-{{ $session->id }}">
                                    <td>
                                        <small>{{ $session->opened_at?->format('M j, Y') }}</small>
                                        <small class="text-muted d-block">{{ $session->opened_at?->format('H:i') }}</small>
                                    </td>
                                    <td>{{ $session->user?->name }}</td>
                                    <td>{{ $session->branch?->name }}</td>
                                    <td class="text-end">{{ $currency }} {{ number_format($session->opening_cash, 2) }}</td>
                                    <td class="text-end">{{ $currency }} {{ number_format($session->cash_sales, 0) }}</td>
                                    <td class="text-end">{{ $currency }} {{ number_format($session->expected_closing_cash ?? 0, 2) }}</td>
                                    <td class="text-end">{{ $currency }} {{ number_format($session->actual_closing_cash ?? 0, 2) }}</td>
                                    <td class="text-end">
                                        @if($session->status === 'closed')
                                            <span class="{{ $diff < 0 ? 'text-danger fw-bold' : ($diff > 0 ? 'text-warning fw-bold' : 'text-success') }}">
                                                {{ $diff >= 0 ? '+' : '' }}{{ number_format($diff, 2) }}
                                            </span>
                                        @else
                                            -
                                        @endif
                                    </td>
                                    <td>
                                        <span class="badge bg-{{ $session->status === 'open' ? 'success' : 'secondary' }}">
                                            {{ __(ucfirst($session->status)) }}
                                        </span>
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="9" class="text-center py-4 text-muted">
                                        {{ __('No sessions found.') }}
                                    </td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
            @if($sessions->hasPages())
                <div class="card-footer">
                    {{ $sessions->links() }}
                </div>
            @endif
        </div>
    </x-layouts.app>
</div>
