@extends('layouts.master')
@section('title', 'Supplier Listing Report')
@section('content')

<div class="container-fluid py-3">
    <div class="d-flex justify-content-end mb-3">
        <a href="{{ route('home') }}" class="btn-back">
            <i class="fas fa-arrow-left me-2"></i> Back to Dashboard
        </a>
    </div>

    <div class="card shadow-sm">
        <div class="card-header bg-primary text-white py-3">
            <h5 class="mb-0"><i class="fas fa-truck me-2"></i> Supplier Listing Report</h5>
        </div>
        
        <div class="card-body">
            @if(session('success'))
                <div class="alert alert-success">{{ session('success') }}</div>
            @endif
            
            @if($errors->any())
                <div class="alert alert-danger">
                    <ul class="mb-0">
                        @foreach($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif
            
            <form action="{{ route('supplierListingReport.generate') }}" method="POST" id="supplierForm">
                @csrf
                <div class="row mb-3">
                    <div class="col-md-6">
                        <label class="form-label"><strong>From Supplier</strong></label>
                        <select class="form-control select2" name="from_supplier" id="from_supplier" required>
                            <option value="" disabled selected>Select Supplier</option>
                            <option value="all" {{ (isset($fromSupplierId) && $fromSupplierId == 'all') ? 'selected' : '' }}>All Suppliers</option>
                            @foreach($allSuppliers ?? $suppliers as $supplier)
                                <option value="{{ $supplier->id }}" {{ (isset($fromSupplierId) && $fromSupplierId == $supplier->id) ? 'selected' : '' }}>
                                    {{ $supplier->supplier_code }} - {{ $supplier->supplier_account }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label"><strong>To Supplier</strong></label>
                        <select class="form-control select2" name="to_supplier" id="to_supplier" required>
                            <option value="" disabled selected>Select Supplier</option>
                            @foreach($allSuppliers ?? $suppliers as $supplier)
                                <option value="{{ $supplier->id }}" {{ (isset($toSupplierId) && $toSupplierId == $supplier->id) ? 'selected' : '' }}>
                                    {{ $supplier->supplier_code }} - {{ $supplier->supplier_account }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                </div>
                
                <div class="d-flex justify-content-end">
                    <button type="submit" class="btn btn-primary me-2">Search</button>
                    <button type="reset" class="btn btn-secondary" onclick="resetForm()">Refresh</button>
                </div>
            </form>
        </div>
    </div>
    
    @if(isset($suppliers) && $suppliers->count() > 0 && isset($fromSupplierId) && (isset($toSupplierId) || $fromSupplierId == 'all'))
        <div class="card shadow-sm mt-4">
            <div class="card-header bg-light py-3 d-flex justify-content-between align-items-center">
                <h6 class="mb-0"><i class="fas fa-list me-2"></i> Supplier Results</h6>
                <div>
                    <form action="{{ route('supplierListingReport.downloadPdf') }}" method="POST" class="d-inline">
                        @csrf
                        <input type="hidden" name="from_supplier" value="{{ $fromSupplierId }}">
                        <input type="hidden" name="to_supplier" value="{{ $toSupplierId ?? '' }}">
                        <button type="submit" class="btn btn-process-all btn-sm">
                            <i class="fas fa-download me-1"></i> DOWNLOAD PDF
                        </button>
                    </form>
                </div>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-bordered table-hover">
                        <thead class="table-light">
                            <tr>
                                <th>Code</th>
                                <th>Account Name</th>
                                <th>Contact Person</th>
                                <th>Address</th>
                                <th>Contact Numbers</th>
                                <th>Email</th>
                                <th>Category</th>
                                <th>Ageing</th>
                                <th>Area</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($suppliers as $supplier)
                                <tr>
                                    <td>{{ $supplier->supplier_code }}</td>
                                    <td>{{ $supplier->supplier_account }}</td>
                                    <td>{{ $supplier->contact_person }}</td>
                                    <td>{{ $supplier->address }}</td>
                                    <td>
                                        {{ $supplier->contact_1 }}
                                        @if($supplier->contact_2)
                                            / {{ $supplier->contact_2 }}
                                        @endif
                                    </td>
                                    <td>{{ $supplier->email }}</td>
                                    <td>{{ $supplier->category->name ?? 'N/A' }}</td>
                                    <td>{{ $supplier->ageAnalysis->description ?? 'N/A' }}</td>
                                    <td>{{ $supplier->area->name ?? 'N/A' }}</td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    @elseif(isset($suppliers) && $suppliers->count() == 0 && isset($fromSupplierId) && (isset($toSupplierId) || $fromSupplierId == 'all'))
        <div class="card shadow-sm mt-4">
            <div class="card-body">
                <div class="alert alert-info mt-3">
                    No suppliers found in the selected range.
                </div>
            </div>
        </div>
    @endif
</div>

@endsection

@section('styles')
<style>
.btn-back {
    text-decoration: none;
    color: #1AB9D9;
    font-weight: 500;
    display: inline-flex;
    align-items: center;
    padding: 5px 15px;
    border-radius: 4px;
    transition: all 0.3s;
}
.btn-back:hover {
    background-color: rgba(26, 185, 217, 0.1);
    text-decoration: none;
}
.card {
    border-radius: 8px;
    overflow: hidden;
    border: none;
    box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.1);
}
.card-header {
    border-bottom: 1px solid rgba(0,0,0,0.1);
    padding: 12px 20px;
}
.table th {
    background-color: #f8f9fa;
}
.table-responsive {
    overflow-x: auto;
}
.table-active {
    background-color: rgba(0,0,0,.05) !important;
}
.btn-process-all {
    background-color: #ffffff !important;
    color: #1AB9D9 !important;
    border: 1px solid rgba(26,185,217,0.35) !important;
    font-weight: 600 !important;
    padding: 7px 14px !important;
    box-shadow: none !important;
    line-height: 1 !important;
}
.btn-process-all:hover {
    background-color: #f5fbfc !important;
    color: #1498b0 !important;
    border-color: rgba(20,152,176,0.45) !important;
}

@media print {
    .btn-back, .card-header .btn, form {
        display: none !important;
    }
    .card {
        box-shadow: none;
        border: 1px solid #ddd;
    }
    .table-responsive {
        overflow-x: visible;
    }
}
</style>
@endsection

@section('scripts')
<script>
    // Initialize Select2 for dropdowns
    $(document).ready(function() {
        $('.select2').select2({
            placeholder: "Select Supplier",
            allowClear: true
        });
        
        // Handle "All Suppliers" selection
        $('#from_supplier').on('change', function() {
            if ($(this).val() === 'all') {
                $('#to_supplier').prop('disabled', true).val(null).trigger('change');
            } else {
                $('#to_supplier').prop('disabled', false);
            }
        });
        
        // Initialize state on page load
        if ($('#from_supplier').val() === 'all') {
            $('#to_supplier').prop('disabled', true);
        }
    });

    function resetForm() {
        // Reset the select2 dropdowns
        $('#from_supplier').val(null).trigger('change');
        $('#to_supplier').val(null).trigger('change').prop('disabled', false);
    }
</script>
@endsection