@extends('layouts.app')

@section('title', 'Quality Control')

@section('content')
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="mb-0">Quality Control Inspections</h4>
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb mb-0 small">
                    <li class="breadcrumb-item"><a href="{{ url('/') }}">Dashboard</a></li>
                    <li class="breadcrumb-item active">Quality Assurance</li>
                </ol>
            </nav>
        </div>
        <a href="{{ route('quality-checks.create') }}" class="btn btn-warning px-4 shadow-sm">
            <i class="bi bi-shield-check me-2"></i> New Inspection
        </a>
    </div>

    <div class="card border-0 shadow-sm">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table align-middle mb-0 table-hover">
                    <thead class="bg-light">
                        <tr>
                            <th class="ps-4">Product</th>
                            <th>Reference</th>
                            <th>Quantity Good/Bad</th>
                            <th>Inspector</th>
                            <th>Status</th>
                            <th class="text-end pe-4">Date</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($qualityChecks as $check)
                            <tr>
                                <td class="ps-4">
                                    <div class="fw-bold">{{ $check->product->name }}</div>
                                    <code class="smallest">{{ $check->product->sku }}</code>
                                </td>
                                <td>
                                    @if($check->workOrder)
                                        <a href="{{ route('work-orders.show', $check->workOrder->id) }}"
                                            class="text-decoration-none">
                                            {{ $check->workOrder->order_number }}
                                        </a>
                                    @else
                                        <span class="text-muted">Direct Check</span>
                                    @endif
                                </td>
                                <td>
                                    <div class="d-flex align-items-center gap-2">
                                        <span
                                            class="badge bg-success-subtle text-success">{{ number_format($check->passed_quantity, 0) }}
                                            ok</span>
                                        <span
                                            class="badge bg-danger-subtle text-danger">{{ number_format($check->failed_quantity, 0) }}
                                            reject</span>
                                    </div>
                                    <div class="progress mt-1" style="height: 4px;">
                                        @php $perc = ($check->checked_quantity > 0) ? ($check->passed_quantity / $check->checked_quantity) * 100 : 0; @endphp
                                        <div class="progress-bar bg-success" style="width: {{ $perc }}%"></div>
                                    </div>
                                </td>
                                <td>
                                    <div class="small fw-bold">{{ $check->inspector->name }}</div>
                                </td>
                                <td>
                                    @php
                                        $class = [
                                            'pass' => 'success',
                                            'fail' => 'danger',
                                            'conditional_pass' => 'warning'
                                        ][$check->status] ?? 'secondary';
                                    @endphp
                                    <span class="badge bg-{{ $class }} text-uppercase">{{ $check->status }}</span>
                                </td>
                                <td class="text-end pe-4">
                                    <div class="small text-muted">{{ $check->created_at->format('M d, Y') }}</div>
                                    <div class="smallest text-muted">{{ $check->created_at->format('H:i') }}</div>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="6" class="text-center py-5 text-muted">No quality inspections recorded yet.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
        @if($qualityChecks->hasPages())
            <div class="card-footer bg-white border-0">{{ $qualityChecks->links() }}</div>
        @endif
    </div>
@endsection