@extends('layouts.app')

@section('title', 'Warehouse - Pending Factory Invoices')

@section('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 bg-primary text-white">
                    <h5 class="mb-0"><i class="fas fa-warehouse"></i> Warehouse - Pending Factory Invoices</h5>
                    <a href="{{ route('factory-pos.index') }}" class="btn btn-light btn-sm">
                        <i class="fas fa-arrow-left"></i> Back to Invoices
                    </a>
                </div>
                <div class="card-body">
                    <div class="alert alert-info">
                        <i class="fas fa-info-circle"></i>
                        <strong>Instructions:</strong> Review the factory invoices below and confirm picking for each item.
                        You can adjust quantities if items are out of stock or if there are other warehouse constraints.
                    </div>

                    <!-- DataTable -->
                    <div class="table-responsive">
                        <table class="table table-striped table-hover" id="pendingInvoicesTable">
                            <thead class="table-light">
                                <tr>
                                    <th>Invoice #</th>
                                    <th>Customer</th>
                                    <th>Supervisor</th>
                                    <th>Items</th>
                                    <th>Total Qty</th>
                                    <th>Ordered Amount</th>
                                    <th>Date</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                            <tbody></tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
$(document).ready(function() {
    // Initialize DataTable
    $('#pendingInvoicesTable').DataTable({
        processing: true,
        serverSide: true,
        ajax: {
            url: '{{ route("warehouse.factory-invoices.data") }}'
        },
        columns: [
            { data: 'invoice_number', orderable: false },
            { data: 'customer_name', orderable: false },
            { data: 'supervisor', orderable: false },
            { data: 'item_count', orderable: false },
            { data: 'total_quantity', orderable: false },
            {
                data: 'total_ordered_amount',
                orderable: false,
                render: function(data) {
                    return 'Rs ' + parseFloat(data).toFixed(2);
                }
            },
            {
                data: 'created_at',
                render: function(data) {
                    return new Date(data).toLocaleDateString();
                }
            },
            { data: 'actions', orderable: false }
        ],
        order: [[6, 'desc']], // Sort by date descending
        pageLength: 25,
        responsive: true,
        language: {
            emptyTable: "No pending factory invoices to process"
        }
    });
});
</script>
@endpush
