@extends('layouts.app')

@section('title', ($lead->displayFairName() ?: 'Lead') . ' — Detail')

@section('content')
    <div class="portal-page-header d-flex justify-content-between align-items-start flex-wrap gap-2">
        <div>
            <h1 class="h4 mb-0">{{ $lead->displayFairName() ?: 'Lead' }}</h1>
            <p class="subtitle mb-0">{{ $lead->company_name ?: 'No company name' }}</p>
            @if($lead->assignedTo)
                <p class="small text-muted mb-0 mt-1">
                    Marketing: <strong>{{ $lead->assignedTo->name }}</strong>
                    @if($lead->assigned_at) · {{ $lead->assigned_at->format('M d, Y') }} @endif
                </p>
            @endif
            @if($lead->designAssignedTo)
                <p class="small text-muted mb-0 mt-1">
                    Design: <strong>{{ $lead->designAssignedTo->name }}</strong>
                    @include('partials.design-task-status-badge', ['status' => $lead->clean_design_task_status])
                    @if($lead->design_due_date)
                        · Due {{ $lead->design_due_date->format('M d, Y') }}
                        @if($lead->isDesignOverdue()) <span class="text-danger fw-semibold">(overdue)</span> @endif
                    @endif
                </p>
            @endif
        </div>
        <div class="portal-actions">
            @if(can_edit_lead($lead))
                <a href="{{ route('leads.edit', $lead) }}" class="btn btn-portal-primary">Edit lead</a>
            @endif
            @if(can_access_module('leads.listing'))
                <a href="{{ route('leads.listing') }}" class="btn btn-portal-outline">Back to listing</a>
            @elseif(can_access_module('leads.my_tasks'))
                <a href="{{ route('leads.my-tasks') }}" class="btn btn-portal-outline">Back to my tasks</a>
            @elseif(can_access_module('leads.design_tasks'))
                <a href="{{ route('leads.design-tasks') }}" class="btn btn-portal-outline">Back to design tasks</a>
            @endif
        </div>
    </div>

    <div class="row g-4">
        <div class="col-lg-4">
            <div class="portal-card mb-4">
                <div class="card-header">Lead information</div>
                <div class="card-body">
                    <div class="portal-detail-grid">
                        <div class="portal-detail-item">
                            <div class="label">Fair name</div>
                            <div class="value">{{ $lead->displayFairName() ?: '—' }}</div>
                        </div>
                        <div class="portal-detail-item">
                            <div class="label">Company</div>
                            <div class="value">{{ $lead->company_name ?: '—' }}</div>
                        </div>
                        <div class="portal-detail-item">
                            <div class="label">Contact person</div>
                            <div class="value">{{ $lead->contact_person_name ?: '—' }}</div>
                        </div>
                        <div class="portal-detail-item">
                            <div class="label">Venue</div>
                            <div class="value">{{ $lead->displayVenue() }}</div>
                        </div>
                        <div class="portal-detail-item">
                            <div class="label">Country</div>
                            <div class="value">{{ optional(optional($lead->tradefair)->fairCountry)->name ?: $lead->country ?: '—' }}</div>
                        </div>
                        <div class="portal-detail-item">
                            <div class="label">Fair start</div>
                            <div class="value">{{ optional($lead->effectiveFairStartDate())->format('M d, Y') ?: '—' }}</div>
                        </div>
                        <div class="portal-detail-item">
                            <div class="label">Fair end</div>
                            <div class="value">{{ optional($lead->effectiveFairEndDate())->format('M d, Y') ?: '—' }}</div>
                        </div>
                        <div class="portal-detail-item">
                            <div class="label">Source location</div>
                            <div class="value">{{ $lead->source_location ?: '—' }}</div>
                        </div>
                        <div class="portal-detail-item">
                            <div class="label">Budget</div>
                            <div class="value">{{ $lead->lead_budget !== null ? number_format($lead->lead_budget, 2) : '—' }}</div>
                        </div>
                        <div class="portal-detail-item">
                            <div class="label">Booth area</div>
                            <div class="value">
                                @if($lead->booth_area_sqm !== null)
                                    {{ number_format($lead->booth_area_sqm, 2) }} sqm
                                @else
                                    —
                                @endif
                                @if($lead->booth_area_sqft !== null)
                                    <span class="text-muted"> / {{ number_format($lead->booth_area_sqft, 2) }} sqft</span>
                                @endif
                            </div>
                        </div>
                        <div class="portal-detail-item">
                            <div class="label">Email</div>
                            <div class="value">
                                @if($lead->email_id)
                                    <a href="mailto:{{ $lead->email_id }}">{{ $lead->email_id }}</a>
                                @else
                                    —
                                @endif
                            </div>
                        </div>
                        <div class="portal-detail-item">
                            <div class="label">Phone</div>
                            <div class="value">
                                {{ $lead->phone_number ?: '—' }}
                                @if($lead->phone_number)
                                    <div class="portal-actions mt-2">
                                        <a class="btn btn-sm btn-portal-outline" href="tel:{{ $lead->phone_number }}">Call</a>
                                        @if($lead->whatsappPhoneNumber())
                                            <a class="btn btn-sm btn-portal-outline" target="_blank" rel="noopener" href="https://wa.me/{{ $lead->whatsappPhoneNumber() }}">WhatsApp</a>
                                        @endif
                                    </div>
                                @endif
                            </div>
                        </div>
                    </div>

                    @if($lead->description)
                        <div class="mt-3 pt-3 border-top">
                            <div class="portal-section-title">Description</div>
                            <p class="mb-0 small">{{ $lead->description }}</p>
                        </div>
                    @endif

                    <div class="mt-3 pt-3 border-top">
                        @if(can_access_module('leads.status'))
                            <form method="POST" action="{{ route('leads.status.update', $lead) }}" class="row g-2">
                                @csrf
                                @method('PATCH')
                                <div class="col-12">
                                    <label class="form-label small fw-semibold">Lead status</label>
                                    <select name="lead_status" class="form-select form-select-sm">
                                        @foreach($statuses as $status)
                                            <option value="{{ $status }}" {{ $lead->clean_lead_status === $status ? 'selected' : '' }}>{{ $status }}</option>
                                        @endforeach
                                    </select>
                                </div>
                                <div class="col-12">
                                    <button class="btn btn-portal-primary btn-sm w-100">Update status</button>
                                </div>
                            </form>
                        @else
                            <div class="label">Status</div>
                            <div class="mt-1">@include('partials.lead-status-badge', ['status' => $lead->clean_lead_status])</div>
                        @endif
                    </div>
                </div>
            </div>

            @if($lead->designAssignedTo && $lead->designRemarks->isNotEmpty())
            <div class="portal-card mb-4">
                <div class="card-header">Design tracking remarks</div>
                <div class="card-body">
                    @foreach($lead->designRemarks->take(5) as $remark)
                        <div class="border rounded p-2 mb-2" style="border-color: var(--portal-border) !important;">
                            <div class="small text-muted">
                                {{ optional($remark->user)->name }} · {{ optional($remark->created_at)->format('M d, Y H:i') }}
                            </div>
                            <div class="small">{{ $remark->remark }}</div>
                        </div>
                    @endforeach
                    <a href="{{ route('leads.design-tasks') }}" class="small">View on design tracking →</a>
                </div>
            </div>
            @endif

            @if(can_access_module('leads.remarks'))
            <div class="portal-card mb-4">
                <div class="card-header">Remarks</div>
                <div class="card-body">
                    <form method="POST" action="{{ route('leads.remarks.store', $lead) }}" class="mb-3">
                        @csrf
                        <textarea name="remark" class="form-control form-control-sm" rows="3" required placeholder="Add client discussion notes..."></textarea>
                        <button class="btn btn-portal-primary btn-sm mt-2">Save remark</button>
                    </form>
                    @forelse($lead->remarks as $remark)
                        <div class="border rounded p-2 mb-2" style="border-color: var(--portal-border) !important;">
                            <div class="small text-muted">
                                {{ optional($remark->user)->name ?: 'Unknown' }} · {{ optional($remark->created_at)->format('M d, Y H:i') }}
                            </div>
                            <div class="small">{{ $remark->remark }}</div>
                        </div>
                    @empty
                        <p class="text-muted small mb-0">No remarks yet.</p>
                    @endforelse
                </div>
            </div>
            @endif

            @if(can_manage_vendor_quotes($lead))
            <div class="portal-card mb-4">
                <div class="card-header">Add vendor quote</div>
                <div class="card-body">
                    <form method="POST" action="{{ route('vendor-quotes.store', $lead) }}" class="row g-2">
                        @csrf
                        <div class="col-12">
                            <label class="form-label small">Vendor name</label>
                            <input name="vendor_name" class="form-control form-control-sm" required>
                        </div>
                        <div class="col-12">
                            <label class="form-label small">Raw cost</label>
                            <input type="number" step="0.01" name="vendor_raw_cost" class="form-control form-control-sm" required>
                        </div>
                        <div class="col-12">
                            <label class="form-label small">Markup %</label>
                            <input type="number" step="0.01" name="our_markup_percent" class="form-control form-control-sm" required>
                        </div>
                        <div class="col-12 form-check ms-1">
                            <input class="form-check-input" type="checkbox" value="1" name="selection_toggle" id="selected-new">
                            <label class="form-check-label small" for="selected-new">Select as chosen vendor</label>
                        </div>
                        <div class="col-12">
                            <button class="btn btn-portal-primary btn-sm w-100">Add quote</button>
                        </div>
                    </form>
                </div>
            </div>
            @endif

            @if(auth()->user()->isPlatformAdmin())
            <div class="portal-card">
                <div class="card-header">View tracking</div>
                <div class="portal-table-wrap">
                    <table class="table portal-table table-sm mb-0">
                        <thead>
                        <tr>
                            <th>User</th>
                            <th>Opens</th>
                            <th>Last</th>
                        </tr>
                        </thead>
                        <tbody>
                        @forelse($lead->leadViews as $view)
                            <tr>
                                <td>{{ optional($view->user)->name ?: '—' }}</td>
                                <td>{{ $view->view_count }}</td>
                                <td>{{ optional($view->last_viewed_at)->format('M d, H:i') ?: '—' }}</td>
                            </tr>
                        @empty
                            <tr><td colspan="3" class="text-center text-muted py-3 small">No views yet.</td></tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
            @endif
        </div>

        <div class="col-lg-8">
            <div class="portal-card">
                <div class="card-header">Vendor comparison</div>
                <div class="portal-table-wrap">
                    <table class="table portal-table mb-0">
                        <thead>
                        <tr>
                            <th>Vendor</th>
                            <th>Raw cost</th>
                            <th>Markup %</th>
                            <th>Client price</th>
                            <th>Profit</th>
                            <th>Selected</th>
                            @if(can_manage_vendor_quotes($lead))
                                <th></th>
                            @endif
                        </tr>
                        </thead>
                        <tbody>
                        @forelse($lead->vendorQuotes as $quote)
                            @php
                                $final = $quote->client_final_price;
                                $profit = $final - $quote->vendor_raw_cost;
                            @endphp
                            <tr>
                                @if(can_manage_vendor_quotes($lead))
                                    <form method="POST" action="{{ route('vendor-quotes.update', [$lead, $quote]) }}">
                                        @csrf
                                        @method('PATCH')
                                        <td><input name="vendor_name" value="{{ $quote->vendor_name }}" class="form-control form-control-sm" required></td>
                                        <td><input type="number" step="0.01" name="vendor_raw_cost" value="{{ $quote->vendor_raw_cost }}" class="form-control form-control-sm" required></td>
                                        <td><input type="number" step="0.01" name="our_markup_percent" value="{{ $quote->our_markup_percent }}" class="form-control form-control-sm" required></td>
                                        <td><strong>{{ number_format($final, 2) }}</strong></td>
                                        <td>{{ number_format($profit, 2) }}</td>
                                        <td class="text-center">
                                            <input type="checkbox" name="selection_toggle" value="1" {{ $quote->selection_toggle ? 'checked' : '' }}>
                                        </td>
                                        <td><button class="btn btn-portal-primary btn-sm">Save</button></td>
                                    </form>
                                @else
                                    <td>{{ $quote->vendor_name }}</td>
                                    <td>{{ number_format($quote->vendor_raw_cost, 2) }}</td>
                                    <td>{{ number_format($quote->our_markup_percent, 2) }}</td>
                                    <td><strong>{{ number_format($final, 2) }}</strong></td>
                                    <td>{{ number_format($profit, 2) }}</td>
                                    <td>{{ $quote->selection_toggle ? 'Yes' : 'No' }}</td>
                                @endif
                            </tr>
                        @empty
                            <tr><td colspan="7"><div class="portal-empty">Add vendor quotes to start comparison.</div></td></tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>
            </div>

            <div class="alert border-0 mt-4 mb-0 rounded-3" style="background: var(--portal-primary-soft); color: var(--portal-primary);">
                <strong>Workflow:</strong> Costing → add 3 vendor quotes → compare prices → select vendor → set status to <strong>Won/Ongoing</strong>.
            </div>
        </div>
    </div>
@endsection
