<?php

class DBMain
{

    public $conn;
    public $table;

    function __construct($database = 'pro_art_bronze_jobcards', $prefix = 'elegaysv')
    {

        $user = "Code2";
        $pass = "EWG2Cod!@#";
        $db = $database;

        $ip = 'elegantwork.co.za';
        $this->conn = mysqli_connect($ip, $prefix . "_" . $user, $pass, $prefix . '_' . $db);
    }

    function insert($Query = '')
    {

        $this->conn->query($Query);

        return $this->conn->insert_id;
    }

    public function query(string $query): mysqli_result
    {
        $exec = $this->conn->query($query);

        if (!$exec) {
            throw new Exception($this->conn->error);
        }

        return $exec;
    }

    function CheckAccount($email)
    {
    }

    function Update($tableName, $columns = [], $values = [], $where = '1', $devmode = False)
    {

        $this->table = $tableName;

        if (count($columns) == count($values)) {


            $queryDatabase[] = "UPDATE $this->table SET ";
            $i = 0;
            foreach ($columns as $colls) {

                $queryDatabaseCols[] = "`$colls` = '$values[$i]'";

                $i++;
            }
            $queryDatabase[] = implode(',', $queryDatabaseCols);

            $queryDatabase[] = " WHERE $where";

            if ($devmode == True) {

                echo implode('', $queryDatabase);
            } else {

                // echo implode('',$queryDatabase);

                return $this->conn->query(implode('', $queryDatabase));
            }
        } else {
            return "values and columns not same length";
        }
    }

    function exec_query($tableName, $columns = [], $joinType = '', $joinTable = '', $table1Column = '', $table2Column = '', $where = '1', $orderBy = "", $devMode = False)
    {

        $this->table = $tableName;

        if ($columns[0] == '*') {

            $results = "";
            $join = strlen($joinType) > 1 ? " a $joinType JOIN $joinTable b ON a.$table1Column = b.$table2Column " : '';


            $queryDatabase = "SELECT * FROM `$this->table` $join WHERE $where $orderBy";


            $results = $this->conn->query($queryDatabase);


            if (!$results)
                var_dump($results);

            if ($devMode == True) {

                echo $queryDatabase;
            } else {
                return $results;
            }
        } else {
            $queryColumns = [];

            foreach ($columns as $col) {

                $queryColumns[] = " $col ";
            }

            $queryColumns = implode(' , ', $queryColumns);

            $queryDatabase = "SELECT $queryColumns FROM `$this->table` WHERE $where $orderBy";


            $results = $this->conn->query($queryDatabase);


            if ($results->num_rows > 0) {

                if ($devMode == True) {

                    echo $queryDatabase;
                } else {
                    echo $queryDatabase;

                    // return $results;
                }
            } else {

                return "$queryDatabase";
            }
        }
    }

    function add_log($log_text, $table_name, $affected_record_id)
    {
        if (strlen($log_text) < 1) {
            echo "PLEASE SUPPLY A LOG REASON";
        } else {
            $date = date('Y-m-d H:i', strtotime('+ 2 Hours'));
            self::insert("INSERT INTO `logs` (`proart_accounting_users_id`,`log`,`date_time`,`table_name`,`affected_record`) VALUES ({$_SESSION['user_id']},'" . strtoupper($log_text) . "','$date','$table_name',$affected_record_id)");
        }
    }
}
