@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">
            {{-- Header Sidebar --}}
            <div class="p-3 border-bottom">
                <h6 class="fw-bold text-dark mb-3">Pesan Masuk</h6>
                <input type="text" class="form-control bg-light border-0 rounded-pill px-3"
                    placeholder="Cari percakapan...">
            </div>

            {{-- List Chat --}}
            <div class="flex-grow-1 overflow-auto">
                @forelse ($chatList as $chat)
                <a href="{{ route('pembeli.pesan.show', $chat['lawan_id']) }}"
                    class="d-flex align-items-center p-3 text-decoration-none text-dark chat-item">

                    <div class="position-relative">
                        <img src="{{ asset('template/frontend/img/avatar.jpg') }}" class="rounded-circle shadow-sm"
                            width="45" height="45" style="object-fit: cover;">

                        {{-- Badge Merah Notifikasi --}}
                        @if($chat['unread'] > 0)
                        <span
                            class="position-absolute top-0 start-100 translate-middle p-1 bg-danger border border-light rounded-circle"></span>
                        @endif
                    </div>

                    <div class="ms-3 flex-grow-1 overflow-hidden">
                        <div class="d-flex justify-content-between align-items-center">
                            <span class="fw-bold small">{{ $chat['nama'] }}</span>
                            <span class="text-muted" style="font-size: 0.7rem;">{{ $chat['time'] }}</span>
                        </div>
                        <div class="d-flex justify-content-between align-items-center mt-1">
                            <p class="mb-0 text-muted small text-truncate" style="max-width: 85%;">
                                {{ Str::limit($chat['last_message'], 25) }}
                            </p>

                            {{-- Angka Merah Notifikasi --}}
                            @if($chat['unread'] > 0)
                            <span class="badge bg-danger rounded-pill" style="font-size: 0.6rem;">{{ $chat['unread']
                                }}</span>
                            @endif
                        </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>

        {{-- 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