{{--
    Вьюха: Список заявок с веб-форм
    Создана: 2026-05-07
    Контроллер: Admin\LeadController::index()
    Переменные: $leads (пагинация), $forms (все формы для фильтра), $formId (текущий фильтр)
--}}
@extends('admin.layout')

@section('title', 'Заявки')

@section('content_header')
    <div class="d-flex justify-content-between align-items-center">
        <h1 class="m-0">Заявки</h1>
        <span class="badge badge-secondary" style="font-size:14px">{{ $leads->total() }}</span>
    </div>
@stop

@section('breadcrumb')
    <li class="breadcrumb-item"><a href="{{ route('admin.dashboard') }}">Главная</a></li>
    <li class="breadcrumb-item active">Заявки</li>
@stop

@section('content')
    @if(session('success'))
        <div class="alert alert-success alert-dismissible">
            <button type="button" class="close" data-dismiss="alert">&times;</button>
            {{ session('success') }}
        </div>
    @endif

    {{-- Фильтр по форме --}}
    <div class="card card-secondary card-outline mb-3">
        <div class="card-body py-2">
            <form method="GET" class="d-flex align-items-center gap-2" style="gap:10px">
                <label class="mb-0 mr-2 text-muted small">Форма:</label>
                <select name="form_id" class="form-control form-control-sm" style="width:auto" onchange="this.form.submit()">
                    <option value="">Все формы</option>
                    @foreach($forms as $f)
                        <option value="{{ $f->id }}" {{ $formId == $f->id ? 'selected' : '' }}>{{ $f->title }}</option>
                    @endforeach
                </select>
                @if($formId)
                    <a href="{{ route('admin.leads.index') }}" class="btn btn-xs btn-default ml-2">Сбросить</a>
                @endif
            </form>
        </div>
    </div>

    <div class="card card-primary card-outline">
        <div class="card-body p-0">
            <table class="table table-hover mb-0">
                <thead>
                    <tr>
                        <th style="width:40px">#</th>
                        <th style="width:160px">Дата</th>
                        <th>Форма</th>
                        <th>Краткое содержание</th>
                        <th style="width:80px">IP</th>
                        <th style="width:80px">Статус</th>
                        <th style="width:80px"></th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($leads as $lead)
                        <tr class="{{ $lead->read_at ? '' : 'font-weight-bold' }}">
                            <td class="text-muted">{{ $lead->id }}</td>
                            <td class="text-muted small">{{ $lead->created_at->format('d.m.Y H:i') }}</td>
                            <td>
                                <span class="badge badge-info">{{ $lead->form->title ?? '—' }}</span>
                            </td>
                            <td>
                                {{-- Показываем первые 2 поля заявки --}}
                                @php $preview = collect($lead->data)->take(2) @endphp
                                @foreach($preview as $k => $v)
                                    <span class="text-muted small">{{ $k }}:</span> {{ Str::limit($v, 40) }}
                                    @if(!$loop->last) &nbsp;·&nbsp; @endif
                                @endforeach
                            </td>
                            <td class="text-muted small">{{ $lead->ip }}</td>
                            <td>
                                @if($lead->read_at)
                                    <span class="badge badge-secondary">Просм.</span>
                                @else
                                    <span class="badge badge-danger">Новая</span>
                                @endif
                            </td>
                            <td class="text-right">
                                <a href="{{ route('admin.leads.show', $lead) }}" class="btn btn-xs btn-default" title="Просмотр">
                                    <i class="fas fa-eye"></i>
                                </a>
                                <form action="{{ route('admin.leads.destroy', $lead) }}" method="POST" class="d-inline"
                                      onsubmit="return confirm('Удалить заявку #{{ $lead->id }}?')">
                                    @csrf @method('DELETE')
                                    <button type="submit" class="btn btn-xs btn-danger ml-1" title="Удалить">
                                        <i class="fas fa-trash"></i>
                                    </button>
                                </form>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="text-center text-muted py-4">Заявок пока нет.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($leads->hasPages())
            <div class="card-footer">
                {{ $leads->links() }}
            </div>
        @endif
    </div>
@stop
