<?php
namespace App\Models;

use Core\Database;
use PDO;

class KIP
{
    protected $pdo;

    public function __construct()
    {
        $this->pdo = Database::getConnection();
    }

    // Ambil pendaftar dengan pagination dan search
    public function getPaginated($search = '', $offset = 0, $limit = 15)
    {
        $sql = "SELECT * FROM tb_reg_kip 
                WHERE nama_camaba LIKE :search OR nik_ayah LIKE :search 
                ORDER BY created_at DESC
                LIMIT :offset, :limit";
        $stmt = $this->pdo->prepare($sql);
        $stmt->bindValue(':search', "%$search%", PDO::PARAM_STR);
        $stmt->bindValue(':offset', (int)$offset, PDO::PARAM_INT);
        $stmt->bindValue(':limit', (int)$limit, PDO::PARAM_INT);
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    // Hitung total data (untuk pagination)
    public function countAll($search = '')
    {
        $sql = "SELECT COUNT(*) FROM tb_reg_kip 
                WHERE nama_camaba LIKE :search OR nik_ayah LIKE :search";
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute([':search' => "%$search%"]);
        return (int) $stmt->fetchColumn();
    }
}
