<?php
/**
 * HomeController - Mengelola halaman beranda dan publik
 * VERSI TERBARU - TANPA harga_base
 */
class HomeController {
    private $pdo;
    
    public function __construct($pdo) {
        $this->pdo = $pdo;
    }
    
    /**
     * Halaman Beranda - Menampilkan 6 kapal terbaru (HANYA SEBAGAI DISPLAY)
     */
    public function index() {
        // ==============================================
        // AMBIL 6 KAPAL TERBARU UNTUK DISPLAY DI BERANDA
        // ==============================================
        $stmt = $this->pdo->query("
            SELECT 
                k.id,
                k.pemilik_id,
                k.nama_kapal,
                k.jenis,
                k.kapasitas,
                k.fasilitas,
                k.nomor_lambung,
                k.foto,
                k.status_aktif,
                k.status_verifikasi,
                k.verified_at,
                k.created_at,
                k.pelabuhan_asal,
                k.pelabuhan_tujuan,
                k.rute,
                k.deskripsi,
                u.nama as pemilik,
                COALESCE(
                    (SELECT harga_jual FROM kapal_rute WHERE kapal_id = k.id AND is_active = 1 ORDER BY id LIMIT 1),
                    0
                ) as harga_tampil
            FROM kapal k 
            JOIN users u ON k.pemilik_id = u.id 
            WHERE k.status_verifikasi = 'verified' 
              AND k.status_aktif = 1 
            ORDER BY k.created_at DESC 
            LIMIT 6
        ");
        $kapalList = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        // ==============================================
        // HITUNG TOTAL KAPAL UNTUK DISPLAY DI BERANDA
        // ==============================================
        $stmt = $this->pdo->query("
            SELECT COUNT(*) as total 
            FROM kapal 
            WHERE status_verifikasi = 'verified' 
              AND status_aktif = 1
        ");
        $totalKapal = $stmt->fetch(PDO::FETCH_ASSOC)['total'] ?? 0;
        
        // Ambil data user yang sedang login (jika ada)
        $userData = null;
        if(isset($_SESSION['user_id'])) {
            $stmt = $this->pdo->prepare("SELECT * FROM users WHERE id = ?");
            $stmt->execute([$_SESSION['user_id']]);
            $userData = $stmt->fetch(PDO::FETCH_ASSOC);
        }
        
        require_once __DIR__ . '/../views/home/index.php';
    }
    
    /**
     * Halaman Tentang
     */
    public function tentang() {
        require_once __DIR__ . '/../views/home/tentang.php';
    }
    
    /**
     * Halaman Cara Booking
     */
    public function caraBooking() {
        require_once __DIR__ . '/../views/home/cara_booking.php';
    }
    
    /**
     * Halaman Daftar Kapal - SEMUA kapal terverifikasi (untuk halaman kapal)
     */
    public function kapal() {
        // ==============================================
        // AMBIL SEMUA KAPAL TERVERIFIKASI
        // ==============================================
        $stmt = $this->pdo->query("
            SELECT 
                k.id,
                k.pemilik_id,
                k.nama_kapal,
                k.jenis,
                k.kapasitas,
                k.fasilitas,
                k.nomor_lambung,
                k.foto,
                k.status_aktif,
                k.status_verifikasi,
                k.verified_at,
                k.created_at,
                k.pelabuhan_asal,
                k.pelabuhan_tujuan,
                k.rute,
                k.deskripsi,
                u.nama as pemilik,
                COALESCE(
                    (SELECT harga_jual FROM kapal_rute WHERE kapal_id = k.id AND is_active = 1 ORDER BY id LIMIT 1),
                    0
                ) as harga_tampil
            FROM kapal k 
            JOIN users u ON k.pemilik_id = u.id 
            WHERE k.status_verifikasi = 'verified' 
              AND k.status_aktif = 1 
            ORDER BY k.nama_kapal ASC
        ");
        $kapalList = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        // Ambil data user yang sedang login (jika ada)
        $userData = null;
        if(isset($_SESSION['user_id'])) {
            $stmt = $this->pdo->prepare("SELECT * FROM users WHERE id = ?");
            $stmt->execute([$_SESSION['user_id']]);
            $userData = $stmt->fetch(PDO::FETCH_ASSOC);
        }
        
        require_once __DIR__ . '/../views/home/kapal.php';
    }
    
    /**
     * API/JSON - Get all kapal for booking page (AJAX)
     */
    public function getKapalList() {
        header('Content-Type: application/json');
        
        $stmt = $this->pdo->query("
            SELECT 
                k.id,
                k.nama_kapal,
                k.jenis,
                k.kapasitas,
                k.fasilitas,
                k.foto,
                k.pelabuhan_asal,
                k.pelabuhan_tujuan,
                k.rute,
                k.deskripsi,
                u.nama as pemilik,
                COALESCE(
                    (SELECT harga_jual FROM kapal_rute WHERE kapal_id = k.id AND is_active = 1 ORDER BY id LIMIT 1),
                    0
                ) as harga_tampil
            FROM kapal k 
            JOIN users u ON k.pemilik_id = u.id 
            WHERE k.status_verifikasi = 'verified' 
              AND k.status_aktif = 1 
            ORDER BY k.nama_kapal ASC
        ");
        $kapalList = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        echo json_encode([
            'success' => true,
            'data' => $kapalList,
            'total' => count($kapalList)
        ]);
        exit;
    }
}