@extends('layouts.app')
@section('title', $project->name.' | Project')
@section('content')
<main class="nxl-container"><div class="nxl-content"><div class="page-header d-flex justify-content-between"><div><h5>{{ $project->name }}</h5><small>{{ $project->deal?->name }} · {{ $project->account?->account_name }}</small></div><a class="btn btn-light" href="{{ route('projects') }}">All Projects</a></div><div class="main-content">
@if(session('success'))<div class="alert alert-success">{{ session('success') }}</div>@endif @if($errors->any())<div class="alert alert-danger">{{ $errors->first() }}</div>@endif
<ul class="nav nav-tabs mb-3"><li class="nav-item"><a class="nav-link active" data-bs-toggle="tab" href="#overview">Overview</a></li><li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#milestones">Milestones</a></li><li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#tasks">Tasks</a></li><li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#products">Products</a></li><li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#files">Files</a></li><li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#meetings">Meetings</a></li><li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#team">Team & Reporting</a></li><li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#timeline">Timeline</a></li></ul>
<div class="tab-content">
<div class="tab-pane fade show active" id="overview"><div class="card"><div class="card-body"><form method="POST" action="{{ route('projects.update',$project) }}" class="row g-3">@csrf @method('PUT')
 <div class="col-md-8"><label class="form-label">Name</label><input class="form-control" name="name" value="{{ $project->name }}" required></div><div class="col-md-4"><label class="form-label">Status</label><select class="form-select" name="status">@foreach(['planned','active','on_hold','completed','cancelled'] as $s)<option value="{{ $s }}" @selected($project->status===$s)>{{ ucwords(str_replace('_',' ',$s)) }}</option>@endforeach</select></div>
 <div class="col-12"><label class="form-label">Description</label><textarea class="form-control" name="description">{{ $project->description }}</textarea></div>
 <input type="hidden" name="deal_id" value="{{ $project->deal_id }}"><input type="hidden" name="account_id" value="{{ $project->account_id }}"><input type="hidden" name="owner_id" value="{{ $project->owner_id }}">
 <div class="col-md-3"><label class="form-label">Start</label><input type="date" class="form-control" name="start_date" value="{{ $project->start_date?->format('Y-m-d') }}"></div><div class="col-md-3"><label class="form-label">Due</label><input type="date" class="form-control" name="due_date" value="{{ $project->due_date?->format('Y-m-d') }}"></div><div class="col-md-3"><label class="form-label">Progress</label><input type="number" min="0" max="100" class="form-control" name="progress" value="{{ $project->progress }}"></div><div class="col-md-3"><label class="form-label">Status reason</label><input class="form-control" name="status_reason"></div>
 <div class="col-md-3"><label class="form-label">Project manager ID</label><input type="number" class="form-control" name="project_manager_id" value="{{ $project->project_manager_id }}"></div><div class="col-md-3"><label class="form-label">Project type</label><input class="form-control" name="project_type" value="{{ $project->project_type }}"></div><div class="col-md-3"><label class="form-label">Priority</label><select class="form-select" name="priority"><option value="">Select</option>@foreach(['low','medium','high','urgent'] as $priority)<option value="{{ $priority }}" @selected($project->priority===$priority)>{{ ucfirst($priority) }}</option>@endforeach</select></div><div class="col-md-3"><label class="form-label">Actual end</label><input type="date" class="form-control" name="actual_end_date" value="{{ $project->actual_end_date?->format('Y-m-d') }}"></div>
 <div class="col-md-4"><label class="form-label">Budget</label><input type="number" step=".01" min="0" class="form-control" name="budget" value="{{ $project->budget }}"></div><div class="col-md-4"><label class="form-label">Contract value</label><input type="number" step=".01" min="0" class="form-control" name="contract_value" value="{{ $project->contract_value }}"></div><div class="col-md-4"><label class="form-label">Billing type</label><input class="form-control" name="billing_type" value="{{ $project->billing_type }}"></div>
 <div class="col-md-6"><label class="form-label">Technology stack</label><textarea class="form-control" name="technology_stack">{{ $project->technology_stack }}</textarea></div><div class="col-md-6"><label class="form-label">Scope of work</label><textarea class="form-control" name="scope_of_work">{{ $project->scope_of_work }}</textarea></div><div class="col-md-6"><label class="form-label">Requirements</label><textarea class="form-control" name="requirements">{{ $project->requirements }}</textarea></div><div class="col-md-6"><label class="form-label">Deliverables</label><textarea class="form-control" name="deliverables">{{ $project->deliverables }}</textarea></div><div class="col-12"><label class="form-label">Risks</label><textarea class="form-control" name="risks">{{ $project->risks }}</textarea></div>
 <div class="col-12"><button class="btn btn-primary">Update Project</button></div></form></div></div></div>

<div class="tab-pane fade" id="milestones"><div class="card"><div class="card-header">Milestone CRUD</div><div class="card-body">
 <form method="POST" action="{{ route('projects.milestones.store',$project) }}" class="row g-2 mb-4">@csrf <div class="col"><input class="form-control" name="name" placeholder="Milestone name" required></div><div class="col"><input class="form-control" name="description" placeholder="Description"></div><div class="col-2"><input type="date" class="form-control" name="due_date"></div><div class="col-2"><select class="form-select" name="status">@foreach(['pending','in_progress','completed','cancelled'] as $s)<option>{{ $s }}</option>@endforeach</select></div><div class="col-auto"><button class="btn btn-primary">Add</button></div></form>
 @forelse($project->milestones as $m)<form method="POST" action="{{ route('projects.milestones.update',[$project,$m]) }}" class="row g-2 border rounded p-2 mb-2">@csrf @method('PUT')<div class="col"><input class="form-control" name="name" value="{{ $m->name }}"></div><div class="col"><input class="form-control" name="description" value="{{ $m->description }}"></div><div class="col-2"><input type="date" class="form-control" name="due_date" value="{{ $m->due_date?->format('Y-m-d') }}"></div><div class="col-2"><select class="form-select" name="status">@foreach(['pending','in_progress','completed','cancelled'] as $s)<option @selected($m->status===$s)>{{ $s }}</option>@endforeach</select></div><div class="col-auto"><button class="btn btn-light">Save</button></div></form><form method="POST" action="{{ route('projects.milestones.destroy',[$project,$m]) }}" class="text-end mb-2">@csrf @method('DELETE')<button class="btn btn-sm btn-outline-danger">Delete {{ $m->name }}</button></form>@empty<div class="text-muted">No milestones.</div>@endforelse
</div></div></div>

<div class="tab-pane fade" id="tasks"><div class="card"><div class="card-header">Project Task CRUD</div><div class="card-body">
 <form method="POST" action="{{ route('projects.tasks.store',$project) }}" class="row g-2 mb-4">@csrf <div class="col"><input class="form-control" name="title" placeholder="Task title" required></div><div class="col-2"><select class="form-select" name="milestone_id"><option value="">No milestone</option>@foreach($project->milestones as $m)<option value="{{ $m->id }}">{{ $m->name }}</option>@endforeach</select></div><div class="col-2"><select class="form-select crm-remote-lookup" data-lookup-type="user" name="assigned_to"><option value="">Unassigned</option></select></div><div class="col-2"><select class="form-select" name="priority">@foreach(['low','medium','high','urgent'] as $s)<option>{{ $s }}</option>@endforeach</select></div><div class="col-2"><select class="form-select" name="status">@foreach(['pending','in_progress','completed','cancelled'] as $s)<option>{{ $s }}</option>@endforeach</select></div><div class="col-2"><input type="datetime-local" class="form-control" name="due_at"></div><div class="col-auto"><button class="btn btn-primary">Add</button></div><div class="col-12"><input class="form-control" name="description" placeholder="Description"></div></form>
 @forelse($project->tasks as $t)<form method="POST" action="{{ route('projects.tasks.update',[$project,$t]) }}" class="row g-2 border rounded p-2 mb-2">@csrf @method('PUT')<div class="col"><input class="form-control" name="title" value="{{ $t->title }}"><input type="hidden" name="description" value="{{ $t->description }}"></div><div class="col-2"><select class="form-select" name="milestone_id"><option value="">None</option>@foreach($project->milestones as $m)<option value="{{ $m->id }}" @selected($t->milestone_id===$m->id)>{{ $m->name }}</option>@endforeach</select></div><div class="col-2"><select class="form-select" name="assigned_to"><option value="">Unassigned</option>@foreach($users as $u)<option value="{{ $u->id }}" @selected($t->assigned_to===$u->id)>{{ $u->name }}</option>@endforeach</select></div><div class="col-2"><select class="form-select" name="priority">@foreach(['low','medium','high','urgent'] as $s)<option @selected($t->priority===$s)>{{ $s }}</option>@endforeach</select></div><div class="col-2"><select class="form-select" name="status">@foreach(['pending','in_progress','completed','cancelled'] as $s)<option @selected($t->status===$s)>{{ $s }}</option>@endforeach</select></div><input type="hidden" name="due_at" value="{{ $t->due_at?->format('Y-m-d H:i:s') }}"><div class="col-auto"><button class="btn btn-light">Save</button></div></form><form method="POST" action="{{ route('projects.tasks.destroy',[$project,$t]) }}" class="text-end mb-2">@csrf @method('DELETE')<button class="btn btn-sm btn-outline-danger">Delete {{ $t->title }}</button></form>@empty<div class="text-muted">No tasks.</div>@endforelse
</div></div></div>

<div class="tab-pane fade" id="products"><div class="card"><div class="card-body"><form method="POST" action="{{ route('projects.products.store',$project) }}" class="row g-2 mb-4">@csrf <div class="col"><select class="form-select crm-remote-lookup" data-lookup-type="product" name="product_id"></select></div><div class="col-2"><input type="number" step=".001" value="1" name="quantity" class="form-control"></div><div class="col"><input name="notes" class="form-control" placeholder="Usage notes"></div><div class="col-auto"><button class="btn btn-primary">Link Product</button></div></form>@foreach($project->products as $p)<div class="d-flex justify-content-between border rounded p-3 mb-2"><span>{{ $p->product_name }} · Qty {{ $p->pivot->quantity }}<small class="d-block text-muted">{{ $p->pivot->notes }}</small></span><form method="POST" action="{{ route('projects.products.destroy',[$project,$p]) }}">@csrf @method('DELETE')<button class="btn btn-sm btn-outline-danger">Remove</button></form></div>@endforeach</div></div></div>

<div class="tab-pane fade" id="files"><div class="card"><div class="card-body"><x-crm-file-manager module="projects" :record="$project" /></div></div></div>

<div class="tab-pane fade" id="meetings"><div class="card"><div class="card-body"><form method="POST" action="{{ route('projects.meetings.store',$project) }}" class="row g-2 mb-4">@csrf <div class="col"><input class="form-control" name="title" placeholder="Meeting title" required></div><div class="col-2"><select class="form-select" name="meeting_type">@foreach(['online','physical','phone'] as $s)<option>{{ $s }}</option>@endforeach</select></div><div class="col"><input class="form-control" name="location" placeholder="Link / location"></div><div class="col-2"><input type="date" name="start_date" class="form-control" required></div><div class="col-2"><input type="time" name="start_time" class="form-control" required></div><div class="col-12"><textarea class="form-control" name="agenda" placeholder="Agenda"></textarea><select class="form-select mt-2" name="attendee_ids[]" multiple>@foreach($users as $u)<option value="{{ $u->id }}">{{ $u->name }}</option>@endforeach</select></div><div class="col-auto"><button class="btn btn-primary">Schedule</button></div></form>@forelse($project->meetings as $m)<div class="border rounded p-3 mb-2"><strong>{{ $m->title }}</strong><span class="float-end">{{ $m->start_date }} {{ $m->start_time }}</span><small class="d-block">{{ $m->location }} · {{ $m->attendees->pluck('name')->join(', ') }}</small></div>@empty<div class="text-muted">No Project meetings.</div>@endforelse</div></div></div>

<div class="tab-pane fade" id="team"><div class="card"><div class="card-body"><form method="POST" action="{{ route('projects.assign',$project) }}" class="row g-2 mb-4">@csrf <div class="col"><select class="form-select crm-remote-lookup" data-lookup-type="user" name="assignee_ids[]" multiple>@foreach($project->assignees as $u)<option value="{{ $u->id }}" selected>{{ $u->name }}</option>@endforeach</select></div><div class="col-auto"><button class="btn btn-primary">Update Team</button></div></form><table class="table"><tr><th>Employee</th><th>Manager</th><th>Tasks</th><th>Completed</th><th>Overdue</th></tr>@foreach($employeeReport as $r)<tr><td>{{ $r['user']?->name ?: 'Unassigned' }}</td><td>{{ $r['user']?->reportingManager?->name }}</td><td>{{ $r['total'] }}</td><td>{{ $r['completed'] }}</td><td>{{ $r['overdue'] }}</td></tr>@endforeach</table></div></div></div>

<div class="tab-pane fade" id="timeline"><div class="card"><div class="card-body">@forelse($project->statusHistory as $h)<div class="border-start border-primary ps-3 pb-4"><strong>{{ ucwords(str_replace('_',' ',$h->from_status ?: 'Created')) }} → {{ ucwords(str_replace('_',' ',$h->to_status)) }}</strong><small class="d-block text-muted">{{ $h->changed_at?->format('M d, Y H:i') }} by {{ $h->changedBy?->name }}</small><div>{{ $h->reason }}</div></div>@empty<div class="text-muted">No status history.</div>@endforelse</div></div></div>
</div><x-project-milestone-plan :project="$project" /><x-project-risks-issues :project="$project" :users="$users" /><x-project-corrective-actions :project="$project" /><x-crm-related-workflows module="projects" :record="$project" /><x-unified-operational-timeline module="projects" :record="$project" /></div></div></main>
@endsection
