

<?php $__env->startSection('title', 'Delivery Receipt Details'); ?>

<?php $__env->startSection('content'); ?>
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header bg-info text-white">
                    <div class="d-flex justify-content-between align-items-center">
                        <h5 class="mb-0"><i class="fas fa-eye"></i> Delivery Receipt Details</h5>
                        <div>
                            <?php if(!$receipt->isProcessed()): ?>
                                <a href="<?php echo e(route('delivery-receipts.edit', $receipt)); ?>" class="btn btn-warning btn-sm">
                                    <i class="fas fa-edit"></i> Edit
                                </a>
                                <?php if($receipt->isSubmitted()): ?>
                                    <button onclick="processReceipt(<?php echo e($receipt->id); ?>)" class="btn btn-success btn-sm">
                                        <i class="fas fa-check"></i> Process
                                    </button>
                                <?php endif; ?>
                            <?php endif; ?>
                            <a href="<?php echo e(route('delivery-receipts.index')); ?>" class="btn btn-secondary btn-sm">
                                <i class="fas fa-arrow-left"></i> Back
                            </a>
                        </div>
                    </div>
                </div>
                <div class="card-body">
                    <!-- Receipt Information -->
                    <div class="row mb-4">
                        <div class="col-md-6">
                            <div class="card border-primary">
                                <div class="card-header bg-light">
                                    <h6 class="mb-0">Receipt Information</h6>
                                </div>
                                <div class="card-body">
                                    <div class="row">
                                        <div class="col-6">
                                            <p class="mb-1"><strong>Delivery Receipt ID:</strong> #<?php echo e($receipt->id); ?></p>
                                            <p class="mb-1"><strong>Status:</strong>
                                                <?php if($receipt->isDraft()): ?>
                                                    <span class="badge bg-secondary">Draft</span>
                                                <?php elseif($receipt->isSubmitted()): ?>
                                                    <span class="badge bg-warning">Submitted</span>
                                                <?php elseif($receipt->isProcessed()): ?>
                                                    <span class="badge bg-success">Processed</span>
                                                <?php endif; ?>
                                            </p>
                                        </div>
                                        <div class="col-6">
                                            <p class="mb-1"><strong>Delivery Date:</strong> <?php echo e($receipt->delivery_date->format('d/m/Y')); ?></p>
                                            
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="card border-success">
                                <div class="card-header bg-light">
                                    <h6 class="mb-0">Gate Pass Information</h6>
                                </div>
                                <div class="card-body">
                                    <p class="mb-1"><strong>Gate Pass:</strong> <?php echo e($receipt->gatePass->gate_pass_number ?? 'N/A'); ?></p>

                                    <!-- Show all customers involved -->
                                    <?php
                                        $customers = $receipt->items->pluck('customer')->unique()->filter();
                                        $customerNames = $customers->pluck('name')->join(', ');
                                        $totalAmount = $receipt->items->sum('delivered_total');
                                        $totalItems = $receipt->items->count();
                                    ?>

                                    <p class="mb-1"><strong>Customers:</strong> <?php echo e($customerNames ?: 'N/A'); ?></p>
                                    <p class="mb-1"><strong>Total Items:</strong> <?php echo e($totalItems); ?></p>
                                    <p class="mb-1"><strong>Total Amount:</strong> $<?php echo e(number_format($totalAmount, 2)); ?></p>

                                    <p class="mb-1"><strong>Vehicle:</strong> <?php echo e($receipt->gatePass->vehicle_no ?? 'N/A'); ?></p>
                                    <p class="mb-1"><strong>Driver:</strong> <?php echo e($receipt->gatePass->driver_name ?? 'N/A'); ?></p>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Delivery Items Grouped by Customer -->
                    <div class="card border-info mb-4">
                        <div class="card-header bg-light">
                            <h6 class="mb-0">Delivered Items by Customer</h6>
                        </div>
                        <div class="card-body">
                            <?php
                                $customerItems = $receipt->items->groupBy('customer_account_id');
                            ?>

                            <?php $__currentLoopData = $customerItems; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $customerId => $items): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <?php
                                    $customer = $items->first()->customer;
                                    $customerTotal = $items->sum('delivered_total');
                                ?>

                                <div class="customer-section mb-4">
                                    <h5 class="text-primary mb-3">
                                        <i class="fas fa-user"></i> <?php echo e($customer->name ?? 'Unknown Customer'); ?>

                                        <small class="text-muted">(Total: $<?php echo e(number_format($customerTotal, 2)); ?>)</small>
                                    </h5>

                                    <div class="table-responsive">
                                        <table class="table table-sm table-striped">
                                            <thead class="table-light">
                                                <tr>
                                                    <th>Item</th>
                                                    <th>Factory Invoice</th>
                                                    <th>Delivered Qty</th>
                                                    <th>Unit Price</th>
                                                    <th>Total</th>
                                                    <th>Notes</th>
                                                </tr>
                                            </thead>
                                            <tbody class="border-primary border-top-2">
                                                <?php $__currentLoopData = $items; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <tr>
                                                    <td>
                                                        <div class="fw-bold"><?php echo e($item->gatePassItem->item->name ?? 'N/A'); ?></div>
                                                    </td>
                                                    <td><?php echo e($item->factoryInvoice->invoice_number ?? 'N/A'); ?></td>
                                                    <td class="text-center"><?php echo e(number_format($item->delivered_quantity, 2)); ?></td>
                                                    <td class="text-end">$<?php echo e(number_format($item->delivered_unit_price, 2)); ?></td>
                                                    <td class="text-end">$<?php echo e(number_format($item->delivered_total, 2)); ?></td>
                                                    <td><?php echo e($item->notes ?? '-'); ?></td>
                                                </tr>
                                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                            </tbody>
                                            <tfoot class="table-light ">
                                                <tr>
                                                    <th colspan="4" class="text-end">Customer Total:</th>
                                                    <th class="text-end">$<?php echo e(number_format($customerTotal, 2)); ?></th>
                                                    <th></th>
                                                </tr>
                                            </tfoot>
                                        </table>
                                    </div>
                                </div>

                                <?php if(!$loop->last): ?>
                                    <hr class="my-4">
                                <?php endif; ?>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                            <!-- Overall Total -->
                            <div class="row mt-4">
                                <div class="col-12">
                                    <div class="alert alert-success text-center">
                                        <h5 class="mb-0">
                                            <strong>Grand Total: $<?php echo e(number_format($receipt->items->sum('delivered_total'), 2)); ?></strong>
                                        </h5>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Notes -->
                    <?php if($receipt->notes): ?>
                    <div class="card border-warning">
                        <div class="card-header bg-light">
                            <h6 class="mb-0">Notes</h6>
                        </div>
                        <div class="card-body">
                            <p class="mb-0"><?php echo e($receipt->notes); ?></p>
                        </div>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Process Receipt Modal -->
<div class="modal fade" id="processModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Process Delivery Receipt</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <p>Are you sure you want to process this delivery receipt? This will create account transactions for the customers.</p>
                <div id="processContent"></div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-success" id="confirmProcessBtn">Process</button>
            </div>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<script>
function processReceipt(receiptId) {
    $('#processModal').modal('show');
}

$('#confirmProcessBtn').on('click', function() {
    $(this).prop('disabled', true).html('<i class="fas fa-spinner fa-spin"></i> Processing...');

    $.ajax({
        url: '<?php echo e(route("delivery-receipts.process", $receipt)); ?>',
        method: 'POST',
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        success: function(response) {
            $('#processModal').modal('hide');
            location.reload();
        },
        error: function(xhr) {
            alert(xhr.responseJSON?.error || 'An error occurred');
            $('#confirmProcessBtn').prop('disabled', false).html('Process');
        }
    });
});
</script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\laragon\www\habibcables-ERP\resources\views\delivery-receipts\show.blade.php ENDPATH**/ ?>