@extends('layouts.admin')

@section('title', 'CRM')

@section('content')
<div class="fundi-page-header">
    <div>
        <h1>CRM</h1>
        <p class="fundi-page-header__desc">Leads pipeline and customer interactions.</p>
    </div>
</div>

<div class="fundi-grid-2">
    <div class="fundi-card">
        <div class="fundi-card__header"><h3>New Lead</h3></div>
        <div class="fundi-card__body fundi-card__body--padded">
            <form method="POST" action="{{ route('admin.crm.leads.store') }}">
                @csrf
                <div class="fundi-form-grid">
                    <div class="fundi-form-group">
                        <label for="full_name">Full Name *</label>
                        <input type="text" id="full_name" name="full_name" class="fundi-input" value="{{ old('full_name') }}" required>
                    </div>
                    <div class="fundi-form-group">
                        <label for="business_name">Business Name</label>
                        <input type="text" id="business_name" name="business_name" class="fundi-input" value="{{ old('business_name') }}">
                    </div>
                    <div class="fundi-form-group">
                        <label for="phone">Phone *</label>
                        <input type="text" id="phone" name="phone" class="fundi-input" value="{{ old('phone') }}" required>
                    </div>
                    <div class="fundi-form-group">
                        <label for="email">Email</label>
                        <input type="email" id="email" name="email" class="fundi-input" value="{{ old('email') }}">
                    </div>
                    <div class="fundi-form-group">
                        <label for="source">Source</label>
                        <input type="text" id="source" name="source" class="fundi-input" value="{{ old('source') }}" placeholder="Referral, walk-in, web">
                    </div>
                    <div class="fundi-form-group">
                        <label for="interest">Interest</label>
                        <input type="text" id="interest" name="interest" class="fundi-input" value="{{ old('interest') }}" placeholder="Loan, investment">
                    </div>
                    <div class="fundi-form-group">
                        <label for="estimated_amount">Est. Amount (₦)</label>
                        <input type="number" step="0.01" min="0" id="estimated_amount" name="estimated_amount" class="fundi-input" value="{{ old('estimated_amount') }}">
                    </div>
                    <div class="fundi-form-group">
                        <label for="assigned_to">Assigned To</label>
                        <select id="assigned_to" name="assigned_to" class="fundi-select">
                            <option value="">— Select —</option>
                            @foreach($officers as $officer)
                                <option value="{{ $officer->id }}" @selected(old('assigned_to') == $officer->id)>{{ $officer->name }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="fundi-form-group">
                    <label for="notes">Notes</label>
                    <textarea id="notes" name="notes" class="fundi-textarea">{{ old('notes') }}</textarea>
                </div>
                <button type="submit" class="fundi-btn fundi-btn--primary">Create Lead</button>
            </form>
        </div>
    </div>

    <div class="fundi-card">
        <div class="fundi-card__header"><h3>Log Interaction</h3></div>
        <div class="fundi-card__body fundi-card__body--padded">
            <form method="POST" action="{{ route('admin.crm.interactions.store') }}">
                @csrf
                <div class="fundi-form-group">
                    <label for="interaction_customer_id">Customer</label>
                    <select id="interaction_customer_id" name="customer_id" class="fundi-select">
                        <option value="">— Select customer —</option>
                        @foreach($customers as $c)
                            <option value="{{ $c->id }}" @selected(old('customer_id') == $c->id)>{{ $c->customer_no }} — {{ $c->display_name }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="fundi-form-group">
                    <label for="lead_id">Or Lead</label>
                    <select id="lead_id" name="lead_id" class="fundi-select">
                        <option value="">— Select lead —</option>
                        @foreach($leads as $lead)
                            <option value="{{ $lead->id }}" @selected(old('lead_id') == $lead->id)>{{ $lead->full_name }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="fundi-form-grid">
                    <div class="fundi-form-group">
                        <label for="channel">Channel *</label>
                        <select id="channel" name="channel" class="fundi-select" required>
                            @foreach(['call','email','visit','whatsapp','meeting','other'] as $ch)
                                <option value="{{ $ch }}" @selected(old('channel') === $ch)>{{ ucfirst($ch) }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="fundi-form-group">
                        <label for="next_follow_up">Next Follow-up</label>
                        <input type="date" id="next_follow_up" name="next_follow_up" class="fundi-input" value="{{ old('next_follow_up') }}">
                    </div>
                </div>
                <div class="fundi-form-group">
                    <label for="subject">Subject *</label>
                    <input type="text" id="subject" name="subject" class="fundi-input" value="{{ old('subject') }}" required>
                </div>
                <div class="fundi-form-group">
                    <label for="interaction_notes">Notes</label>
                    <textarea id="interaction_notes" name="notes" class="fundi-textarea">{{ old('notes') }}</textarea>
                </div>
                <button type="submit" class="fundi-btn fundi-btn--primary">Log Interaction</button>
            </form>
        </div>
    </div>
</div>

<form method="GET" class="fundi-filters">
    <div class="fundi-form-group">
        <label for="search">Search</label>
        <input type="text" id="search" name="search" class="fundi-input" value="{{ request('search') }}">
    </div>
    <div class="fundi-form-group">
        <label for="stage">Stage</label>
        <select id="stage" name="stage" class="fundi-select">
            <option value="">All stages</option>
            @foreach(['new','contacted','qualified','proposal','won','lost'] as $st)
                <option value="{{ $st }}" @selected(request('stage') === $st)>{{ ucfirst($st) }}</option>
            @endforeach
        </select>
    </div>
    <button type="submit" class="fundi-btn fundi-btn--secondary">Filter</button>
</form>

<div class="fundi-card">
    <div class="fundi-card__header"><h3>Leads Pipeline</h3></div>
    <div class="fundi-card__body">
        <div class="fundi-table-wrap">
            <table class="fundi-table">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Phone</th>
                        <th>Interest</th>
                        <th class="num">Est. Amount</th>
                        <th>Stage</th>
                        <th>Assigned</th>
                        <th>Update Stage</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($leads as $lead)
                        <tr>
                            <td>
                                <strong>{{ $lead->full_name }}</strong>
                                @if($lead->business_name)<br><span class="text-muted">{{ $lead->business_name }}</span>@endif
                            </td>
                            <td>{{ $lead->phone }}</td>
                            <td>{{ $lead->interest ?? '—' }}</td>
                            <td class="num">{{ $lead->estimated_amount ? money($lead->estimated_amount) : '—' }}</td>
                            <td><x-status-badge :status="$lead->stage" /></td>
                            <td>{{ $lead->assignedTo->name ?? '—' }}</td>
                            <td>
                                <form method="POST" action="{{ route('admin.crm.leads.stage', $lead) }}" class="fundi-inline-form">
                                    @csrf
                                    @method('PATCH')
                                    <select name="stage" class="fundi-select" style="min-width:120px;">
                                        @foreach(['new','contacted','qualified','proposal','won','lost'] as $st)
                                            <option value="{{ $st }}" @selected($lead->stage === $st)>{{ ucfirst($st) }}</option>
                                        @endforeach
                                    </select>
                                    <button type="submit" class="fundi-btn fundi-btn--secondary fundi-btn--sm">Update</button>
                                </form>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="7" class="fundi-empty">No leads found.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($leads->hasPages())
            <div class="fundi-pagination">{{ $leads->links() }}</div>
        @endif
    </div>
</div>
@endsection
