@extends('layouts.master')
@section('title', 'Supplier Document Reprint')
@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-print me-2"></i> Supplier Document Reprint</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('supplierDocumentReprint.generate') }}" method="POST" id="supplierDocumentReprintForm">
                @csrf
                <div class="row mb-3">
                    <div class="col-md-3">
                        <label class="form-label"><strong>Supplier Account</strong></label>
                        <select class="form-control select2" name="supplier_account" id="supplier_account" required>
                            <option value="" disabled selected>Select Supplier</option>
                            <option value="all" {{ (isset($supplierId) && $supplierId == 'all') ? 'selected' : '' }}>All Suppliers</option>
                            @foreach($suppliers as $supplier)
                                <option value="{{ $supplier->id }}" {{ (isset($supplierId) && $supplierId == $supplier->id) ? 'selected' : '' }}>
                                    {{ $supplier->supplier_code }} - {{ $supplier->supplier_account }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label"><strong>Date From</strong></label>
                        <input type="date" class="form-control" name="date_from" id="date_from" value="{{ isset($dateFrom) ? $dateFrom : now()->format('Y-m-d') }}" required>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label"><strong>Date To</strong></label>
                        <input type="date" class="form-control" name="date_to" id="date_to" value="{{ isset($dateTo) ? $dateTo : now()->format('Y-m-d') }}" required>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label"><strong>Currency</strong></label>
                        <select class="form-control select2" name="currency" id="currency" required>
                            <option value="" disabled selected>Select Currency</option>
                            @foreach($currencies as $currency)
                                <option value="{{ $currency->code }}" {{ (isset($currencyCode) && $currencyCode == $currency->code) ? 'selected' : '' }}>
                                    {{ $currency->code }} - {{ $currency->name }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                </div>
                
                <div class="d-flex justify-content-end">
                    <button type="submit" class="btn btn-primary me-2">Search Documents</button>
                    <button type="reset" class="btn btn-secondary" onclick="resetForm()">Refresh</button>
                </div>
            </form>
        </div>
    </div>
    
    @if(isset($allDocuments))
        <!-- Display All Suppliers Documents -->
        @foreach($allDocuments as $supplierData)
            @if(count($supplierData['documents']) > 0)
            <div class="card shadow-sm mt-4">
                <div class="card-header bg-light py-3">
                    <h6 class="mb-0"><i class="fas fa-user me-2"></i> {{ $supplierData['supplier']->supplier_account }} ({{ $supplierData['supplier']->supplier_code }})</h6>
                </div>
                <div class="card-body">
                    @if(count($supplierData['documents']) > 0)
                    <div class="table-responsive mb-4">
                        <h6 class="mb-2"><i class="fas fa-file-alt me-2"></i> Documents</h6>
                        <table class="table table-bordered table-hover">
                            <thead class="table-light">
                                <tr>
                                    <th>Date</th>
                                    <th>Type</th>
                                    <th>Document Number</th>
                                    <th>Total Amount</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($supplierData['documents'] as $document)
                                <tr>
                                    <td>{{ $document->document_date }}</td>
                                    <td>{{ ucfirst(str_replace('_', ' ', $document->type)) }}</td>
                                    <td>
                                        <a href="{{ route('supplierDocumentReprint.reprintDocument', $document->id) }}" target="_blank" class="document-link">
                                            {{ $document->document_number }}
                                        </a>
                                    </td>
                                    <td>{{ number_format($document->inclusive_total, 2) }}</td>
                                   
                                    <td>
                                        <a href="{{ route('supplierDocumentReprint.downloadDocument', $document->id) }}" class="btn btn-sm btn-outline-primary">
                                            <i class="fas fa-download me-1"></i> Download
                                        </a>
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                    @endif
                </div>
            </div>
            @endif
        @endforeach
        
    @elseif(isset($documents))
        <!-- Single Supplier View -->
        <div class="card shadow-sm mt-4">
            <div class="card-header bg-light py-3">
                <h6 class="mb-0"><i class="fas fa-user me-2"></i> {{ $supplier->supplier_account }} ({{ $supplier->supplier_code }})</h6>
            </div>
            <div class="card-body">
                @if(count($documents) > 0)
                <div class="table-responsive mb-4">
                    <h6 class="mb-2"><i class="fas fa-file-alt me-2"></i> Documents</h6>
                    <table class="table table-bordered table-hover">
                        <thead class="table-light">
                            <tr>
                                <th>Date</th>
                                <th>Type</th>
                                <th>Document Number</th>
                                <th>Total Amount</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($documents as $document)
                            <tr>
                                <td>{{ $document->document_date }}</td>
                                <td>{{ ucfirst(str_replace('_', ' ', $document->type)) }}</td>
                                <td>
                                    <a href="{{ route('supplierDocumentReprint.reprintDocument', $document->id) }}" target="_blank" class="document-link">
                                        {{ $document->document_number }}
                                    </a>
                                </td>
                                <td>{{ number_format($document->inclusive_total, 2) }}</td>
                                <td>
                                    <a href="{{ route('supplierDocumentReprint.downloadDocument', $document->id) }}" class="btn btn-sm btn-outline-primary">
                                        <i class="fas fa-download me-1"></i> Download
                                    </a>
                                </td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
                @endif
                
                @if(count($documents) == 0)
                    <div class="alert alert-info mt-3">
                        No documents found for the selected criteria.
                    </div>
                @endif
            </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;
}
.document-link {
    color: #1AB9D9;
    text-decoration: none;
    font-weight: 500;
}
.document-link:hover {
    text-decoration: underline;
    color: #1498b0;
}

@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 Option",
            allowClear: true
        });
    });

    function resetForm() {
        // Reset the select2 dropdowns
        $('#supplier_account').val(null).trigger('change');
        $('#currency').val(null).trigger('change');
        $('#date_from').val('{{ now()->format('Y-m-d') }}');
        $('#date_to').val('{{ now()->format('Y-m-d') }}');
    }
</script>
@endsection