

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

<?php $__env->startSection('content'); ?>
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header bg-primary text-white">
                    <h5 class="mb-0"><i class="fas fa-truck"></i> Delivery Receipts</h5>
                </div>
                <div class="card-body">
                    <!-- Flash Messages -->
                    <?php if(session('success')): ?>
                        <div class="alert alert-success alert-dismissible fade show" role="alert">
                            <i class="fas fa-check-circle"></i> <?php echo e(session('success')); ?>

                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    <?php endif; ?>

                    <?php if(session('error')): ?>
                        <div class="alert alert-danger alert-dismissible fade show" role="alert">
                            <i class="fas fa-exclamation-triangle"></i> <?php echo e(session('error')); ?>

                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    <?php endif; ?>

                    <!-- Filters -->
                    <div class="row mb-3">
                        <div class="col-md-3">
                            <label class="form-label">Status</label>
                            <select class="form-select" id="statusFilter">
                                <option value="all">All Status</option>
                                <option value="draft">Draft</option>
                                <option value="submitted">Submitted</option>
                                <option value="processed">Processed</option>
                            </select>
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">Date From</label>
                            <input type="date" class="form-control" id="dateFromFilter">
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">Date To</label>
                            <input type="date" class="form-control" id="dateToFilter">
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">&nbsp;</label>
                            <div>
                                <button class="btn btn-secondary" onclick="clearFilters()"><i class="fas fa-times"></i> Clear</button>
                                <button class="btn btn-primary" onclick="applyFilters()"><i class="fas fa-search"></i> Filter</button>
                            </div>
                        </div>
                    </div>

                    <!-- Actions -->
                    <div class="row mb-3">
                        <div class="col-12">
                            <a href="<?php echo e(route('delivery-receipts.create')); ?>" class="btn btn-success">
                                <i class="fas fa-plus"></i> Create Delivery Receipt
                            </a>
                        </div>
                    </div>

                    <!-- DataTable -->
                    <div class="table-responsive">
                        <table class="table table-striped table-hover" id="deliveryReceiptsTable">
                            <thead class="table-light">
                                <tr>
                                    <th>Gate Pass</th>
                                    <th>Customer</th>
                                    <th>Delivery Date</th>
                                    <th>Delivery Man</th>
                                    <th>Status</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                        </table>
                    </div>
                </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>
let table;
let currentReceiptId = null;

$(document).ready(function() {
    initializeDataTable();
});

function initializeDataTable() {
    table = $('#deliveryReceiptsTable').DataTable({
        processing: true,
        serverSide: true,
        ajax: {
            url: '<?php echo e(route("delivery-receipts.data")); ?>',
            data: function(d) {
                d.status = $('#statusFilter').val();
                d.date_from = $('#dateFromFilter').val();
                d.date_to = $('#dateToFilter').val();
            }
        },
        columns: [
            { data: 'gate_pass_number', name: 'gate_pass_number' },
            { data: 'customer_name', name: 'customer_name' },
            { data: 'delivery_date', name: 'delivery_date' },
            { data: 'delivery_man', name: 'delivery_man' },
            { data: 'status_badge', name: 'status_badge', orderable: false },
            { data: 'actions', name: 'actions', orderable: false, searchable: false }
        ],
        order: [[2, 'desc']],
        pageLength: 25
    });
}

function applyFilters() {
    table.ajax.reload();
}

function clearFilters() {
    $('#statusFilter').val('all');
    $('#dateFromFilter').val('');
    $('#dateToFilter').val('');
    table.ajax.reload();
}

function processReceipt(receiptId) {
    currentReceiptId = receiptId;
    $('#processModal').modal('show');
}

$('#confirmProcessBtn').on('click', function() {
    if (!currentReceiptId) return;

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

    $.ajax({
        url: '<?php echo e(route("delivery-receipts.process", ":id")); ?>'.replace(':id', currentReceiptId),
        method: 'POST',
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        success: function(response) {
            $('#processModal').modal('hide');
            table.ajax.reload();
            toastr.success(response.message);
        },
        error: function(xhr) {
            toastr.error(xhr.responseJSON?.error || 'An error occurred');
        },
        complete: function() {
            $('#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/index.blade.php ENDPATH**/ ?>