{{-- workorders/index.blade.php --}}
@extends('layouts.admin')
@section('title','Work Order (FixLab & Havva)')
@section('content')
<div class="card mb-3">
  <div class="card-body py-2">
    <form class="row g-2" method="GET">
      <div class="col-md-4"><input type="text" name="search" class="form-control form-control-sm" placeholder="Cari WO / item / customer..." value="{{ request('search') }}"></div>
      <div class="col-md-2"><select name="division" class="form-select form-select-sm"><option value="">Semua Divisi</option><option value="FixLab" {{ request('division')==='FixLab'?'selected':'' }}>FixLab</option><option value="Havva" {{ request('division')==='Havva'?'selected':'' }}>Havva</option></select></div>
      <div class="col-md-2"><select name="status" class="form-select form-select-sm"><option value="">Semua Status</option>@foreach(['received','diagnosed','waiting_approval','in_repair','done','delivered','cancelled'] as $s)<option value="{{ $s }}" {{ request('status')===$s?'selected':'' }}>{{ strtoupper($s) }}</option>@endforeach</select></div>
      <div class="col-auto"><button type="submit" class="btn btn-primary btn-sm"><i class="fas fa-search"></i></button><a href="{{ route('admin.workorders.index') }}" class="btn btn-secondary btn-sm ms-1"><i class="fas fa-xmark"></i></a></div>
    </form>
  </div>
</div>
<div class="card">
  <div class="card-header d-flex justify-content-between align-items-center">
    <span>Work Order <span class="badge bg-secondary">{{ $workorders->total() }}</span></span>
    <a href="{{ route('admin.workorders.create') }}" class="btn btn-primary btn-sm"><i class="fas fa-plus me-1"></i>Buat WO</a>
  </div>
  <div class="table-responsive">
    <table class="table table-hover mb-0">
      <thead><tr><th>No. WO</th><th>Divisi</th><th>Item</th><th>Customer</th><th>Teknisi</th><th>Status</th><th>Masuk</th><th></th></tr></thead>
      <tbody>
      @forelse($workorders as $wo)
      @php $statusColors = ['received'=>'bg-secondary','diagnosed'=>'bg-info','waiting_approval'=>'bg-warning text-dark','in_repair'=>'bg-primary','done'=>'bg-success','delivered'=>'bg-dark','cancelled'=>'bg-danger']; @endphp
      <tr>
        <td><a href="{{ route('admin.workorders.show',$wo) }}" class="fw-semibold text-decoration-none text-primary">{{ $wo->wo_number }}</a></td>
        <td><span class="badge bg-light text-dark border">{{ $wo->division }}</span></td>
        <td><div class="small fw-semibold">{{ $wo->item_name }}</div><div class="text-muted" style="font-size:11px">{{ $wo->item_brand }}</div></td>
        <td class="small">{{ $wo->customer_name ?? $wo->client?->name ?? '—' }}</td>
        <td class="small">{{ $wo->assignedTo?->name ?? '—' }}</td>
        <td><span class="badge {{ $statusColors[$wo->status]??'bg-secondary' }}">{{ str_replace('_',' ',strtoupper($wo->status)) }}</span></td>
        <td class="text-muted small">{{ $wo->received_at?->format('d/m/Y') ?? $wo->created_at->format('d/m/Y') }}</td>
        <td><a href="{{ route('admin.workorders.show',$wo) }}" class="btn btn-sm btn-outline-primary"><i class="fas fa-eye"></i></a>
        @if(auth()->user()->role === 'admin')
        <form action="{{ route('admin.workorders.destroy',$wo) }}" method="POST" onsubmit="return confirm('Hapus work order ini?')" style="display:inline">@csrf @method('DELETE')<button class="btn btn-sm btn-outline-danger"><i class="fas fa-trash"></i></button></form>
        @endif
        </td>
      </tr>
      @empty<tr><td colspan="8" class="text-center text-muted py-4">Tidak ada work order</td></tr>
      @endforelse
      </tbody>
    </table>
  </div>
  <div class="card-footer">{{ $workorders->links() }}</div>
</div>
@endsection
