@extends('layouts.app')

@section('title', 'QC Details - ' . $qc->qc_number)

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h4 class="card-title">QC Inspection Details - {{ $qc->qc_number }}</h4>
                    <div class="card-tools">
                        <a href="{{ route('qcs.index') }}" class="btn btn-secondary">
                            <i class="fas fa-arrow-left"></i> Back to QC List
                        </a>
                        @if($qc->status === 'pending')
                        <a href="{{ route('qcs.create', $qc->grn) }}" class="btn btn-primary">
                            <i class="fas fa-edit"></i> Edit QC
                        </a>
                        @endif
                    </div>
                </div>
                <div class="card-body">
                    <!-- QC Information -->
                    <div class="row mb-4">
                        <div class="col-md-6">
                            <h5>QC Information</h5>
                            <table class="table table-sm">
                                <tr>
                                    <td><strong>QC Number:</strong></td>
                                    <td>{{ $qc->qc_number }}</td>
                                </tr>
                                <tr>
                                    <td><strong>Status:</strong></td>
                                    <td>
                                        <span class="badge bg-{{ $qc->status === 'pending' ? 'warning' : ($qc->status === 'completed' ? 'success' : ($qc->status === 'cancelled' ? 'danger' : 'info')) }}">
                                            {{ ucfirst(str_replace('_', ' ', $qc->status)) }}
                                        </span>
                                    </td>
                                </tr>
                                <tr>
                                    <td><strong>Inspector:</strong></td>
                                    <td>{{ $qc->inspector->name ?? 'N/A' }}</td>
                                </tr>
                                <tr>
                                    <td><strong>Inspected At:</strong></td>
                                    <td>{{ $qc->inspected_at ? $qc->inspected_at->format('d/m/Y H:i') : 'N/A' }}</td>
                                </tr>
                                <tr>
                                    <td><strong>Created:</strong></td>
                                    <td>{{ $qc->created_at->format('d/m/Y H:i') }}</td>
                                </tr>
                                @if($qc->status === 'cancelled')
                                <tr>
                                    <td><strong>Cancelled By:</strong></td>
                                    <td>{{ $qc->cancelledBy->name ?? 'N/A' }}</td>
                                </tr>
                                <tr>
                                    <td><strong>Cancelled At:</strong></td>
                                    <td>{{ $qc->cancelled_at ? $qc->cancelled_at->format('d/m/Y H:i') : 'N/A' }}</td>
                                </tr>
                                <tr>
                                    <td><strong>Reason:</strong></td>
                                    <td>{{ $qc->cancelled_reason ?? 'N/A' }}</td>
                                </tr>
                                @endif
                            </table>
                        </div>
                        <div class="col-md-6">
                            <h5>GRN Information</h5>
                            <table class="table table-sm">
                                <tr>
                                    <td><strong>GRN Number:</strong></td>
                                    <td>{{ $qc->grn->grn_number }}</td>
                                </tr>
                                <tr>
                                    <td><strong>PO Number:</strong></td>
                                    <td>{{ $qc->grn->purchaseOrder->po_number }}</td>
                                </tr>
                                <tr>
                                    <td><strong>Supplier:</strong></td>
                                    <td>{{ $qc->grn->purchaseOrder->vendor->name }}</td>
                                </tr>
                                <tr>
                                    <td><strong>Received Date:</strong></td>
                                    <td>{{ $qc->grn->received_date->format('d/m/Y') }}</td>
                                </tr>
                                <tr>
                                    <td><strong>GRN Status:</strong></td>
                                    <td>
                                        <span class="badge bg-{{ $qc->grn->status === 'accepted' ? 'success' : ($qc->grn->status === 'rejected' ? 'danger' : 'warning') }}">
                                            {{ ucfirst($qc->grn->status) }}
                                        </span>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>

                    <!-- QC Summary -->
                    @if($qc->status === 'completed')
                    <div class="row mb-4">
                        <div class="col-md-12">
                            <h5>QC Summary</h5>
                            <div class="row">
                                <div class="col-md-3">
                                    <div class="info-box bg-success">
                                        <div class="info-box-content p-3">
                                            <h6>Total Passed</h6>
                                            <h4>{{ $qc->total_passed_qty }}</h4>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="info-box bg-danger">
                                        <div class="info-box-content p-3">
                                            <h6>Total Failed</h6>
                                            <h4>{{ $qc->total_failed_qty }}</h4>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="info-box bg-info">
                                        <div class="info-box-content p-3">
                                            <h6>Items Inspected</h6>
                                            <h4>{{ $qc->qcItems->count() }}</h4>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="info-box bg-warning">
                                        <div class="info-box-content p-3">
                                            <h6>Pass Rate</h6>
                                            <h4>{{ $qc->total_passed_qty + $qc->total_failed_qty > 0 ? round(($qc->total_passed_qty / ($qc->total_passed_qty + $qc->total_failed_qty)) * 100, 1) : 0 }}%</h4>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    @endif

                    <!-- QC Items -->
                    <div class="row">
                        <div class="col-md-12">
                            <h5>QC Items</h5>
                            <div class="table-responsive">
                                <table class="table table-bordered table-striped">
                                    <thead>
                                        <tr>
                                            <th>Item</th>
                                            <th>Received Qty</th>
                                            <th>Passed Qty</th>
                                            <th>Failed Qty</th>
                                            <th>Available Bin</th>
                                            <th>Rejected Bin</th>
                                            <th>Lot No</th>
                                            <th>Remarks</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach($qc->qcItems as $qcItem)
                                        <tr>
                                            <td>
                                                <strong>{{ $qcItem->item->name }}</strong><br>
                                                <small class="text-muted">{{ $qcItem->item->sku }}</small>
                                            </td>
                                            <td>{{ $qcItem->grnItem->received_quantity }}</td>
                                            <td>
                                                <span class="badge bg-success">{{ $qcItem->qty_passed }}</span>
                                            </td>
                                            <td>
                                                <span class="badge bg-danger">{{ $qcItem->qty_failed }}</span>
                                            </td>
                                            <td>
                                                @if($qcItem->availableBin)
                                                    <span class="badge bg-primary">{{ $qcItem->availableBin->code }}</span>
                                                @else
                                                    <span class="text-muted">N/A</span>
                                                @endif
                                            </td>
                                            <td>
                                                @if($qcItem->rejectedBin)
                                                    <span class="badge bg-secondary">{{ $qcItem->rejectedBin->code }}</span>
                                                @else
                                                    <span class="text-muted">N/A</span>
                                                @endif
                                            </td>
                                            <td>{{ $qcItem->lot_no ?? 'N/A' }}</td>
                                            <td>{{ $qcItem->remarks ?? 'N/A' }}</td>
                                        </tr>
                                        @endforeach
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>

                    <!-- Remarks -->
                    @if($qc->remarks)
                    <div class="row mt-4">
                        <div class="col-md-12">
                            <h5>General Remarks</h5>
                            <div class="alert alert-info">
                                {{ $qc->remarks }}
                            </div>
                        </div>
                    </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
