

<?php $__env->startSection('title', 'Planned Manufacturing Runs - Warehouse Confirmation'); ?>

<?php $__env->startPush('plugin-styles'); ?>
<link href="<?php echo e(asset('theme/vendors/datatables.net-bs5/dataTables.bootstrap5.css')); ?>" rel="stylesheet">
<?php $__env->stopPush(); ?>

<?php $__env->startPush('plugin-scripts'); ?>
<script src="<?php echo e(asset('theme/vendors/datatables.net/jquery.dataTables.js')); ?>"></script>
<script src="<?php echo e(asset('theme/vendors/datatables.net-bs5/dataTables.bootstrap5.js')); ?>"></script>
<?php $__env->stopPush(); ?>

<?php $__env->startSection('content'); ?>
<?php if(session('success')): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
    <i class="fas fa-check-circle me-2"></i><?php echo e(session('success')); ?>

    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></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 me-2"></i><?php echo e(session('error')); ?>

    <?php if(session('insufficientItems')): ?>
        <ul class="mt-2 mb-0">
            <?php $__currentLoopData = session('insufficientItems'); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <li><?php echo e($item); ?></li>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        </ul>
    <?php endif; ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>

<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">Planned Manufacturing Runs - Warehouse Confirmation</h3>
                    <div class="card-tools">
                        <a href="<?php echo e(route('warehouses.index')); ?>" class="btn btn-secondary btn-sm">
                            <i class="fas fa-arrow-left"></i> Back to Warehouses
                        </a>
                    </div>
                </div>
                <div class="card-body">
                    <div id="ajax-alert-container"></div>
                    <div class="alert alert-info">
                        <i class="fas fa-info-circle me-2"></i>
                        <strong>Instructions:</strong> These are manufacturing runs waiting for goods from warehouse.
                        Once you have confirmed the goods are ready, click "Start Manufacturing" to change the status to "In Progress".
                    </div>

                    <div class="table-responsive">
                        <table id="planned-manufacturing-table" class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th>M-Number</th>
                                    <th>BOM Name</th>
                                    <th>Finished Goods</th>
                                    <th>Machine</th>
                                    <th>Supervisor</th>
                                    <th>Planned Qty</th>
                                    <th>Warehouse</th>
                                    <th>Status</th>
                                    <th>Created Date</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Start Manufacturing Confirmation Modal -->
<div class="modal fade" id="startManufacturingModal" tabindex="-1" aria-labelledby="startManufacturingModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="startManufacturingModalLabel">Confirm Start Manufacturing</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <p>Are you sure you want to start manufacturing for <strong id="modal-m-number"></strong>?</p>
                <p class="text-muted">This will change the status from "Planned" to "Received & Manufacturing" indicating that goods have been received from warehouse and manufacturing has begun.</p>
            </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="confirmStartBtn">Start Manufacturing</button>
            </div>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

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

    $('#planned-manufacturing-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: {
            url: "<?php echo e(route('warehouses.planned-manufacturing.data')); ?>",
            type: 'GET'
        },
        columns: [
            { data: 'm_number', name: 'm_number' },
            { data: 'bom_name', name: 'bom_name', searchable: false },
            { data: 'finished_goods', name: 'finished_goods', searchable: false },
            { data: 'machine_name', name: 'machine_name', searchable: false },
            { data: 'supervisor_name', name: 'supervisor_name', searchable: false },
            { data: 'planned_output_qty', name: 'planned_output_qty' },
            { data: 'warehouse_name', name: 'warehouse_name', searchable: false },
            { data: 'status_badge', name: 'status', searchable: false },
            {
                data: 'created_at',
                name: 'created_at',
                render: function (data, type, row) {
                    if (!data) return '';
                    const date = new Date(data);
                    const day = String(date.getDate()).padStart(2, '0');
                    const month = String(date.getMonth() + 1).padStart(2, '0');
                    const year = date.getFullYear();
                    let hours = date.getHours();
                    const minutes = String(date.getMinutes()).padStart(2, '0');
                    const ampm = hours >= 12 ? 'PM' : 'AM';
                    hours = hours % 12;
                    hours = hours ? hours : 12;
                    const formattedTime = `${String(hours).padStart(2, '0')}:${minutes} ${ampm}`;
                    return `${day}-${month}-${year} ${formattedTime}`;
                }
            },
            { data: 'actions', name: 'actions', orderable: false, searchable: false }
        ],
        order: [[0, 'desc']],
        pageLength: 25,
        responsive: true,
        language: {
            processing: "Loading planned manufacturing runs..."
        }
    });

    // Handle start manufacturing button click
    $(document).on('click', '.start-manufacturing-btn', function() {
        selectedRunId = $(this).data('id');
        const mNumber = $(this).data('m-number');

        $('#modal-m-number').text(mNumber);
        $('#startManufacturingModal').modal('show');
    });

    // Handle confirm start manufacturing
    $('#confirmStartBtn').on('click', function() {
        if (!selectedRunId) return;

        // Disable button and show loading
        $(this).prop('disabled', true).html('<i class="fas fa-spinner fa-spin"></i> Processing...');
        $('#ajax-alert-container').empty();

        $.ajax({
            url: "<?php echo e(route('warehouses.planned-manufacturing.start', ':id')); ?>".replace(':id', selectedRunId),
            type: 'POST',
            data: {
                _token: "<?php echo e(csrf_token()); ?>"
            },
            success: function(response) {
                $('#startManufacturingModal').modal('hide');
                
                if (response.success) {
                    showAlert('success', response.message);
                    $('#planned-manufacturing-table').DataTable().ajax.reload(null, false);
                } else {
                    showAlert('danger', response.message, response.insufficientItems);
                }
            },
            error: function(xhr) {
                $('#startManufacturingModal').modal('hide');
                let message = 'An unexpected error occurred.';
                if (xhr.responseJSON && xhr.responseJSON.message) {
                    message = xhr.responseJSON.message;
                }
                showAlert('danger', message);
            },
            complete: function() {
                // Re-enable button and restore text
                $('#confirmStartBtn').prop('disabled', false).html('Start Manufacturing');
            }
        });
    });

    function showAlert(type, message, items = null) {
        let itemsHtml = '';
        if (items && items.length > 0) {
            itemsHtml = '<ul class="mt-2 mb-0">';
            items.forEach(function(item) {
                itemsHtml += `<li>${item}</li>`;
            });
            itemsHtml += '</ul>';
        }

        const alertHtml = `
            <div class="alert alert-${type} alert-dismissible fade show" role="alert">
                <i class="fas fa-${type === 'success' ? 'check-circle' : 'exclamation-triangle'} me-2"></i>
                ${message}
                ${itemsHtml}
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        `;
        $('#ajax-alert-container').html(alertHtml);
        
        // Auto-scroll to alert
        $('html, body').animate({
            scrollTop: $("#ajax-alert-container").offset().top - 100
        }, 500);
    }
});
</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\warehouses\planned-manufacturing\index.blade.php ENDPATH**/ ?>