<?php $__env->startSection('title', 'Supplier Invoice Summary Report'); ?>
<?php $__env->startSection('content'); ?>

<div class="container-fluid py-3">
    <div class="d-flex justify-content-end mb-3">
        <a href="<?php echo e(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 me-2"></i> Supplier Invoice Summary Report</h5>
        </div>
        
        <div class="card-body">
            <?php if(session('success')): ?>
                <div class="alert alert-success"><?php echo e(session('success')); ?></div>
            <?php endif; ?>
            
            <?php if($errors->any()): ?>
                <div class="alert alert-danger">
                    <ul class="mb-0">
                        <?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $error): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <li><?php echo e($error); ?></li>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </ul>
                </div>
            <?php endif; ?>
            
            <form action="<?php echo e(route('supplierInvoiceSummaryReport.generate')); ?>" method="POST" id="reportForm">
                <?php echo csrf_field(); ?>
                <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" <?php echo e((isset($reportType) && $reportType == 'summary') ? 'selected' : ''); ?>>Summary</option>
                            <option value="detailed" <?php echo e((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="<?php echo e(isset($fromDate) ? $fromDate : $currentDate); ?>" 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="<?php echo e(isset($toDate) ? $toDate : $currentDate); ?>" 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>
    
    <?php if(isset($invoices) && count($invoices) > 0): ?>
        <!-- PDF Download Button -->
        <div class="d-flex justify-content-end mt-4">
            <form action="<?php echo e(route('supplierInvoiceSummaryReport.downloadPdf')); ?>" method="POST" class="d-inline">
                <?php echo csrf_field(); ?>
                <input type="hidden" name="report_type" value="<?php echo e($reportType); ?>">
                <input type="hidden" name="from_date" value="<?php echo e($fromDate); ?>">
                <input type="hidden" name="to_date" value="<?php echo e($toDate); ?>">
                <button type="submit" class="btn btn-process-all btn-sm">
                    <i class="fas fa-download me-1"></i> DOWNLOAD PDF
                </button>
            </form>
        </div>
        
        <?php if($reportType === 'summary'): ?>
            <!-- Summary Report -->
            <div class="card shadow-sm mt-4">
                <div class="card-header bg-light py-3">
                    <h6 class="mb-0"><i class="fas fa-list me-2"></i> Supplier Invoice Summary Results</h6>
                </div>
                <div class="card-body">
                    <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>Amount (USD)</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $__currentLoopData = $invoices; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $invoice): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <tr>
                                        <td><?php echo e(\Carbon\Carbon::parse($invoice->document_date)->format('d/m/Y')); ?></td>
                                        <td><?php echo e($invoice->supplier->supplier_code ?? 'N/A'); ?></td>
                                        <td><?php echo e($invoice->supplier->supplier_name ?? 'N/A'); ?></td>
                                        <td><?php echo e(number_format($invoice->inclusive_total, 2)); ?></td>
                                    </tr>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </tbody>
                            <tfoot>
                                <tr class="table-active">
                                    <td colspan="3" class="text-end"><strong>Total Amount:</strong></td>
                                    <td><strong>USD <?php echo e(number_format($invoices->sum('inclusive_total'), 2)); ?></strong></td>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        <?php else: ?>
            <!-- Detailed Report -->
            <?php $__currentLoopData = $supplierInvoices; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $supplierData): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <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> <?php echo e($supplierData['supplier']->supplier_name ?? 'N/A'); ?> (<?php echo e($supplierData['supplier']->supplier_code ?? 'N/A'); ?>)</h6>
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-bordered table-hover">
                                <thead class="table-light">
                                    <tr>
                                        <th>Date</th>
                                        <th>Invoice Number</th>
                                        <th>Amount (USD)</th>
                                        <th>Tax (USD)</th>
                                        <th>Discount (USD)</th>
                                        <th>Net Amount (USD)</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                        $supplierTotal = 0;
                                    ?>
                                    <?php $__currentLoopData = $supplierData['invoices']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $invoice): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <?php
                                            $supplierTotal += $invoice->inclusive_total;
                                        ?>
                                        <tr>
                                            <td><?php echo e(\Carbon\Carbon::parse($invoice->document_date)->format('d/m/Y')); ?></td>
                                            <td><?php echo e($invoice->document_number); ?></td>
                                            <td><?php echo e(number_format($invoice->inclusive_total, 2)); ?></td>
                                            <td><?php echo e(number_format($invoice->tax_amount, 2)); ?></td>
                                            <td><?php echo e(number_format($invoice->discount_total, 2)); ?></td>
                                            <td><?php echo e(number_format($invoice->inclusive_total - $invoice->tax_amount, 2)); ?></td>
                                        </tr>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </tbody>
                                <tfoot>
                                    <tr class="table-active">
                                        <td colspan="5" class="text-end"><strong>Total for <?php echo e($supplierData['supplier']->supplier_name ?? 'N/A'); ?>:</strong></td>
                                        <td><strong>USD <?php echo e(number_format($supplierTotal, 2)); ?></strong></td>
                                    </tr>
                                </tfoot>
                            </table>
                        </div>
                    </div>
                </div>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            
            <!-- Grand Total -->
            <div class="card shadow-sm mt-4">
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-bordered">
                            <tfoot>
                                <tr class="table-active">
                                    <td colspan="5" class="text-end"><strong>Grand Total:</strong></td>
                                    <td><strong>USD <?php echo e(number_format($invoices->sum('inclusive_total'), 2)); ?></strong></td>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        <?php endif; ?>
        
        <div class="mt-4 text-center">
            <p class="text-muted">Copyright © Melsoft (Private) Limited</p>
        </div>
        
    <?php elseif(isset($invoices) && count($invoices) == 0): ?>
        <div class="card shadow-sm mt-4">
            <div class="card-body">
                <div class="alert alert-info mt-3">
                    No supplier invoices found in the selected date range.
                </div>
            </div>
        </div>
    <?php endif; ?>
</div>

<?php $__env->stopSection(); ?>

<?php $__env->startSection('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>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('scripts'); ?>
<script>
    // Initialize Select2 for dropdowns
    $(document).ready(function() {
        $('.select2').select2({
            placeholder: "Select Report Type",
            allowClear: true
        });
        
        // Set default dates to today
        var today = new Date().toISOString().split('T')[0];
        $('#from_date').val(today);
        $('#to_date').val(today);
    });

    function resetForm() {
        // Reset the form
        $('#report_type').val(null).trigger('change');
        
        // Set dates back to today
        var today = new Date().toISOString().split('T')[0];
        $('#from_date').val(today);
        $('#to_date').val(today);
    }
</script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.master', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/melsoftzimco/public_html/demo/resources/views/reports/supplier-invoice-summary.blade.php ENDPATH**/ ?>