@extends('layouts.app')

@section('title', 'New Work Order')

@section('content')
    <div class="mb-4">
        <h4 class="mb-0">Create Production Order</h4>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb mb-0">
                <li class="breadcrumb-item"><a href="{{ url('/') }}">Dashboard</a></li>
                <li class="breadcrumb-item"><a href="{{ route('work-orders.index') }}">Work Orders</a></li>
                <li class="breadcrumb-item active">New Order</li>
            </ol>
        </nav>
    </div>

    <div class="row justify-content-center">
        <div class="col-lg-8">
            <div class="card shadow-sm border-0">
                <div class="card-body p-4">
                    @if($products->isEmpty())
                        <div class="alert alert-warning border-0 shadow-sm d-flex align-items-center">
                            <i class="bi bi-exclamation-triangle-fill fs-4 me-3"></i>
                            <div>
                                <strong>No Active BOMs Found!</strong><br>
                                You must configure and activate a Bill of Materials for a product before you can create a work
                                order.
                                <a href="{{ route('products.index') }}" class="alert-link">Go to Product Catalog</a>.
                            </div>
                        </div>
                    @endif

                    <form action="{{ route('work-orders.store') }}" method="POST">
                        @csrf

                        <div class="row g-4">
                            <div class="col-md-12">
                                <label class="form-label fw-bold small text-muted text-uppercase">Select Product to
                                    Manufacture <span class="text-danger">*</span></label>
                                <select name="product_id" class="form-select form-select-lg" required {{ $products->isEmpty() ? 'disabled' : '' }}>
                                    <option value="">Choose item...</option>
                                    @foreach($products as $product)
                                        <option value="{{ $product->id }}">
                                            {{ $product->name }} (Active BOM: v{{ $product->activeBom->version }})
                                        </option>
                                    @endforeach
                                </select>
                            </div>

                            <div class="col-md-6">
                                <label class="form-label fw-bold small text-muted text-uppercase">Target Quantity <span
                                        class="text-danger">*</span></label>
                                <input type="number" name="quantity" class="form-control" required placeholder="0" min="1">
                            </div>

                            <div class="col-md-6">
                                <label class="form-label fw-bold small text-muted text-uppercase">Priority <span
                                        class="text-danger">*</span></label>
                                <select name="priority" class="form-select" required>
                                    <option value="medium">Medium</option>
                                    <option value="low">Low</option>
                                    <option value="high">High</option>
                                    <option value="urgent">Urgent</option>
                                </select>
                            </div>

                            <div class="col-md-6">
                                <label class="form-label fw-bold small text-muted text-uppercase">Production Line</label>
                                <select name="production_line_id" class="form-select">
                                    <option value="">Select line (Optional)...</option>
                                    @foreach($productionLines as $line)
                                        <option value="{{ $line->id }}">{{ $line->name }}</option>
                                    @endforeach
                                </select>
                            </div>

                            <div class="col-md-6">
                                <label class="form-label fw-bold small text-muted text-uppercase">Primary Machine</label>
                                <select name="machine_id" class="form-select">
                                    <option value="">Select machine (Optional)...</option>
                                    @foreach($machines as $machine)
                                        <option value="{{ $machine->id }}">{{ $machine->name }}
                                            ({{ $machine->productionLine->name ?? 'No Line' }})</option>
                                    @endforeach
                                </select>
                            </div>

                            <div class="col-md-6">
                                <label class="form-label fw-bold small text-muted text-uppercase">Planned Start Date <span
                                        class="text-danger">*</span></label>
                                <input type="date" name="start_date" class="form-control" required
                                    value="{{ date('Y-m-d') }}">
                            </div>

                            <div class="col-md-6">
                                <label class="form-label fw-bold small text-muted text-uppercase">Planned Completion Date
                                    <span class="text-danger">*</span></label>
                                <input type="date" name="end_date" class="form-control" required
                                    value="{{ date('Y-m-d', strtotime('+7 days')) }}">
                            </div>

                            <div class="col-md-12">
                                <label class="form-label fw-bold small text-muted text-uppercase">Production Notes</label>
                                <textarea name="notes" class="form-control" rows="3"
                                    placeholder="Special assembly instructions, quality checks..."></textarea>
                            </div>
                        </div>

                        <div class="mt-5 d-flex justify-content-end gap-2 text-primary">
                            <a href="{{ route('work-orders.index') }}" class="btn btn-light px-4">Cancel</a>
                            <button type="submit" class="btn btn-primary px-5 py-2 shadow-sm" {{ $products->isEmpty() ? 'disabled' : '' }}>
                                Generate Work Order
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
@endsection