@extends('layouts.admin')
@section('title', 'Chat: ' . $lawan->nama_lengkap)

@section('content')
    <style>
        .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;
        }

        .chat-window {
            height: 100%;
            display: flex;
            flex-direction: column;
            background-color: #f2f7ff;
        }

        .chat-content {
            flex-grow: 1;
            overflow-y: auto;
            padding: 1.5rem;
        }

        ::-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">

                {{-- SIDEBAR KIRI (DAFTAR CHAT) --}}
                <div class="col-md-4 chat-sidebar d-none d-md-flex">
                    <div class="p-4 border-bottom bg-white">
                        <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">
                        @foreach ($chatList as $chat)
                            <a href="{{ route('petani.pesan.show', $chat['lawan_id']) }}"
                                class="list-group-item list-group-item-action chat-item p-3 {{ $chat['lawan_id'] == $lawan->id ? 'active' : '' }}">
                                <div class="d-flex align-items-center">

                                    <div class="avatar avatar-lg me-3">
                                        {{-- Avatar dengan 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 fw-bold {{ $chat['lawan_id'] == $lawan->id ? 'text-white' : 'text-dark' }}">
                                                {{ $chat['nama'] }}
                                            </h6>
                                            <small
                                                class="{{ $chat['lawan_id'] == $lawan->id ? 'text-white-50' : 'text-muted' }}"
                                                style="font-size: 0.75rem">
                                                {{ $chat['time'] }}
                                            </small>
                                        </div>
                                        <p
                                            class="mb-0 small text-truncate {{ $chat['lawan_id'] == $lawan->id ? 'text-white-50' : 'text-muted' }}">
                                            {{ Str::limit($chat['last_message'], 30) }}
                                        </p>
                                    </div>
                                </div>
                            </a>
                        @endforeach
                    </div>
                </div>

                {{-- AREA CHAT KANAN --}}
                <div class="col-md-8 chat-window">

                    {{-- Header Chat --}}
                    <div class="p-3 border-bottom bg-white d-flex align-items-center shadow-sm" style="z-index: 10;">
                        <a href="{{ route('petani.pesan.index') }}"
                            class="btn btn-sm btn-light me-3 d-md-none rounded-circle">
                            <i class="bi bi-arrow-left"></i>
                        </a>

                        <div class="avatar avatar-md me-3">
                            <span
                                class="avatar-content rounded-circle d-flex align-items-center justify-content-center text-white font-bold"
                                style="width: 40px; height: 40px; background-color: #81c408;">
                                {{ substr($lawan->nama_lengkap, 0, 1) }}
                            </span>
                        </div>
                        <div>
                            <h6 class="mb-0 text-dark fw-bold">{{ $lawan->nama_lengkap }}</h6>
                            <small class="text-muted">{{ $lawan->role ?? 'Pembeli' }}</small>
                        </div>
                    </div>

                    {{-- Isi Chat --}}
                    <div class="chat-content" id="chatContainer">
                        @forelse($chats as $chat)
                            @php
                                $isMe = ($chat->pengirim_id == Auth::guard('petani')->id() &&
                                    $chat->pengirim_type == 'App\Models\Petani');
                            @endphp

                            <div class="d-flex w-100 mb-3 {{ $isMe ? 'justify-content-end' : 'justify-content-start' }}">
                                <div style="max-width: 75%;">
                                    <div class="p-3 shadow-sm position-relative" style="border-radius: 15px; 
                                                        border-{{ $isMe ? 'bottom-right' : 'bottom-left' }}-radius: 0;
                                                        background-color: {{ $isMe ? '#81c408' : '#ffffff' }}; 
                                                        color: {{ $isMe ? '#ffffff' : '#212529' }};">

                                        <p class="mb-1" style="font-size: 0.95rem; line-height: 1.5; word-wrap: break-word;">
                                            {{ $chat->isi_pesan }}
                                        </p>

                                        <div class="d-flex justify-content-end align-items-center mt-1">
                                            <small style="font-size: 0.7rem; opacity: 0.8; margin-right: 4px;">
                                                {{ $chat->created_at->format('H:i') }}
                                            </small>
                                            @if ($isMe)
                                                <i class="bi {{ $chat->sudah_dibaca ? 'bi-check-all text-info' : 'bi-check' }}"
                                                    style="font-size: 0.9rem;"></i>
                                            @endif
                                        </div>
                                    </div>
                                </div>
                            </div>
                        @empty
                            <div class="text-center my-auto">
                                <span class="badge bg-light text-secondary p-3 rounded-pill shadow-sm">
                                    👋 Belum ada percakapan. Mulai sapa pembeli Anda!
                                </span>
                            </div>
                        @endforelse
                    </div>

                    {{-- Footer Input Pesan --}}
                    <div class="p-3 bg-white border-top">
                        <form action="{{ route('pesan.kirim') }}" method="POST" class="d-flex gap-2 align-items-center">
                            @csrf
                            <input type="hidden" name="penerima_id" value="{{ $lawan->id }}">
                            <input type="hidden" name="penerima_type" value="App\Models\Pembeli">
                            <div class="input-group">
                                <input type="text" name="isi_pesan" class="form-control" placeholder="Ketik pesan..."
                                    required autocomplete="off">
                                <button type="submit" class="btn btn-primary-green">
                                    <i class="bi bi-send-fill"></i> Kirim
                                </button>
                            </div>
                        </form>
                    </div>

                </div>
            </div>
        </div>
    </section>

    <script>
        document.addEventListener("DOMContentLoaded", function () {
            var chatBox = document.getElementById("chatContainer");
            if (chatBox) {
                chatBox.scrollTop = chatBox.scrollHeight;
            }
        });
    </script>
@endsection