<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;

use Exception;
use PDO;

class TestController extends Controller
{

    function queryMBS()
    {
        $db = new Database('online');
        $sql = "SELECT * FROM data_nasabah_master WHERE nasabah_id = '01.1.11757'";
        $user = $db->fetch($sql);
        return response()->json([
            'success' => true,
            'data' => $user
        ]);
    }

    function query88()
    {
        $db = new Database('server88');
        $sql = "SELECT * FROM tb_user";
        $nasabah = $db->fetchAll($sql);
        return response()->json([
            'success' => true,
            'data' => $nasabah
        ]);
    }

    function php()
    {
        phpinfo();
    }
}

class Config
{

    public static $database = [
        "local" => [
            "host" => "localhost",
            "username" => "root",
            "password" => "",
            "db" => "bkk_mobile",
        ],
        "simpegbkk" => [
            "host" => "10.5.88.2",
            "username" => "administrator",
            "password" => "adminppti123!",
            "db" => "simpegbkk",
        ],
        "simpeg" => [
            "host" => "10.5.88.2",
            "username" => "administrator",
            "password" => "adminppti123!",
            "db" => "simpeg",
        ],
        "simpegdev" => [
            "host" => "10.5.88.2",
            "username" => "administrator",
            "password" => "adminppti123!",
            "db" => "simpeg_dev",
        ],
        "server88" => [
            "host" => "10.5.88.2",
            "username" => "bkk_mobile",
            "password" => "p1#",
            "db" => "bkk_mobile",
        ],
        // "server88" => [
        //     "host" => "10.5.88.2",
        //     "username" => "administrator",
        //     "password" => "adminppti123!",
        //     "db" => "bkk_mobile",
        // ],
        "bkk_mobile_backup" => [
            "host" => "10.5.88.2",
            "username" => "administrator",
            "password" => "adminppti123!",
            "db" => "bkk_mobile_backup",
        ],
        "online" => [
            "host" => "10.5.50.2",
            "username" => "adminbpr",
            "password" => "adminbpr**",
            "db" => "mbsbackup",
        ],
        "mbsonline" => [
            "host" => "10.5.50.2",
            "username" => "adminbpr",
            "password" => "adminbpr**",
            "db" => "mbs-online",
        ],
        "hosting" => [
            "host" => "203.175.8.57",
            "username" => "wwwy3493_bkk_2022",
            // "password" => "Purwokerto123!",
            "password" => "bkk_2022",
            "db" => "wwwy3493_bkk_2022",
        ],
    ];
}

class Database
{

    public $con;

    public function __construct($source = "server88")
    {
        try {
            $this->con = new PDO("mysql:host=" . Config::$database[$source]['host'] . ";dbname=" . Config::$database[$source]['db'], Config::$database[$source]['username'], Config::$database[$source]['password']);
            $this->con->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
            $this->con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            return $this->con;
        } catch (Exception $error) {
            die("Connection Gagal : $error");
        }
    }

    function runQuery($query)
    {
        $stmt = $this->con->prepare($query);
        try {
            $stmt->execute();
            return $this->con->lastInsertId();
        } catch (Exception $error) {
            throw $error;
        }
    }

    function fetch($query)
    {
        $stmt = $this->con->prepare($query);
        $stmt->execute();
        return $stmt->fetch();
    }

    function fetchAll($query)
    {
        $stmt = $this->con->prepare($query);
        $stmt->execute();
        return $stmt->fetchAll();
    }

    function count($table, $condition)
    {
        $sql = "SELECT COUNT(*) as count FROM $table WHERE $condition";
        try {
            $stmt = $this->con->prepare($sql);
            $stmt->execute();
            return $stmt->fetch()['count'];
        } catch (Exception $error) {
            throw $error;
        }
    }
}
