<?php
class kontrolDB
{    
    private $_host     = 'localhost';
    private $_username = 'porj1834_test2234';
    private $_password = 'panjaitan232';
    private $_database = 'porj1834_dbtest';
    protected $conn;
    
    function __construct()
    {
        if (!isset($this->conn)) {
            $this->conn = new mysqli($this->_host, $this->_username, $this->_password, $this->_database);
            if ($this->conn->connect_error) {
                // Jangan exit — biarkan aplikasi tetap jalan walau DB error
                $this->conn = null;
                return;
            }
        }
        return $this->conn;
    }

    public function bindQueryParams($sql, $param_type, $param_value_array) {
        $param_value_reference[] = & $param_type;
        for ($i = 0; $i < count($param_value_array); $i++) {
            $param_value_reference[] = & $param_value_array[$i];
        }
        call_user_func_array(array($sql, 'bind_param'), $param_value_reference);
    }

    public function justExecute($query) {
        if (!$this->conn) return false;
        $result = $this->conn->query($query);
        return $result;
    }

    public function getDataAll($query) {
        if (!$this->conn) return array();
        $result    = $this->conn->query($query);
        $resultset = array();
        if ($result && $result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                $resultset[] = $row;
            }
        }
        return $resultset;
    }

    /**
     * FIX UTAMA:
     * getData() dulu selalu panggil bindQueryParams() meski param kosong.
     * Pada PHP >= 8.1, bind_param("") dengan string kosong = fatal error -> halaman blank.
     * Sekarang: jika tidak ada parameter, langsung pakai query() biasa.
     */
    public function getData($query, $param_type, $param_value_array) {
        if (!$this->conn) return null;

        // Jika tidak ada parameter, langsung query tanpa prepare+bind
        if (empty($param_type) || count($param_value_array) === 0) {
            $result    = $this->conn->query($query);
            $resultset = array();
            if ($result && $result->num_rows > 0) {
                while ($row = $result->fetch_assoc()) {
                    $resultset[] = $row;
                }
            }
            return !empty($resultset) ? $resultset : null;
        }

        // Ada parameter: gunakan prepared statement seperti biasa
        $sql = $this->conn->prepare($query);
        if (!$sql) return null;
        $this->bindQueryParams($sql, $param_type, $param_value_array);
        $sql->execute();
        $result    = $sql->get_result();
        $resultset = array();
        if ($result && $result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                $resultset[] = $row;
            }
        }
        return !empty($resultset) ? $resultset : null;
    }

    public function cekData($query, $param_type, $param_value_array) {
        if (!$this->conn) return false;
        $sql = $this->conn->prepare($query);
        if (!$sql) return false;
        $this->bindQueryParams($sql, $param_type, $param_value_array);
        $sql->execute();
        $result = $sql->get_result();
        return ($result && $result->num_rows > 0);
    }

    public function insert($query, $param_type, $param_value_array) {
        if (!$this->conn) return 0;
        $sql = $this->conn->prepare($query);
        if (!$sql) return 0;
        $this->bindQueryParams($sql, $param_type, $param_value_array);
        $sql->execute();
        return $sql->insert_id;
    }

    public function update($query, $param_type, $param_value_array) {
        if (!$this->conn) return false;
        $sql = $this->conn->prepare($query);
        if (!$sql) return false;
        $this->bindQueryParams($sql, $param_type, $param_value_array);
        return $sql->execute();
    }

    public function delete($query, $param_type, $param_value_array) {
        if (!$this->conn) return false;
        $sql = $this->conn->prepare($query);
        if (!$sql) return false;
        $this->bindQueryParams($sql, $param_type, $param_value_array);
        return $sql->execute();
    }

    public function escape_string($value) {
        if (!$this->conn) return $value;
        return $this->conn->real_escape_string($value);
    }
}
?>