@extends('dashboard')
@section('title','Distribution List')
@section('page_content')
<div class="container-fluid py-4">
    <div class="d-flex flex-column flex-md-row justify-content-between align-items-md-center mb-4 gap-3">
        <div>
            <h4 class="mb-1 fw-bold">Food Distribution History</h4>
            <p class="text-muted small mb-0">Total successful distributions logged in the system.</p>
        </div>
        <div class="d-flex gap-2">
            <a href="{{ route('distributions.print', request()->query()) }}" target="_blank" class="btn btn-success shadow-sm">
                <i class="fas fa-print me-1"></i> Print List
            </a>
            <a href="{{ route('distributions.select_word') }}" class="btn btn-primary shadow-sm">
                <i class="fas fa-plus-circle me-1"></i> New Distribution
            </a>
        </div>
    </div>

    <div class="card shadow-sm border-0 mb-4">
        <div class="card-body">
            <form action="{{ route('distributions.index') }}" method="GET" class="row g-3">
                <div class="col-md-3">
                    <label class="form-label small text-muted">Search Name/NID</label>
                    <input type="text" name="search" class="form-control" value="{{ request('search') }}" placeholder="Client Name or NID...">
                </div>

                <div class="col-md-2">
                    <label class="form-label small text-muted">Word No</label>
                    <select name="word_number" class="form-select">
                        <option value="">All Word</option>
                        @foreach($wordNumbers as $word)
                            <option value="{{ $word }}" {{ request('word_number') == $word ? 'selected' : '' }}>Word {{ $word }}</option>
                        @endforeach
                    </select>
                </div>

                <div class="col-md-3">
                    <label class="form-label small text-muted">Sale Point / Dealer</label>
                    <select name="dealer_id" class="form-select">
                        <option value="">All Sale Points</option>
                        @foreach($dealers as $dealer)
                            <option value="{{ $dealer->id }}" {{ request('dealer_id') == $dealer->id ? 'selected' : '' }}>
                                {{ $dealer->sale_point }} ({{ $dealer->name }})
                            </option>
                        @endforeach
                    </select>
                </div>

                <div class="col-md-2">
                    <label class="form-label small text-muted">Start Date</label>
                    <input type="date" name="start_date" class="form-control" value="{{ request('start_date') }}">
                </div>

                <div class="col-md-2">
                    <label class="form-label small text-muted">End Date</label>
                    <input type="date" name="end_date" class="form-control" value="{{ request('end_date') }}">
                </div>

                <div class="col-12 d-flex justify-content-end gap-2">
                    <a href="{{ route('distributions.index') }}" class="btn btn-light border">Reset</a>
                    <button type="submit" class="btn btn-dark px-4">Filter Search</button>
                </div>
            </form>
        </div>
    </div>

    <div class="table-responsive">
        <table class="table table-bordered table-sm align-middle m-0" style="border: 1px solid #ccc; font-weight: normal;">
            <thead>
                <tr style="background-color: #f8f9fa;">
                    <th style="padding: 8px; font-weight: 600; border: 1px solid #ccc;">ক্রমিক নং</th>
                    <th style="padding: 8px; font-weight: 600; border: 1px solid #ccc;">এন আই ডি</th>
                    <th style="padding: 8px; font-weight: 600; border: 1px solid #ccc;">ভোক্তার নাম</th>
                    <th style="padding: 8px; font-weight: 600; border: 1px solid #ccc;">পণ্যের নাম</th>
                    <th style="padding: 8px; font-weight: 600; border: 1px solid #ccc;">তারিখ এবং সময়</th>
                    <th style="padding: 8px; font-weight: 600; border: 1px solid #ccc;">ডিস্ট্রিবিউটর</th>
                </tr>
            </thead>
            <tbody>
                @forelse($distributions as $distribution)
                <tr>
                    <td style="padding: 8px; border: 1px solid #ccc;">{{ $loop->iteration + ($distributions->currentPage() - 1) * $distributions->perPage() }}</td>
                    <td style="padding: 8px; border: 1px solid #ccc;">{{ $distribution->nid_number }}</td>
                    <td style="padding: 8px; border: 1px solid #ccc;">{{ $distribution->client->client_name ?? 'N/A' }}</td>
                    <td style="padding: 8px; border: 1px solid #ccc;">{{ $distribution->client->product_name ?? 'N/A' }}</td>
                    <td style="padding: 8px; border: 1px solid #ccc;">
                        {{ \Carbon\Carbon::parse($distribution->distributed_at)->format('d M, Y') }} - {{ $distribution->created_at->format('h:i A') }}
                    </td>
                    <td style="padding: 8px; border: 1px solid #ccc;">
                        {{ $distribution->user->name ?? 'N/A' }} ({{ $distribution->dealer->sale_point ?? 'N/A' }})
                    </td>
                </tr>
                @empty
                <tr>
                    <td colspan="6" style="padding: 15px; border: 1px solid #ccc; text-align: center; color: #6c757d;">
                        No distribution records found.
                    </td>
                </tr>
                @endforelse
            </tbody>
        </table>
    </div>

    <div class="mt-4 d-flex justify-content-center">
        {{ $distributions->links('pagination::bootstrap-5') }}
    </div>
</div>
@endsection