@props(['project'])
@php($milestones=$project->milestones()->with('dependency')->orderBy('due_date')->get())
<div class="card mt-4"><div class="card-header"><h5 class="mb-0">Milestone plan, dependencies &amp; variance</h5></div><div class="card-body"><form method="POST" action="{{ route('projects.milestones.store',$project) }}" class="row g-2 mb-4">@csrf<div class="col-md-3"><input class="form-control" name="name" placeholder="Milestone name" required></div><div class="col-md-2"><input class="form-control" type="date" name="baseline_date" title="Baseline date"></div><div class="col-md-2"><input class="form-control" type="date" name="due_date" title="Planned date"></div><div class="col-md-2"><select class="form-select" name="depends_on_milestone_id"><option value="">No dependency</option>@foreach($milestones as $milestone)<option value="{{ $milestone->id }}">{{ $milestone->name }}</option>@endforeach</select></div><div class="col-md-3"><input class="form-control" name="blocking_reason" placeholder="Blocking reason"></div><div class="col-md-3"><textarea class="form-control" name="description" placeholder="Description"></textarea></div><div class="col-md-2"><select class="form-select" name="status">@foreach(['pending','in_progress','completed','cancelled'] as $status)<option>{{ $status }}</option>@endforeach</select></div><div class="col-auto"><button class="btn btn-primary">Add milestone</button></div></form><div class="table-responsive"><table class="table table-sm"><thead><tr><th>Milestone</th><th>Dependency / blocked reason</th><th>Baseline</th><th>Planned</th><th>Actual</th><th>Variance</th><th>Status</th><th></th></tr></thead><tbody>@forelse($milestones as $milestone)@php($variance=$milestone->actual_date&&$milestone->due_date?$milestone->due_date->diffInDays($milestone->actual_date,false):null)<tr class="{{ $milestone->dependency && $milestone->dependency->status!=='completed'?'table-warning':'' }}"><td>{{ $milestone->name }}</td><td>{{ $milestone->dependency?->name ?: '—' }}<small class="d-block">{{ $milestone->blocking_reason }}</small></td><td>{{ $milestone->baseline_date }}</td><td>{{ $milestone->due_date }}</td><td>{{ $milestone->actual_date }}</td><td>{{ is_null($variance)?'—':($variance>0?'+'.$variance:$variance).' days' }}</td><td>{{ ucfirst($milestone->status) }}</td><td><details><summary>Edit</summary><form method="POST" action="{{ route('projects.milestones.update',[$project,$milestone]) }}" class="row g-1 mt-1">@csrf @method('PUT')<input class="form-control" name="name" value="{{ $milestone->name }}" required><input class="form-control" name="description" value="{{ $milestone->description }}"><input class="form-control" type="date" name="baseline_date" value="{{ $milestone->baseline_date?->format('Y-m-d') }}"><input class="form-control" type="date" name="due_date" value="{{ $milestone->due_date?->format('Y-m-d') }}"><select class="form-select" name="depends_on_milestone_id"><option value="">No dependency</option>@foreach($milestones->where('id','!=',$milestone->id) as $candidate)<option value="{{ $candidate->id }}" @selected($milestone->depends_on_milestone_id===$candidate->id)>{{ $candidate->name }}</option>@endforeach</select><input class="form-control" name="blocking_reason" value="{{ $milestone->blocking_reason }}"><select class="form-select" name="status">@foreach(['pending','in_progress','completed','cancelled'] as $status)<option @selected($milestone->status===$status)>{{ $status }}</option>@endforeach</select><button class="btn btn-sm btn-outline-primary">Save</button></form></details></td></tr>@empty<tr><td colspan="8" class="text-muted">No milestones.</td></tr>@endforelse</tbody></table></div></div></div>
