@extends('layouts.frontend')
@section('title', 'Pesan Saya')

@section('css')
<style>
    /* Layout Utama Chat  */
    .chat-layout {
        height: 60vh; 
        background-color: #fff;
        border: 1px solid #dee2e6;
        border-radius: 12px;
        overflow: hidden;
    }
    /* --- Sidebar Kiri --- */
    .chat-sidebar {
        height: 100%;
        background-color: #fff;
        border-right: 1px solid #dee2e6; 
        display: flex;
        flex-direction: column;
    }

    /* Header Pencarian */
    .sidebar-header {
        padding: 15px;
        border-bottom: 1px solid #f0f0f0;
    }

    /* List User Scrollable */
    .chat-list-container {
        flex-grow: 1;
        overflow-y: auto;
    }

    /* Item Chat */
    .chat-item {
        transition: all 0.2s;
        border-bottom: 1px solid #f8f9fa;
        cursor: pointer;
    }

    .chat-item:hover {
        background-color: #f9fdf0; 
        border-left: 4px solid #81c408; 
    }

    /* --- Area Kanan --- */
    .chat-placeholder {
        height: 100%;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        background-color: #f8f9fa;
        color: #adb5bd;
    }

    /* Custom Scrollbar */
    ::-webkit-scrollbar { width: 5px; }
    ::-webkit-scrollbar-track { background: transparent; }
    ::-webkit-scrollbar-thumb { background: #ccc; border-radius: 10px; }
</style>
@endsection

@section('content')
    <div class="container py-5">
        <h2 class="mb-4 fw-bold text-dark">Pesan Saya</h2>

        {{-- Container Utama --}}
        <div class="row g-0 chat-layout shadow-sm">
            
            {{-- KOLOM KIRI: DAFTAR PESAN --}}
            <div class="col-md-4 chat-sidebar">
                <div class="sidebar-header">
                    <input type="text" class="form-control bg-light border-0 rounded-pill px-3" placeholder="Cari pesan...">
                </div>

                <div class="chat-list-container">
                    <div class="list-group list-group-flush">
                        @forelse($chatList as $chat)
                            <a href="{{ route('pembeli.pesan.show', $chat['lawan_id']) }}"
                                class="list-group-item list-group-item-action chat-item py-3 border-0">
                                <div class="d-flex align-items-center">
                                    {{-- Avatar --}}
                                    <div class="position-relative">
                                        <img src="{{ asset('template/frontend/img/avatar.jpg') }}" class="rounded-circle shadow-sm"
                                            width="50" height="50" style="object-fit: cover;">
                                        @if ($chat['unread'] > 0)
                                            <span class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger border border-white">
                                                {{ $chat['unread'] }}
                                            </span>
                                        @endif
                                    </div>

                                    {{-- Info Pesan --}}
                                    <div class="ms-3 flex-grow-1 overflow-hidden">
                                        <div class="d-flex justify-content-between align-items-center mb-1">
                                            <h6 class="mb-0 text-dark fw-bold text-truncate">{{ $chat['nama'] }}</h6>
                                            <small class="text-muted" style="font-size: 0.75rem;">{{ $chat['time'] }}</small>
                                        </div>
                                        <p class="mb-0 text-muted text-truncate small">
                                            {{ Str::limit($chat['last_message'], 35) }}
                                        </p>
                                    </div>
                                </div>
                            </a>
                        @empty
                            <div class="text-center py-5 px-3">
                                <i class="far fa-comment-dots fa-3x text-muted mb-3 opacity-50"></i>
                                <p class="text-muted small">Belum ada percakapan.</p>
                            </div>
                        @endforelse
                    </div>
                </div>
            </div>

            {{-- KOLOM KANAN: PLACEHOLDER  --}}
            <div class="col-md-8 d-none d-md-flex chat-placeholder">
                <div class="text-center">
                    <div class="bg-white p-4 rounded-circle shadow-sm d-inline-block mb-3">
                        <i class="fas fa-comments fa-4x text-success"></i>
                    </div>
                    <h5 class="fw-bold text-dark">Selamat Datang di Chat</h5>
                    <p class="small">Pilih salah satu percakapan di sebelah kiri<br>untuk mulai berdiskusi dengan Petani.</p>
                </div>
            </div>
        </div>
    </div>
@endsection