@extends('layouts.frontend')

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

@section('content')
    <div class="container-fluid page-header py-5 mb-5">
        <h1 class="text-center text-white display-6">Percakapan</h1>
        <ol class="breadcrumb justify-content-center mb-0">
            <li class="breadcrumb-item"><a href="{{ route('pembeli.pesan.index') }}" class="text-white">Pesan Saya</a></li>
            <li class="breadcrumb-item active text-white">Detail</li>
        </ol>
    </div>

    <div class="container py-3">
        <div class="row justify-content-center">
            <div class="col-lg-8 col-md-10">

                <div class="card border shadow-sm">
                    <div class="card-header bg-white py-3 border-bottom d-flex align-items-center">
                        <a href="{{ route('pembeli.pesan.index') }}"
                            class="btn btn-sm btn-outline-secondary rounded-circle me-3">
                            <i class="fas fa-arrow-left"></i>
                        </a>
                        <div>
                            <h6 class="mb-0 fw-bold">{{ $lawan->nama_lengkap }}</h6>
                            <small class="text-muted">Petani</small>
                        </div>
                    </div>

                    <div class="card-body bg-light overflow-auto" id="chatBox" style="height: 500px;">
                        @foreach ($chats as $chat)
                            @php
                                $isMe =
                                    $chat->pengirim_id == Auth::guard('pembeli')->id() &&
                                    $chat->pengirim_type == 'App\Models\Pembeli';
                            @endphp

                            <div class="d-flex mb-3 {{ $isMe ? 'justify-content-end' : 'justify-content-start' }}">

                                <div class="p-3 rounded-3"
                                    style="max-width: 75%; 
                                        {{ $isMe ? 'background-color: #0d6efd; color: white;' : 'background-color: white; border: 1px solid #dee2e6;' }}">

                                    <p class="mb-1">{{ $chat->isi_pesan }}</p>

                                    <div class="text-end">
                                        <small style="font-size: 11px; {{ $isMe ? 'color: #e0e0e0;' : 'color: #6c757d;' }}">
                                            {{ $chat->created_at->format('H:i') }}
                                            @if ($isMe)
                                                <i
                                                    class="fas fa-check {{ $chat->sudah_dibaca ? 'text-warning' : '' }} ms-1"></i>
                                            @endif
                                        </small>
                                    </div>
                                </div>
                            </div>
                        @endforeach
                    </div>

                    <div class="card-footer bg-white p-3">
                        <form action="{{ route('pesan.kirim') }}" method="POST">
                            @csrf
                            <input type="hidden" name="penerima_id" value="{{ $lawan->id }}">

                            <div class="input-group">
                                <input type="text" name="isi_pesan" class="form-control" placeholder="Tulis pesan..."
                                    required autocomplete="off">
                                <button type="submit" class="btn btn-primary px-4">
                                    <i class="fas fa-paper-plane"></i>
                                </button>
                            </div>
                        </form>
                    </div>

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

@section('js')
    <script>
        $(document).ready(function() {
            var chatBox = document.getElementById("chatBox");
            chatBox.scrollTop = chatBox.scrollHeight;
        });
    </script>
@endsection
