@extends('layouts.app')

@section('header')
<div class="px-4 sm:px-6 lg:px-8 h-16 flex items-center justify-between">
  <div class="flex items-center gap-3">
    <div class="h-9 w-9 rounded-xl bg-slate-900 text-white grid place-items-center text-sm font-bold">AD</div>
    <div>
      <div class="font-semibold">Edit Task</div>
    </div>
  </div>
  <a href="{{ route('admin.tasks.show', $task) }}" class="rounded-lg border px-4 py-2.5">Back</a>
</div>
@endsection

@section('sidebar')
<nav class="p-4">
  <a href="{{ route('admin.tasks.index') }}" class="block px-3 py-2 rounded-lg hover:bg-slate-50">Tasks</a>
</nav>
@endsection

@section('content')
@if ($errors->any())
  <div class="mb-4 rounded-xl border border-red-200 bg-red-50 px-4 py-3 text-red-700">
    {{ $errors->first() }}
  </div>
@endif

<form method="POST" action="{{ route('admin.tasks.update', $task) }}" class="bg-white rounded-2xl border shadow-sm p-6 max-w-3xl">
  @csrf @method('PUT')
  <div class="grid gap-4">
    <div class="grid md:grid-cols-2 gap-4">
      <div>
        <label class="block text-sm font-medium mb-1">Department</label>
        <select name="department_id" required class="w-full rounded-xl border-slate-300 focus:border-brand-500 focus:ring-brand-500">
          @foreach($departments as $d)
            <option value="{{ $d->id }}" @selected($task->department_id==$d->id)>{{ $d->name }}</option>
          @endforeach
        </select>
      </div>
      <div>
        <label class="block text-sm font-medium mb-1">Priority</label>
        <select name="priority" required class="w-full rounded-xl border-slate-300 focus:border-brand-500 focus:ring-brand-500">
          @foreach(['low','medium','high','critical'] as $p)
            <option value="{{ $p }}" @selected($task->priority===$p)>{{ ucfirst($p) }}</option>
          @endforeach
        </select>
      </div>
    </div>

    <div>
      <label class="block text-sm font-medium mb-1">Title</label>
      <input name="title" value="{{ old('title',$task->title) }}" required class="w-full rounded-xl border-slate-300 focus:border-brand-500 focus:ring-brand-500">
    </div>

    <div>
      <label class="block text-sm font-medium mb-1">Description</label>
      <textarea name="description" rows="4" class="w-full rounded-xl border-slate-300 focus:border-brand-500 focus:ring-brand-500">{{ old('description',$task->description) }}</textarea>
    </div>

    <div class="grid md:grid-cols-3 gap-4">
      <div>
        <label class="block text-sm font-medium mb-1">Start date</label>
        <input type="date" name="start_date" value="{{ $task->start_date?->format('Y-m-d') }}" class="w-full rounded-xl border-slate-300 focus:border-brand-500 focus:ring-brand-500">
      </div>
      <div>
        <label class="block text-sm font-medium mb-1">Due date</label>
        <input type="date" name="due_date" value="{{ $task->due_date?->format('Y-m-d') }}" class="w-full rounded-xl border-slate-300 focus:border-brand-500 focus:ring-brand-500">
      </div>
      <div>
        <label class="block text-sm font-medium mb-1">Estimate (min)</label>
        <input type="number" name="estimate_minutes" min="0" value="{{ $task->estimate_minutes }}" class="w-full rounded-xl border-slate-300 focus:border-brand-500 focus:ring-brand-500">
      </div>
    </div>

    <div class="grid md:grid-cols-2 gap-4">
      <div>
        <label class="block text-sm font-medium mb-1">Complexity (1–5)</label>
        <input type="number" name="complexity" min="1" max="5" value="{{ $task->complexity }}" class="w-full rounded-xl border-slate-300 focus:border-brand-500 focus:ring-brand-500">
      </div>
      <div>
        <label class="block text-sm font-medium mb-1">Status</label>
        <select name="status" class="w-full rounded-xl border-slate-300 focus:border-brand-500 focus:ring-brand-500">
          @foreach(['backlog','todo','in_progress','blocked','done','cancelled'] as $s)
            <option value="{{ $s }}" @selected($task->status===$s)>{{ ucfirst(str_replace('_',' ',$s)) }}</option>
          @endforeach
        </select>
      </div>
    </div>

    <div class="pt-2">
      <button class="rounded-xl bg-brand-500 hover:bg-brand-600 text-white px-5 py-2.5 font-medium">Save</button>
    </div>
  </div>
</form>
@endsection
