@extends('layouts.admin')
@section('title','WO '.$workorder->wo_number)
@section('content')
@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
<div class="row">
<div class="col-lg-8">
  <div class="card mb-3">
    <div class="card-header d-flex justify-content-between align-items-center">
      <div>
        <span class="fw-bold text-primary">{{ $workorder->wo_number }}</span>
        <span class="badge {{ $statusColors[$workorder->status]??'bg-secondary' }} ms-2">{{ str_replace('_',' ',strtoupper($workorder->status)) }}</span>
        <span class="badge bg-light text-dark border ms-1">{{ $workorder->division }}</span>
      </div>
      <div class="d-flex gap-2">
        <a href="{{ route('admin.workorders.edit',$workorder) }}" class="btn btn-sm btn-outline-secondary"><i class="fas fa-edit"></i></a>
        @if(auth()->user()->role === 'admin')
        <form action="{{ route('admin.workorders.destroy',$workorder) }}" 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
      </div>
    </div>
    <div class="card-body">
      <table class="table table-sm mb-3">
        <tr><td class="text-muted small" width="160">Item</td><td class="fw-semibold">{{ $workorder->item_name }} {{ $workorder->item_brand }}</td></tr>
        <tr><td class="text-muted small">Serial No</td><td class="small">{{ $workorder->item_serial ?? '—' }}</td></tr>
        <tr><td class="text-muted small">Customer</td><td class="small">{{ $workorder->customer_name ?? $workorder->client?->name ?? '—' }} {{ $workorder->customer_phone ? '('.$workorder->customer_phone.')' : '' }}</td></tr>
        <tr><td class="text-muted small">Teknisi</td><td class="small">{{ $workorder->assignedTo?->name ?? '—' }}</td></tr>
        <tr><td class="text-muted small">Diterima</td><td class="small">{{ $workorder->received_at?->format('d/m/Y H:i') ?? '—' }}</td></tr>
      </table>

      <div class="mb-3">
        <div class="fw-semibold small mb-1">Keluhan:</div>
        <div class="p-2 bg-light rounded small" style="white-space:pre-wrap">{{ $workorder->reported_issue }}</div>
      </div>
      @if($workorder->diagnosis)
      <div class="mb-3">
        <div class="fw-semibold small mb-1">Diagnosa:</div>
        <div class="p-2 bg-light rounded small" style="white-space:pre-wrap">{{ $workorder->diagnosis }}</div>
      </div>
      @endif
      @if($workorder->action_taken)
      <div class="mb-3">
        <div class="fw-semibold small mb-1">Tindakan:</div>
        <div class="p-2 bg-light rounded small" style="white-space:pre-wrap">{{ $workorder->action_taken }}</div>
      </div>
      @endif
      @if($workorder->estimated_cost > 0 || $workorder->final_cost > 0)
      <div class="d-flex gap-3 mb-3">
        <div><div class="text-muted small">Est. Biaya</div><div class="fw-semibold">Rp {{ number_format($workorder->estimated_cost,0,',','.') }}</div></div>
        <div><div class="text-muted small">Biaya Final</div><div class="fw-semibold text-success">Rp {{ number_format($workorder->final_cost,0,',','.') }}</div></div>
      </div>
      @endif
    </div>
  </div>

  {{-- Photos --}}
  @if($workorder->photos->count())
  <div class="card mb-3">
    <div class="card-header">Foto</div>
    <div class="card-body">
      @foreach(['before'=>'Sebelum','after'=>'Sesudah','delivery'=>'Serah Terima'] as $type=>$label)
      @php $photos = $workorder->photos->where('type',$type); @endphp
      @if($photos->count())
      <div class="mb-3">
        <div class="fw-semibold small mb-2">{{ $label }}:</div>
        <div class="d-flex flex-wrap gap-2">
          @foreach($photos as $p)
          <a href="{{ asset('storage/'.$p->path) }}" target="_blank">
            <img src="{{ asset('storage/'.$p->path) }}" style="width:100px;height:80px;object-fit:cover;border-radius:6px;border:1px solid #e5e7eb">
          </a>
          @endforeach
        </div>
      </div>
      @endif
      @endforeach
    </div>
  </div>
  @endif

  {{-- Upload photo --}}
  <div class="card">
    <div class="card-header">Upload Foto</div>
    <div class="card-body">
      <form action="{{ route('admin.workorders.photo',$workorder) }}" method="POST" enctype="multipart/form-data">@csrf
        <div class="row g-2 align-items-end">
          <div class="col-md-4">
            <select name="type" class="form-select form-select-sm">
              <option value="before">Sebelum</option>
              <option value="after">Sesudah</option>
              <option value="delivery">Serah Terima</option>
            </select>
          </div>
          <div class="col-md-6"><input type="file" name="photo" class="form-control form-control-sm" accept="image/*" required></div>
          <div class="col-md-2"><button type="submit" class="btn btn-primary btn-sm w-100"><i class="fas fa-upload"></i></button></div>
        </div>
      </form>
    </div>
  </div>
</div>

<div class="col-lg-4">
  {{-- Status change --}}
  <div class="card mb-3">
    <div class="card-header">Update Status</div>
    <div class="card-body">
      <form action="{{ route('admin.workorders.status',$workorder) }}" method="POST">@csrf
        <select name="status" class="form-select form-select-sm mb-2">
          @foreach(['received','diagnosed','waiting_approval','in_repair','done','delivered','cancelled'] as $s)
          <option value="{{ $s }}" {{ $workorder->status===$s?'selected':'' }}>{{ str_replace('_',' ',strtoupper($s)) }}</option>
          @endforeach
        </select>
        <button type="submit" class="btn btn-primary btn-sm w-100">Update</button>
      </form>
    </div>
  </div>

  {{-- Quick edit diagnosis --}}
  <div class="card mb-3">
    <div class="card-header">Diagnosa & Biaya</div>
    <div class="card-body">
      <form action="{{ route('admin.workorders.update',$workorder) }}" method="POST">@csrf @method('PUT')
        <div class="mb-2"><label class="form-label small fw-semibold">Diagnosa</label><textarea name="diagnosis" class="form-control form-control-sm" rows="3">{{ $workorder->diagnosis }}</textarea></div>
        <div class="mb-2"><label class="form-label small fw-semibold">Tindakan</label><textarea name="action_taken" class="form-control form-control-sm" rows="3">{{ $workorder->action_taken }}</textarea></div>
        <div class="row g-2">
          <div class="col-6"><label class="form-label small fw-semibold">Est. Biaya</label><input type="number" name="estimated_cost" class="form-control form-control-sm" value="{{ $workorder->estimated_cost }}"></div>
          <div class="col-6"><label class="form-label small fw-semibold">Biaya Final</label><input type="number" name="final_cost" class="form-control form-control-sm" value="{{ $workorder->final_cost }}"></div>
        </div>
        <div class="mt-2"><label class="form-label small fw-semibold">Teknisi</label>
          <select name="assigned_to" class="form-select form-select-sm">
            <option value="">—</option>
            @foreach($technicians as $t)<option value="{{ $t->id }}" {{ $workorder->assigned_to==$t->id?'selected':'' }}>{{ $t->name }}</option>@endforeach
          </select>
        </div>
        <button type="submit" class="btn btn-secondary btn-sm w-100 mt-2"><i class="fas fa-save me-1"></i>Simpan</button>
      </form>
    </div>
  </div>
</div>
</div>
@endsection
