<?php
session_start();
date_default_timezone_set('Asia/Jakarta');

$host = 'localhost';
$user = 'majapahi_omnich';
$pass = 'A1b2c3d4.,@';
$db   = 'majapahi_omnich';

$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) die("Koneksi gagal");

$conn->query("SET time_zone = '+07:00'");

$phone = $conn->real_escape_string($_GET['phone']);
$messages = [];
$msg_query = $conn->query("SELECT * FROM messages WHERE phone_number = '$phone' ORDER BY created_at ASC");

while($row = $msg_query->fetch_assoc()) {
    $messages[] = $row;
}

foreach($messages as $msg) {
    $media_html = '';
    $file_ext = !empty($msg['file_url']) ? strtolower(pathinfo($msg['file_url'], PATHINFO_EXTENSION)) : '';

    if (in_array($file_ext, ['ogg', 'mp3', 'webm', 'wav'])) {
        $media_html = '<audio src="'.htmlspecialchars($msg['file_url']).'" controls controlsList="nodownload" style="max-width: 100%; margin-bottom: 5px; height: 40px; border-radius: 20px; outline: none;"></audio><br>';
    } elseif ($msg['message_type'] == 'image' && !empty($msg['file_url'])) {
        $media_html = '<img src="'.htmlspecialchars($msg['file_url']).'" style="max-width: 100%; max-height: 250px; border-radius: 6px; margin-bottom: 5px; cursor: pointer;" onclick="tampilkanLightbox(this.src)"><br>';
    } elseif ($msg['message_type'] == 'video' && !empty($msg['file_url'])) {
        $media_html = '<video src="'.htmlspecialchars($msg['file_url']).'" controls style="max-width: 100%; max-height: 250px; border-radius: 6px; margin-bottom: 5px;"></video><br>';
    } elseif ($msg['message_type'] == 'document' && !empty($msg['file_url'])) {
        $media_html = '<a href="'.htmlspecialchars($msg['file_url']).'" target="_blank" style="display:inline-flex; align-items:center; padding:10px 14px; background:rgba(0,0,0,0.05); border-radius:6px; color:inherit; text-decoration:none; margin-bottom:5px; font-weight:500; font-size:0.85rem;"><svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2" class="me-2"><path d="M13 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V9z"></path><polyline points="13 2 13 9 20 9"></polyline></svg> Dokumen</a><br>';
    }

    $text_html = !empty($msg['message_text']) ? nl2br(htmlspecialchars($msg['message_text'])) : '';
    
    // Teks mentah untuk disiapkan jika ingin di-forward massal
    $raw_text_for_fw = !empty($msg['message_text']) ? htmlspecialchars($msg['message_text']) : '';

    // EVENT SENTUH/KLIK TAHAN LAMA
    $press_events = 'onmousedown="startPress(event, this.parentElement)" onmouseup="cancelPress()" onmouseleave="cancelPress()" ontouchstart="startPress(event, this.parentElement)" ontouchend="cancelPress()" onclick="bubbleClick(this.parentElement)" style="cursor: pointer;"';

    echo '<div class="message-row ' . $msg['type'] . '" data-id="' . $msg['id'] . '">';
    
    // STRUKTUR CHECKBOX YANG TERSEMBUNYI
    echo '<div class="msg-checkbox-wrapper" style="display:none; align-items:center; padding: 0 15px 0 10px;" onclick="bubbleClick(this.parentElement)">';
    echo '<input type="checkbox" class="msg-checkbox d-none" value="' . $msg['id'] . '">';
    echo '<input type="hidden" class="msg-raw-text" value="' . $raw_text_for_fw . '">';
    echo '<input type="hidden" class="msg-raw-url" value="' . htmlspecialchars($msg['file_url'] ?? '') . '">';
    echo '<div class="custom-cb"></div>';
    echo '</div>';

    if($msg['type'] == 'in'){
        echo '<div class="bubble in" ' . $press_events . '>';
        echo $media_html . $text_html;
        echo '<span class="msg-time">'. date('H:i', strtotime($msg['created_at'])) .'</span>';
        echo '</div>';
    } else {
        echo '<div class="bubble out" ' . $press_events . '>';
        echo $media_html . $text_html;
        echo '<span class="msg-time">'. date('H:i', strtotime($msg['created_at'])) .'</span>';
        echo '</div>';
    }
    echo '</div>';
}
?>
