<?php
require_once __DIR__ . '/hpl_calculator.php';

function escape($string) {
    return htmlspecialchars($string ?? '', ENT_QUOTES, 'UTF-8');
}

function formatDate($date) {
    if (empty($date)) return '-';
    return date('d-m-Y', strtotime($date));
}

function formatBulan($month) {
    $bulan = [
        1 => 'Januari', 2 => 'Februari', 3 => 'Maret', 4 => 'April',
        5 => 'Mei', 6 => 'Juni', 7 => 'Juli', 8 => 'Agustus',
        9 => 'September', 10 => 'Oktober', 11 => 'November', 12 => 'Desember'
    ];
    return $bulan[(int)$month] ?? '-';
}

function getProfilPosyandu() {
    global $db;
    if (!isset($db)) {
        require_once __DIR__ . '/../config/config.php';
        $db = getDB();
    }
    static $profil = null;
    if ($profil === null) {
        $profil = $db->query("SELECT * FROM profil_posyandu WHERE id = 1")->fetch();
    }
    return $profil;
}

function getLogoUrl() {
    $profil = getProfilPosyandu();
    if (!empty($profil['logo'])) {
        return BASE_URL . '/assets/uploads/profil/' . escape($profil['logo']);
    }
    return BASE_URL . '/assets/uploads/profil/logo_1777612491.jpeg';
}

function renderPagination($total_pages, $current_page, $base_url, $query_params = []) {
    if ($total_pages <= 1) return '';

    // Build query string keeping existing parameters except 'page'
    unset($query_params['page']);
    $query_string = http_build_query($query_params);
    $url_prefix = $base_url . '?' . ($query_string ? $query_string . '&' : '') . 'page=';

    $html = '<div class="px-6 py-4 border-t border-gray-100 bg-gray-50/50 flex flex-col sm:flex-row items-center justify-between gap-4">';
    
    // Info text
    $html .= '<div class="text-sm text-gray-500">';
    $html .= 'Halaman <span class="font-bold text-gray-900">' . $current_page . '</span> dari <span class="font-bold text-gray-900">' . $total_pages . '</span>';
    $html .= '</div>';

    // Buttons
    $html .= '<div class="flex items-center gap-1">';
    
    // Prev
    if ($current_page > 1) {
        $html .= '<a href="' . $url_prefix . ($current_page - 1) . '" class="px-3 py-1.5 rounded-lg border border-gray-200 bg-white text-gray-500 hover:bg-gray-50 hover:text-primary transition-colors text-sm font-medium"><i class="fas fa-chevron-left text-xs mr-1"></i> Prev</a>';
    } else {
        $html .= '<span class="px-3 py-1.5 rounded-lg border border-gray-200 bg-gray-50 text-gray-400 text-sm font-medium cursor-not-allowed"><i class="fas fa-chevron-left text-xs mr-1"></i> Prev</span>';
    }

    // Page Numbers (max 5 visible)
    $start = max(1, $current_page - 2);
    $end = min($total_pages, $current_page + 2);
    
    if ($start > 1) {
        $html .= '<a href="' . $url_prefix . '1" class="hidden sm:block px-3 py-1.5 rounded-lg border border-gray-200 bg-white text-gray-500 hover:bg-gray-50 hover:text-primary transition-colors text-sm font-medium">1</a>';
        if ($start > 2) $html .= '<span class="hidden sm:block px-2 text-gray-400">...</span>';
    }

    for ($i = $start; $i <= $end; $i++) {
        if ($i == $current_page) {
            $html .= '<span class="hidden sm:block px-3 py-1.5 rounded-lg bg-primary text-white text-sm font-bold shadow-sm">' . $i . '</span>';
        } else {
            $html .= '<a href="' . $url_prefix . $i . '" class="hidden sm:block px-3 py-1.5 rounded-lg border border-gray-200 bg-white text-gray-500 hover:bg-gray-50 hover:text-primary transition-colors text-sm font-medium">' . $i . '</a>';
        }
    }

    if ($end < $total_pages) {
        if ($end < $total_pages - 1) $html .= '<span class="hidden sm:block px-2 text-gray-400">...</span>';
        $html .= '<a href="' . $url_prefix . $total_pages . '" class="hidden sm:block px-3 py-1.5 rounded-lg border border-gray-200 bg-white text-gray-500 hover:bg-gray-50 hover:text-primary transition-colors text-sm font-medium">' . $total_pages . '</a>';
    }

    // Next
    if ($current_page < $total_pages) {
        $html .= '<a href="' . $url_prefix . ($current_page + 1) . '" class="px-3 py-1.5 rounded-lg border border-gray-200 bg-white text-gray-500 hover:bg-gray-50 hover:text-primary transition-colors text-sm font-medium">Next <i class="fas fa-chevron-right text-xs ml-1"></i></a>';
    } else {
        $html .= '<span class="px-3 py-1.5 rounded-lg border border-gray-200 bg-gray-50 text-gray-400 text-sm font-medium cursor-not-allowed">Next <i class="fas fa-chevron-right text-xs ml-1"></i></span>';
    }

    $html .= '</div></div>';

    return $html;
}
