@extends('layouts.app')

@section('title', $lead->name)
@section('heading', $lead->name)

@section('content')
    <div class="flex-between mb-1">
        <div class="flex gap" style="flex-wrap:wrap">
            <a href="{{ route('leads.edit', $lead) }}" class="btn btn-ghost btn-sm">Edit</a>
            <a href="{{ route('pipeline.index') }}" class="btn btn-ghost btn-sm">Pipeline</a>
            @can('create', App\Models\Lead::class)
                <form method="POST" action="{{ route('leads.duplicate', $lead) }}" class="mb-0">
                    @csrf
                    <button type="submit" class="btn btn-ghost btn-sm">Duplicate</button>
                </form>
            @endcan
        </div>
        @if (! $lead->customer)
            <form method="POST" action="{{ route('customers.from-lead', $lead) }}" class="flex gap" style="align-items:flex-end">
                @csrf
                <div class="form-group mb-0">
                    <label for="lifetime_value">Lifetime value (optional)</label>
                    <input id="lifetime_value" class="form-control" type="number" step="0.01" min="0" name="lifetime_value" placeholder="0.00" style="max-width:140px">
                </div>
                <button type="submit" class="btn btn-primary btn-sm">Convert to customer</button>
            </form>
        @else
            <a href="{{ route('customers.show', $lead->customer) }}" class="btn btn-primary btn-sm">View customer</a>
        @endif
    </div>

    <div class="card">
        <p class="mb-0" style="font-size:0.9rem;color:var(--muted)">
            <strong>Stage:</strong> {{ $lead->stage->name ?? '—' }}
            @if ($lead->assignee) · <strong>Agent:</strong> {{ $lead->assignee->name }} @endif
            @if ($lead->email) · {{ $lead->email }} @endif
            @if ($lead->phone) · {{ $lead->phone }} @endif
            @if ($lead->source) · <strong>Source:</strong> {{ $lead->source }} @endif
        </p>
        @if ($lead->notes)
            <pre class="mt-1 mb-0" style="white-space:pre-wrap;font:inherit;font-size:0.9rem">{{ $lead->notes }}</pre>
        @endif
    </div>

    <div class="card">
        <h2 class="card-title">Activity</h2>
        <div class="timeline">
            @forelse ($lead->activities as $act)
                <div class="timeline-item">
                    <div style="font-size:0.75rem;color:var(--muted)">{{ $act->created_at->diffForHumans() }} · {{ $act->type }}
                        @if ($act->user) · {{ $act->user->name }} @endif
                    </div>
                    @if ($act->description)
                        <div style="font-size:0.9rem">{{ $act->description }}</div>
                    @endif
                </div>
            @empty
                <p>No activity yet.</p>
            @endforelse
        </div>
    </div>

    <form method="POST" action="{{ route('leads.destroy', $lead) }}" onsubmit="return confirm('Delete this lead?');">
        @csrf
        @method('DELETE')
        <button type="submit" class="btn btn-danger btn-sm">Delete lead</button>
    </form>
@endsection
