@extends('admin.layouts.layout')
@section('title', 'گزارشات بیاتو بات')

@section('content')
<div class="container-xxl flex-grow-1 container-p-y">

    <div class="d-flex align-items-center justify-content-between mb-4">
        <div>
            <h4 class="mb-1">گزارشات بیاتو بات</h4>
            <p class="text-muted mb-0 small">گفتگوهای کاربران و مهمان‌ها با چت‌بات</p>
        </div>
    </div>

    @if(session('success'))
    <div class="alert alert-success alert-dismissible mb-4">
        {{ session('success') }}
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    @endif

    <div class="card mb-4">
        <div class="card-body py-3">
            <form method="GET" action="{{ route('admin.biata-bot.chats.index') }}">
                <div class="row g-2 align-items-center">
                    <div class="col-md-4">
                        <input type="text" name="q" class="form-control"
                               placeholder="نام، موبایل یا session#..."
                               value="{{ request('q') }}">
                    </div>
                    <div class="col-md-3">
                        <select name="type" class="form-select">
                            <option value="">همه گفتگوها</option>
                            <option value="user" {{ request('type') === 'user' ? 'selected' : '' }}>کاربران لاگین‌شده</option>
                            <option value="guest" {{ request('type') === 'guest' ? 'selected' : '' }}>مهمان (session)</option>
                        </select>
                    </div>
                    <div class="col-auto d-flex gap-2">
                        <button type="submit" class="btn btn-primary"><i class="ti ti-search ti-xs"></i></button>
                        @if(request()->hasAny(['q', 'type']))
                        <a href="{{ route('admin.biata-bot.chats.index') }}" class="btn btn-outline-secondary">پاک کردن</a>
                        @endif
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div class="card">
        <div class="card-body p-0">
            @if($conversations->isEmpty())
            <div class="text-center py-5 text-muted">
                <i class="ti ti-messages-off ti-lg mb-2 d-block"></i>
                گفتگویی یافت نشد.
            </div>
            @else
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>شناسه</th>
                            <th>کاربر / نشست</th>
                            <th>نوع</th>
                            <th>تعداد پیام</th>
                            <th>آخرین پیام</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($conversations as $conv)
                        <tr>
                            <td class="text-muted small" dir="ltr">#{{ $conv->id }}</td>
                            <td>
                                <div class="fw-medium">{{ $conv->displayTitle() }}</div>
                                @if($conv->displaySubtitle())
                                <div class="text-muted small" dir="ltr">{{ $conv->displaySubtitle() }}</div>
                                @endif
                            </td>
                            <td>
                                @if($conv->user_id)
                                <span class="badge bg-label-primary">کاربر</span>
                                @else
                                <span class="badge bg-label-secondary">مهمان</span>
                                @endif
                            </td>
                            <td>{{ number_format($conv->messages_count) }}</td>
                            <td class="text-muted small">{{ $conv->shamsiLastMessage() }}</td>
                            <td>
                                <a href="{{ route('admin.biata-bot.chats.show', $conv) }}"
                                   class="btn btn-sm btn-outline-primary">
                                    <i class="ti ti-eye ti-xs me-1"></i>مشاهده
                                </a>
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
            @if($conversations->hasPages())
            <div class="d-flex justify-content-center py-3">
                {{ $conversations->links() }}
            </div>
            @endif
            @endif
        </div>
    </div>

</div>
@endsection
