@extends('layouts.master')
@section('title', 'Outstanding Payments 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-file-invoice-dollar me-2"></i> Outstanding Payments 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('outstandingPaymentsReport.generate') }}" method="POST" id="reportForm">
                @csrf
                <div class="row mb-3">
                    <div class="col-md-4">
                        <label class="form-label"><strong>Report Type</strong></label>
                        <select class="form-control select2" name="report_type" id="report_type" required>
                            <option value="summary" {{ (isset($reportType) && $reportType == 'summary') ? 'selected' : '' }}>Summary</option>
                            <option value="detailed" {{ (isset($reportType) && $reportType == 'detailed') ? 'selected' : '' }}>Detailed</option>
                        </select>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label"><strong>From Date</strong></label>
                        <input type="date" class="form-control" name="from_date" id="from_date" 
                               value="{{ isset($fromDate) ? $fromDate : date('Y-m-d') }}" required>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label"><strong>To Date</strong></label>
                        <input type="date" class="form-control" name="to_date" id="to_date" 
                               value="{{ isset($toDate) ? $toDate : date('Y-m-d') }}" required>
                    </div>
                </div>
                
                <div class="d-flex justify-content-end">
                    <button type="submit" class="btn btn-primary me-2">Generate Report</button>
                    <button type="reset" class="btn btn-secondary" onclick="resetForm()">Refresh</button>
                </div>
            </form>
        </div>
    </div>
    
    @if(isset($invoiceBalances) && count($invoiceBalances) > 0)
        <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> 
                    Outstanding Payments Results ({{ isset($fromDate) ? \Carbon\Carbon::parse($fromDate)->format('d/m/Y') : '' }} - {{ isset($toDate) ? \Carbon\Carbon::parse($toDate)->format('d/m/Y') : '' }})
                </h6>
                <div>
                    <form action="{{ route('outstandingPaymentsReport.downloadPdf') }}" method="POST" class="d-inline">
                        @csrf
                        <input type="hidden" name="report_type" value="{{ $reportType }}">
                        <input type="hidden" name="from_date" value="{{ $fromDate }}">
                        <input type="hidden" name="to_date" value="{{ $toDate }}">
                        <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">
                @if($reportType === 'summary')
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover">
                            <thead class="table-light">
                                <tr>
                                    <th>Date</th>
                                    <th>Supplier Code</th>
                                    <th>Name</th>
                                    <th>Balance (USD)</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($invoiceBalances as $item)
                                    <tr>
                                        <td>{{ \Carbon\Carbon::parse($item['invoice']->document_date)->format('d/m/Y') }}</td>
                                        <td>{{ $item['invoice']->supplier->supplier_code }}</td>
                                        <td>{{ $item['invoice']->supplier->supplier_account }}</td>
                                        <td>{{ number_format($item['balance'], 2) }}</td>
                                    </tr>
                                @endforeach
                            </tbody>
                            <tfoot>
                                <tr class="table-active">
                                    <td colspan="3" class="text-end"><strong>Total Outstanding:</strong></td>
                                    <td><strong>USD{{ number_format($totalOutstanding, 2) }}</strong></td>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                @else
                    @foreach($supplierInvoices as $supplierData)
                        <div class="mb-4">
                            <h6 class="mb-2">Supplier: {{ $supplierData['supplier']->supplier_account }} | 
                                Code: {{ $supplierData['supplier']->supplier_code }}</h6>
                            
                            <div class="table-responsive">
                                <table class="table table-bordered table-hover">
                                    <thead class="table-light">
                                        <tr>
                                            <th>Date</th>
                                            <th>Invoice</th>
                                            <th>Amount (USD)</th>
                                            <th>Tax</th>
                                            <th>Discount</th>
                                            <th>Amount Paid (USD)</th>
                                            <th>Balance (USD)</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @php
                                            $supplierBalance = 0;
                                        @endphp
                                        @foreach($supplierData['invoices'] as $item)
                                            @php
                                                $supplierBalance += $item['balance'];
                                            @endphp
                                            <tr>
                                                <td>{{ \Carbon\Carbon::parse($item['invoice']->document_date)->format('d/m/Y') }}</td>
                                                <td>{{ $item['invoice']->document_number }}</td>
                                                <td>{{ number_format($item['invoice']->inclusive_total, 2) }}</td>
                                                <td>{{ number_format($item['invoice']->tax_amount, 2) }}</td>
                                                <td>{{ number_format($item['invoice']->discount_total, 2) }}</td>
                                                <td>{{ number_format($item['amount_paid'], 2) }}</td>
                                                <td>{{ number_format($item['balance'], 2) }}</td>
                                            </tr>
                                        @endforeach
                                    </tbody>
                                </table>
                            </div>
                            
                            <div class="text-end mb-3">
                                <strong>Closing balance for {{ strtoupper($supplierData['supplier']->supplier_account) }} : USD{{ number_format($supplierBalance, 2) }}</strong>
                            </div>
                            <hr>
                        </div>
                    @endforeach
                    
                    <div class="mt-3 p-2 bg-light rounded">
                        <div class="text-end">
                            <strong>Total Outstanding: USD{{ number_format($totalOutstanding, 2) }}</strong>
                        </div>
                    </div>
                @endif
            </div>
            <div class="card-footer">
                <div class="mt-4 text-center">
                    <p class="text-muted">Copyright © Melsoft (Private) Limited</p>
                </div>
            </div>
        </div>
    @elseif(isset($invoiceBalances) && count($invoiceBalances) == 0)
        <div class="card shadow-sm mt-4">
            <div class="card-body">
                <div class="alert alert-info mt-3">
                    No outstanding payments found in the selected date 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 Report Type",
            allowClear: true
        });
    });

    function resetForm() {
        // Reset the form
        $('#report_type').val(null).trigger('change');
        $('#from_date').val('{{ date('Y-m-d') }}');
        $('#to_date').val('{{ date('Y-m-d') }}');
    }
</script>
@endsection