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

@section('css')
<style>
    /* Layout Utama */
    .chat-layout {
        height: 60vh;
        background-color: #fff;
        border: 1px solid #dee2e6;
        border-radius: 12px;
        overflow: hidden;
    }

    /* --- Sidebar Kiri --- */
    .chat-sidebar {
        background-color: #fff;
        border-right: 1px solid #dee2e6 !important;
        height: 100%;
        display: flex;
        flex-direction: column;
    }

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

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

    /* --- Area Chat Kanan --- */
    .chat-main {
        background-color: #ffffff;
        display: flex;
        flex-direction: column;
        height: 100%;
    }

    .chat-header {
        padding: 15px 20px;
        background: #fff;
        border-bottom: 1px solid #dee2e6; 
        z-index: 10;
    }

    .chat-content {
        flex-grow: 1;
        overflow-y: auto;
        padding: 25px;
        background-image: url('https://www.transparenttextures.com/patterns/subtle-white-feathers.png');*/
        background-color: #f8f9fa;
        display: flex;
        flex-direction: column;
        gap: 15px;
    }

    /* Bubble Chat */
    .chat-bubble {
        max-width: 75%;
        padding: 12px 18px;
        position: relative;
        font-size: 0.95rem;
        line-height: 1.5;
        border-radius: 18px;
        box-shadow: 0 1px 2px rgba(0,0,0,0.1);
    }

    .bubble-me {
        background-color: #81c408;
        color: #fff;
        align-self: flex-end;
        border-bottom-right-radius: 4px;
    }

    .bubble-other {
        background-color: #fff;
        border: 1px solid #e9ecef;
        color: #333;
        align-self: flex-start;
        border-bottom-left-radius: 4px;
    }

    .chat-time {
        font-size: 0.7rem;
        margin-top: 4px;
        display: block;
        text-align: right;
        opacity: 0.75;
    }

    .chat-footer {
        padding: 15px 20px;
        background: #fff;
        border-top: 1px solid #dee2e6; 
    }

    /* 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">
        <div class="row g-0 chat-layout shadow-lg">

            {{-- SIDEBAR KIRI  --}}
            <div class="col-md-4 chat-sidebar d-none d-md-flex">
                {{-- 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">
                    @foreach ($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 {{ $chat['lawan_id'] == $lawan->id ? 'active' : '' }}">

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

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

                {{-- Header Chat --}}
                <div class="chat-header d-flex align-items-center justify-content-between">
                    <div class="d-flex align-items-center">
                        <a href="{{ route('pembeli.pesan.index') }}" class="d-md-none me-3 text-dark"><i class="fas fa-arrow-left"></i></a>

                        <div class="rounded-circle d-flex align-items-center justify-content-center fw-bold text-white me-3 shadow-sm"
                            style="width: 40px; height: 40px; background-color: #81c408; font-size: 1.1rem;">
                            {{ substr($lawan->nama_lengkap, 0, 1) }}
                        </div>

                        <div>
                            <h6 class="mb-0 fw-bold" style="color: #333;">{{ $lawan->nama_lengkap }}</h6>
                            <small class="text-muted" style="font-size: 0.8rem;">
                                <i class="fas fa-store me-1 text-success"></i> {{ $lawan->nama_usaha ?? 'Online' }}
                            </small>
                        </div>
                    </div>
                </div>

                {{-- Isi Pesan (Chat Box) --}}
                <div class="chat-content" id="chatBox">
                    @php
                        $currentUser = Auth::guard('petani')->check() ? Auth::guard('petani')->user() : Auth::guard('pembeli')->user();
                        $currentUserType = get_class($currentUser);
                    @endphp

                    @forelse ($chats as $chat)
                        @php
                            $isMe = ($chat->pengirim_id == $currentUser->id) && ($chat->pengirim_type == $currentUserType);
                        @endphp

                        <div class="chat-bubble {{ $isMe ? 'bubble-me' : 'bubble-other' }}">
                            {{ $chat->isi_pesan }}
                            <span class="chat-time {{ $isMe ? 'text-white-50' : 'text-muted' }}">
                                {{ $chat->created_at->format('H:i') }}
                                @if ($isMe) <i class="fas fa-check ms-1" style="font-size: 0.6rem;"></i> @endif
                            </span>
                        </div>
                    @empty
                        <div class="text-center mt-5 opacity-50">
                            <i class="far fa-comments fa-3x mb-3 text-secondary"></i>
                            <p class="small text-muted">Mulai percakapan dengan<br><strong>{{ $lawan->nama_lengkap }}</strong></p>
                        </div>
                    @endforelse
                </div>

                {{-- Input Footer --}}
                <div class="chat-footer">
                    <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="{{ get_class($lawan) }}">

                        <input type="text" name="isi_pesan" class="form-control bg-light border-0 rounded-pill px-4 py-2"
                            placeholder="Tulis pesan..." required autocomplete="off">

                        <button type="submit" class="btn btn-success rounded-circle d-flex align-items-center justify-content-center shadow-sm"
                            style="width: 42px; height: 42px; background-color: #81c408; border: none;">
                            <i class="fas fa-paper-plane"></i>
                        </button>
                    </form>
                </div>

            </div>
        </div>
    </div>
@endsection

@section('js')
    <script>
        $(document).ready(function () {
            const chatBox = document.getElementById("chatBox");
            if (chatBox) {
                chatBox.scrollTop = chatBox.scrollHeight;
            }
            $('input[name="isi_pesan"]').focus();
        });
    </script>
@endsection