

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

<?php $__env->startSection('content'); ?>
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0"><i class="fas fa-file-invoice-dollar"></i> Factory Invoices</h5>
                    <a href="<?php echo e(route('factory-pos.create')); ?>" class="btn btn-primary">
                        <i class="fas fa-plus"></i> New Invoice
                    </a>
                </div>
                <div class="card-body">
                    <!-- 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="warehouse_pending">Warehouse Pending</option>
                                <option value="picked">Picked</option>
                                <option value="delivered">Delivered</option>
                                <option value="completed">Completed</option>
                            </select>
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">Customer</label>
                            <select class="form-select" id="customerFilter">
                                <option value="">All Customers</option>
                                <?php $__currentLoopData = $customers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $customer): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($customer->id); ?>"><?php echo e($customer->name); ?></option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                        </div>
                        <div class="col-md-2">
                            <label class="form-label">From Date</label>
                            <input type="date" class="form-control" id="dateFromFilter">
                        </div>
                        <div class="col-md-2">
                            <label class="form-label">To Date</label>
                            <input type="date" class="form-control" id="dateToFilter">
                        </div>
                        <div class="col-md-2 d-flex align-items-end">
                            <button type="button" class="btn btn-outline-secondary w-100" onclick="clearFilters()">
                                <i class="fas fa-times"></i> Clear
                            </button>
                        </div>
                    </div>

                    <!-- DataTable -->
                    <div class="table-responsive">
                        <table class="table table-striped table-hover" id="invoicesTable">
                            <thead class="table-light">
                                <tr>
                                    <th>Invoice #</th>
                                    <th>Type</th>
                                    <th>Customer</th>
                                    <th>Supervisor</th>
                                    <th>Tax</th>
                                    <th>Gross Total</th>
                                    <th>Delivered Amount</th>
                                    <th>Status</th>
                                    <th>Date</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                            <tbody></tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<script>
$(document).ready(function() {
    const urlParams = new URLSearchParams(window.location.search);
    const dateFilter = urlParams.get('date');

    if (dateFilter === 'today') {
    const today = new Date();
    const yyyy = today.getFullYear();
    const mm   = String(today.getMonth() + 1).padStart(2, '0');
    const dd   = String(today.getDate()).padStart(2, '0');
    $('#dateFromFilter').val(`${yyyy}-${mm}-01`);      
    $('#dateFromFilter').val(`${yyyy}-${mm}-${dd}`);
    $('#dateToFilter').val(`${yyyy}-${mm}-${dd}`);

    } else if (dateFilter === 'month') {
        const today = new Date();
        const yyyy  = today.getFullYear();
        const mm    = String(today.getMonth() + 1).padStart(2, '0');
        const lastDay = new Date(today.getFullYear(), today.getMonth() + 1, 0).getDate();

        $('#dateFromFilter').val(`${yyyy}-${mm}-01`);
        $('#dateToFilter').val(`${yyyy}-${mm}-${String(lastDay).padStart(2, '0')}`);
    }

    const table = $('#invoicesTable').DataTable({
        processing: true,
        serverSide: true,
        deferRender: true,
        ajax: {
            url: '<?php echo e(route("factory-pos.data")); ?>',
            type: 'GET',
            data: function(d) {
                d.status      = $('#statusFilter').val();
                d.customer_id = $('#customerFilter').val();
                d.date_from   = $('#dateFromFilter').val();
                d.date_to     = $('#dateToFilter').val();
            }
        },
        columns: [
            { data: 'invoice_number',        orderable: false, defaultContent: '-' },
            { data: 'invoice_type',          orderable: false, defaultContent: '-' },
            { data: 'customer_name',         orderable: false, defaultContent: '-' },
            { data: 'supervisor',            orderable: false, defaultContent: '-' },
            {
                data: 'tax_amount', orderable: false, defaultContent: '-',
                render: function(data) { return data ? 'Rs ' + parseFloat(data).toFixed(2) : '-'; }
            },
            {
                data: 'gross_total', orderable: false, defaultContent: '-',
                render: function(data) { return data ? 'Rs ' + parseFloat(data).toFixed(2) : '-'; }
            },
            {
                data: 'total_delivered_amount', orderable: false, defaultContent: '-',
                render: function(data) { return data ? 'Rs ' + parseFloat(data).toFixed(2) : '-'; }
            },
            { data: 'status_badge', orderable: false, defaultContent: '<span class="badge bg-secondary">No Data</span>' },
            {
                data: 'created_at', orderable: false, defaultContent: '-',
                render: function(data) { return data ? new Date(data).toLocaleDateString() : '-'; }
            },
            { data: 'actions', orderable: false, defaultContent: '-' }
        ],
        language: {
            emptyTable: "No factory invoices found.",
            processing: "Processing..."
        },
        pageLength: 25,
        lengthMenu: [[10, 25, 50, -1], [10, 25, 50, "All"]],
        responsive: true,
        order: [[8, 'desc']],
    });

    $('#statusFilter, #customerFilter, #dateFromFilter, #dateToFilter').on('change', function() {
        table.ajax.reload();
    });
});

function clearFilters() {
    $('#statusFilter').val('all');
    $('#customerFilter').val('');
    $('#dateFromFilter').val('');
    $('#dateToFilter').val('');
    $('#invoicesTable').DataTable().ajax.reload();
}
</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/factory-pos/index.blade.php ENDPATH**/ ?>