@extends('layouts.admin')
@section('title', 'Kotak Masuk')

@section('content')
<style>
    /* Styling Khusus agar sama dengan Show Blade */
    .chat-card {
        height: 80vh; 
        overflow: hidden;
    }
    
    .chat-sidebar {
        border-right: 1px solid #dee2e6;
        height: 100%;
        display: flex;
        flex-direction: column;
    }

    .chat-list-container {
        flex-grow: 1;
        overflow-y: auto;
    }

    /* Scrollbar Tipis */
    ::-webkit-scrollbar { width: 6px; }
    ::-webkit-scrollbar-track { background: #f1f1f1; }
    ::-webkit-scrollbar-thumb { background: #ccc; border-radius: 3px; }
    ::-webkit-scrollbar-thumb:hover { background: #aaa; }
</style>

<section class="section">
    <div class="card chat-card shadow-sm">
        <div class="row g-0 h-100">
            
            {{-- DAFTAR CHAT --}}
            <div class="col-md-4 chat-sidebar">
                <div class="p-4 border-bottom bg-white sticky-top">
                    <h5 class="mb-3 text-success"><i class="bi bi-chat-left-text-fill"></i> Percakapan</h5>
                    <div class="form-group position-relative has-icon-right mb-0">
                        <input type="text" class="form-control" placeholder="Cari pembeli...">
                        <div class="form-control-icon">
                            <i class="bi bi-search"></i>
                        </div>
                    </div>
                </div>

                <div class="list-group list-group-flush chat-list-container">
                    @forelse($chatList as $chat)
                        <a href="{{ route('petani.pesan.show', $chat['lawan_id']) }}" class="list-group-item list-group-item-action chat-item p-3">
                            <div class="d-flex align-items-center">
                                
                                <div class="avatar avatar-lg me-3">
                                    {{-- Avatar Inisial  --}}
                                    <span class="avatar-content rounded-circle d-flex align-items-center justify-content-center text-white font-bold"
                                          style="width: 48px; height: 48px; background-color: #81c408; font-size: 1.2rem;">
                                        {{ substr($chat['nama'], 0, 1) }}
                                    </span>
                                    @if($chat['unread'] > 0)
                                        <span class="avatar-status bg-danger"></span>
                                    @endif
                                </div>

                                <div class="flex-grow-1 text-truncate">
                                    <div class="d-flex justify-content-between align-items-center mb-1">
                                        <h6 class="mb-0 text-dark fw-bold">{{ $chat['nama'] }}</h6>
                                        <small class="text-muted" style="font-size: 0.75rem">{{ $chat['time'] }}</small>
                                    </div>
                                    <p class="mb-0 small text-muted text-truncate">
                                        {{ Str::limit($chat['last_message'], 35) }}
                                    </p>
                                </div>
                            </div>
                        </a>
                    @empty
                        <div class="text-center p-5 mt-4">
                            <i class="bi bi-chat-square-text text-muted" style="font-size: 3rem; opacity: 0.5;"></i>
                            <p class="text-muted mt-3">Belum ada pesan masuk.</p>
                        </div>
                    @endforelse
                </div>
            </div>

            <div class="col-md-8 d-none d-md-flex align-items-center justify-content-center bg-light flex-column border-start">
                <div class="text-center opacity-75">
                    <div class="mb-4">
                        <div class="bg-white p-4 rounded-circle shadow-sm d-inline-block">
                            <i class="bi bi-chat-dots-fill text-success" style="font-size: 4rem;"></i>
                        </div>
                    </div>
                    <h4 class="text-dark fw-bold">Selamat Datang di Pesan!</h4>
                    <p class="text-secondary mt-2">
                        Pilih salah satu percakapan di sebelah kiri<br>untuk melihat detail pesan dan membalasnya.
                    </p>
                </div>
            </div>
            
        </div>
    </div>
</section>
@endsection