@extends('layouts.app')

@section('title', 'Configure BOM')

@section('content')
@section('content')
    <div class="mb-4">
        <h4 class="mb-0 fw-bold">Define Product Consumption (BOM)</h4>
        <p class="text-muted small mb-0">Map the raw materials and quantities required to produce a single unit of the target product.</p>
    </div>

    <div class="row justify-content-center">
        <div class="col-lg-12">
            <form action="{{ route('boms.store') }}" method="POST">
                @csrf
                <div class="row g-4">
                    <div class="col-lg-4">
                        <div class="card border-0 shadow-sm mb-4">
                            <div class="card-body">
                                <h6 class="fw-bold mb-3 text-primary">Target Product</h6>
                                <div class="mb-3">
                                    <label class="form-label small text-muted text-uppercase fw-bold">Select Product</label>
                                    <select name="product_id" class="form-select shadow-none" required>
                                        @foreach($products as $product)
                                            <option value="{{ $product->id }}" {{ $selectedProductId == $product->id ? 'selected' : '' }}>
                                                {{ $product->name }} ({{ $product->sku }})
                                            </option>
                                        @endforeach
                                    </select>
                                </div>
                                <div class="mb-3">
                                    <label class="form-label small text-muted text-uppercase fw-bold">Version</label>
                                    <input type="text" name="version" class="form-control" required placeholder="e.g. 1.0.0"
                                        value="1.0">
                                </div>
                                <div class="form-check form-switch mb-3">
                                    <input class="form-check-input" type="checkbox" name="is_active" id="isActive" checked>
                                    <label class="form-check-label fw-bold" for="isActive">Set as Active BOM</label>
                                </div>
                                <div class="mb-0">
                                    <label class="form-label small text-muted text-uppercase fw-bold">Notes</label>
                                    <textarea name="notes" class="form-control" rows="3"
                                        placeholder="Revision changes, manufacturing notes..."></textarea>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="col-lg-8">
                        <div class="card border-0 shadow-sm h-100">
                            <div
                                class="card-header bg-white py-3 border-0 d-flex justify-content-between align-items-center">
                                <h6 class="mb-0 fw-bold text-primary">Raw Material Consumption</h6>
                                <button type="button" class="btn btn-sm btn-outline-primary" onclick="addItem()">
                                    <i class="bi bi-plus-lg"></i> Add Material
                                </button>
                            </div>
                            <div class="card-body p-0">
                                <div class="table-responsive">
                                    <table class="table align-middle mb-0" id="bomTable">
                                        <thead class="bg-light">
                                            <tr>
                                                <th class="ps-4" style="width: 50%;">Raw Material / Component</th>
                                                <th style="width: 20%;">Qty Required</th>
                                                <th style="width: 20%;">Expected Waste %</th>
                                                <th style="width: 10%;"></th>
                                            </tr>
                                        </thead>
                                        <tbody id="bomItemsContainer">
                                            <!-- Items will be added here -->
                                        </tbody>
                                    </table>
                                </div>
                                <div id="emptyMessage" class="text-center py-5 text-muted">
                                    <i class="bi bi-list-nested display-4 mb-2 d-block opacity-25"></i>
                                    <p>No components added yet.</p>
                                    <button type="button" class="btn btn-link py-0" onclick="addItem()">Add your first
                                        component</button>
                                </div>
                            </div>
                            <div class="card-footer bg-white border-top-0 p-4 pt-0 text-end">
                                <button type="submit" class="btn btn-primary px-5 py-2 mt-3">Validate & Save BOM</button>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <script>
        let itemIndex = 0;

        function addItem() {
            document.getElementById('emptyMessage').classList.add('d-none');
            const container = document.getElementById('bomItemsContainer');
            const row = document.createElement('tr');
            row.innerHTML = `
                <td class="ps-4">
                    <select name="items[${itemIndex}][product_id]" class="form-select border-0 bg-light rounded-3" required>
                        <option value="">Select material...</option>
                        @foreach($components as $comp)
                            <option value="{{ $comp->id }}" class="{{ $comp->category == 'raw_material' ? 'fw-bold' : '' }}">
                                {{ $comp->name }} ({{ $comp->category == 'raw_material' ? 'RM' : 'Comp' }} - {{ $comp->sku }})
                            </option>
                        @endforeach
                    </select>
                </td>
                <td>
                    <input type="number" step="0.0001" name="items[${itemIndex}][quantity]" class="form-control border-0 bg-light rounded-3" required placeholder="0.00">
                </td>
                <td>
                    <input type="number" step="0.01" name="items[${itemIndex}][waste_percentage]" class="form-control border-0 bg-light rounded-3" value="0.00">
                </td>
                <td class="pe-4 text-end">
                    <button type="button" class="btn btn-link text-danger p-0" onclick="removeItem(this)">
                        <i class="bi bi-trash"></i>
                    </button>
                </td>
            `;
            container.appendChild(row);
            itemIndex++;
        }

        function removeItem(btn) {
            btn.closest('tr').remove();
            if (document.querySelectorAll('#bomItemsContainer tr').length === 0) {
                document.getElementById('emptyMessage').classList.remove('d-none');
            }
        }

        // Add first item automatically if none
        if (document.querySelectorAll('#bomItemsContainer tr').length === 0) {
            addItem();
        }
    </script>
@endsection