@extends('layouts.app')
@section('title', 'BOM Pick Lists')

@push('plugin-styles')
    <style>
        .select2-container--bootstrap-5{
            border: 1px solid #ddd !important;
            border-radius: 8px !important;
            width: 100% !important;
        }
        .select2-selection__rendered{
            padding-top: 1.8% !important;
        }
        .select2-container .select2-selection--single{
            height: 45px !important;
        }
    </style>
@endpush

@section('content')
<div class="page-content">
    <div class="row">
        <div class="col-md-12 grid-margin stretch-card">
            <div class="card">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center mb-4">
                        <h6 class="card-title">BOM Pick Lists</h6>
                        <a href="{{ route('warehouses.bom-pick-list.create') }}" class="btn btn-primary">
                            <i class="fas fa-plus me-2"></i>Create Pick List
                        </a>
                    </div>

                    <div class="table-responsive">
                        <table class="table table-striped" id="bom-pick-list-table">
                            <thead>
                                <tr>
                                    <th>M-Number</th>
                                    <th>Finished Goods</th>
                                    <th>Status</th>
                                    <th>Assigned Employees</th>
                                    <th>Created By</th>
                                    <th>Created At</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@push('plugin-scripts')
<script src="{{ asset('theme/vendors/select2/select2.min.js') }}"></script>
<script src="{{ asset('theme/vendors/datatables/jquery.dataTables.min.js') }}"></script>
<script src="{{ asset('theme/vendors/datatables/dataTables.bootstrap5.min.js') }}"></script>
@endpush

@push('custom-scripts')
<script>
$(document).ready(function() {
    $('#bom-pick-list-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: {
            url: "{{ route('warehouses.bom-pick-list.data') }}",
            type: 'GET'
        },
        columns: [
            { data: 'm_number', name: 'm_number' },
            { data: 'finished_goods', name: 'finished_goods' },
            { data: 'status_badge', name: 'status', orderable: false },
            { data: 'assigned_employees', name: 'assigned_employees' },
            { data: 'created_by', name: 'created_by' },
            { data: 'created_at', name: 'created_at' },
            { data: 'actions', name: 'actions', orderable: false, searchable: false }
        ],
        order: [[5, 'desc']],
        pageLength: 25,
        responsive: true
    });
});

function updateStatus(pickListId, status) {
    if (confirm('Are you sure you want to update the status?')) {
        $.ajax({
            url: `/warehouses/bom-pick-list/${pickListId}/status`,
            type: 'PUT',
            data: {
                status: status,
                _token: '{{ csrf_token() }}'
            },
            success: function(response) {
                if (response.success) {
                    $('#bom-pick-list-table').DataTable().ajax.reload();
                    alert('Status updated successfully');
                } else {
                    alert('Error updating status');
                }
            },
            error: function() {
                alert('Error updating status');
            }
        });
    }
}
</script>
@endpush
