

<?php $__env->startSection('title', 'Dispatches'); ?>

<?php $__env->startSection('content'); ?>

    <div class="card">

        <div class="card-header">

            <div class="d-flex justify-content-between">

                <h4 class="mb-0">
                    Dispatch List
                </h4>
                <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('dispatch.create')): ?>
                    <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addDispatchModal">

                        <i class="bi bi-plus-circle"></i>
                        New Dispatch

                    </button>
                <?php endif; ?>
            </div>

        </div>

        <div class="card-body">

            <div class="table-responsive">

                <table class="table table-striped" id="dispatchTable">

                    <thead>

                        <tr>
                            <th>Dispatch No</th>
                            <th>Devices</th>
                            <th>Sales User</th>
                            <th>Date</th>
                            <th>Status</th>
                            <th>Created By</th>
                            <th>Action</th>
                        </tr>

                    </thead>

                    <tbody>

                        <?php $__currentLoopData = $dispatches; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $dispatch): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <tr>

                                <td>
                                    <?php echo e($dispatch->dispatch_no); ?>

                                </td>
                                <td>
                                    <?php echo e($dispatch->items_count); ?>

                                </td>
                                <td>
                                    <?php echo e($dispatch->salesUser?->name); ?>

                                </td>
                                <td>
                                    <?php echo e($dispatch->created_at->format('d M Y H:i:s')); ?>

                                </td>

                                <td>

                                    <?php if($dispatch->status == 'pending'): ?>
                                        <span class="badge bg-warning">
                                            PENDING
                                        </span>
                                    <?php elseif($dispatch->status == 'received'): ?>
                                        <span class="badge bg-success">
                                            RECEIVED
                                        </span>
                                    <?php endif; ?>

                                </td>

                                <td>
                                    <?php echo e($dispatch->creator?->name); ?>

                                </td>

                                <td>
                                    <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('dispatch.view')): ?>
                                        <button title="View Dispatch" class="btn btn-info btn-sm view-dispatch"
                                            data-id="<?php echo e($dispatch->id); ?>" data-bs-toggle="modal"
                                            data-bs-target="#viewDispatchModal">

                                            <i class="bi bi-eye"></i>
                                        </button>
                                    <?php endif; ?>

                                    <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('dispatch.receive')): ?>
                                        <?php if($dispatch->status == 'pending'): ?>
                                            <form action="<?php echo e(route('dispatches.receive', $dispatch->id)); ?>" method="POST"
                                                style="display:inline">

                                                <?php echo csrf_field(); ?>

                                                <button class="btn btn-success btn-sm" title="Receive Dispatch"
                                                    onclick="return confirm('Are you sure you want to receive this dispatch?')">

                                                    <i class="bi bi-check-circle"></i>
                                                    Receive

                                                </button>

                                            </form>
                                        <?php endif; ?>
                                    <?php endif; ?>

                                </td>

                            </tr>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                    </tbody>

                </table>

            </div>

        </div>

    </div>


    
    <div class="modal fade" id="viewDispatchModal">

        <div class="modal-dialog modal-lg">

            <div class="modal-content">

                <div class="modal-header">

                    <h5>
                        Dispatch Devices
                    </h5>

                    <button type="button" class="btn-close" data-bs-dismiss="modal">
                    </button>

                </div>

                <div class="modal-body">


                    <div class="row mb-3">

                        <div class="col-md-6">
                            <strong>Dispatch No:</strong>
                            <span id="dispatchNo"></span>
                        </div>

                        <div class="col-md-6">
                            <strong>Sales User:</strong>
                            <span id="dispatchSalesUser"></span>
                        </div>

                        <div class="col-md-6">
                            <strong>Dispatch Date:</strong>
                            <span id="dispatchDate"></span>
                        </div>

                        <div class="col-md-6">
                            <strong>Received Date:</strong>
                            <span id="receivedDate"></span>
                        </div>

                    </div>


                    <table class="table table-bordered">

                        <thead>

                            <tr>
                                <th>S/N</th>
                                <th>Serial Number</th>
                                <th>IMEI</th>
                                <th>Status</th>
                            </tr>

                        </thead>

                        <tbody id="dispatchDevicesBody">

                        </tbody>

                    </table>

                </div>

            </div>

        </div>

    </div>




    <?php echo $__env->make('dispatch.partials.add-dispatch-modal', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>

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

<?php $__env->startPush('scripts'); ?>
    <script>
        $(document).ready(function() {

            $('#dispatchTable').DataTable({
                pageLength: 25
            });

        });

        $(document).on(
            'click',
            '.view-dispatch',
            function() {

                let id = $(this).data('id');

                $.get(
                    '/dispatches/' + id + '/devices',
                    function(response) {

                        $('#dispatchNo').text(
                            response.dispatch.dispatch_no
                        );

                        $('#dispatchSalesUser').text(
                            response.dispatch.sales_user
                        );

                        $('#dispatchDate').text(
                            response.dispatch.dispatch_date
                        );

                        $('#receivedDate').text(
                            response.dispatch.received_date
                        );

                        let rows = '';

                        response.devices.forEach(function(item, index) {

                            rows += `
                <tr>
                    <td>${index + 1}</td>
                    <td>${item.device.serial_number}</td>
                    <td>${item.device.imei ?? ''}</td>
                    <td>${item.device.status}</td>
                </tr>
                `;
                        });

                        $('#dispatchDevicesBody').html(rows);

                    }
                );

            }
        );
    </script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.master', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH D:\Projects\lms-ects\resources\views/dispatch/index.blade.php ENDPATH**/ ?>