@extends('layouts.app')

@section('title', 'Work Orders')

@section('content')
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="mb-0">Production Work Orders</h4>
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb mb-0">
                    <li class="breadcrumb-item"><a href="{{ url('/') }}">Dashboard</a></li>
                    <li class="breadcrumb-item active">Work Orders</li>
                </ol>
            </nav>
        </div>
        <a href="{{ route('work-orders.create') }}" class="btn btn-primary px-4">
            <i class="bi bi-file-earmark-plus me-2"></i> New Work Order
        </a>
    </div>

    <div class="row g-3 mb-4">
        @php
            $stats = [
                ['label' => 'Total Orders', 'value' => $workOrders->total(), 'icon' => 'bi-list-ul', 'color' => 'primary'],
                ['label' => 'In Progress', 'value' => $workOrders->where('status', 'in_progress')->count(), 'icon' => 'bi-gear-wide-connected', 'color' => 'warning'],
                ['label' => 'Completed', 'value' => $workOrders->where('status', 'completed')->count(), 'icon' => 'bi-check-circle', 'color' => 'success'],
                ['label' => 'Draft / Released', 'value' => $workOrders->whereIn('status', ['draft', 'released'])->count(), 'icon' => 'bi-clock-history', 'color' => 'info'],
            ];
        @endphp
        @foreach($stats as $stat)
            <div class="col-md-3">
                <div class="card border-0 shadow-sm">
                    <div class="card-body py-3 d-flex align-items-center">
                        <div class="rounded-circle bg-{{ $stat['color'] }}-subtle text-{{ $stat['color'] }} p-2 me-3">
                            <i class="bi {{ $stat['icon'] }} fs-5"></i>
                        </div>
                        <div>
                            <div class="text-muted small fw-bold">{{ $stat['label'] }}</div>
                            <div class="fs-4 fw-bold">{{ $stat['value'] }}</div>
                        </div>
                    </div>
                </div>
            </div>
        @endforeach
    </div>

    <div class="card shadow-sm border-0">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="bg-light">
                        <tr>
                            <th class="ps-4">Order No</th>
                            <th>Product</th>
                            <th>BOM</th>
                            <th>Quantity</th>
                            <th>Priority</th>
                            <th>Status</th>
                            <th class="text-end pe-4">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($workOrders as $order)
                            <tr>
                                <td class="ps-4">
                                    <div class="fw-bold">{{ $order->work_order_no }}</div>
                                    <span class="text-muted small">{{ $order->start_date->format('M d') }} -
                                        {{ $order->end_date->format('M d') }}</span>
                                </td>
                                <td>{{ $order->product->name }}</td>
                                <td><span class="small text-muted">v{{ $order->bom->version }}</span></td>
                                <td class="fw-semibold">{{ number_format($order->quantity) }}</td>
                                <td>
                                    @php
                                        $p_colors = ['low' => 'secondary', 'medium' => 'info', 'high' => 'warning', 'urgent' => 'danger'];
                                    @endphp
                                    <span
                                        class="badge bg-{{ $p_colors[$order->priority] }}-subtle text-{{ $p_colors[$order->priority] }} border border-{{ $p_colors[$order->priority] }}-subtle">
                                        {{ ucfirst($order->priority) }}
                                    </span>
                                </td>
                                <td>
                                    @php
                                        $s_colors = ['draft' => 'secondary', 'released' => 'primary', 'in_progress' => 'warning', 'completed' => 'success', 'cancelled' => 'danger'];
                                    @endphp
                                    <span
                                        class="badge bg-{{ $s_colors[$order->status] }}">{{ str_replace('_', ' ', ucfirst($order->status)) }}</span>
                                </td>
                                <td class="text-end pe-4">
                                    <a href="{{ route('work-orders.show', $order->id) }}"
                                        class="btn btn-light btn-sm rounded-pill px-3">Manage</a>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="7" class="text-center py-5 text-muted italic">Plan your production. No work orders
                                    found.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div class="mt-4">
        {{ $workOrders->links() }}
    </div>
@endsection