<?php
include __DIR__.'/../config.php';
class DB{
    private $con;

    function __construct(){
        //$this->general  = $general;
        $this->con=$GLOBALS['connection'];
    }
    
    public function update_by_id($id,$table,$data){
        return $this->update($table,$data,['id'=>$id]);
    }
    public function select_all($table, $where='', $fields='*', $echo = 'No',&$jArray=[]){
        $result = [];
        if($fields == ''){$fields = '*';}
        $query = "SELECT ".$fields." FROM $table $where";
        if($echo=='array'){
            $jArray['select_all'][]=$query;
        }
        else{
            if($echo != 'No'){echo '<pre>'.$query.'</pre>';}
        }
        try{
            $all = mysqli_query($this->con,$query);
        }
        catch(Exception $e){
            $backtrace=debug_backtrace();
            $dd=[
                'b'=>$backtrace,
                'e'=>$e->getMessage()
            ];
            create_log('select_all_error',$dd);
        }
        if(mysqli_error($this->con)!=''){
            if($echo=='array'){
                $jArray['select_all_error'][]=mysqli_error($this->con);
            }
            else{
                if($echo != 'No'){echo mysqli_error($this->con).'<br>';}
            }
        }
        else{
            try{
                while($table= mysqli_fetch_assoc($all)){$result[] = $table;}
            }
            catch(Exception $e){
                $backtrace=debug_backtrace();
                $dd=[
                    'b'=>$backtrace,
                    'e'=>$e->getMessage()
                ];
                create_log('memoryOverData',$dd);
                exit;
            }
        }
        return $result;
    }
    public function get_row(string $table, string $where,string $value='',$echo='No',&$jArray=[]){
        if($value==''){
            $query="SELECT * FROM $table $where";
        }
        else{
            $query="SELECT * FROM $table WHERE $where = '$value'";
        }
        
        if($echo=='array'){
            $jArray[fl()][]=$query;
        }
        else{
            if($echo != 'No'){echo '<pre>'.$query.'</pre>';}
        }
        $sql = mysqli_query($this->con,$query);
        if(mysqli_error($this->con)!=''){
            $backtrace = debug_backtrace();
            //echo mysqli_error($this->con);
            create_log('get_row_data_error',['backtrace'=>$backtrace,'query'=>$query,'SQL Query error. '.mysqli_error($this->con)]);
        }
        $row = mysqli_fetch_assoc($sql);
        if($echo=='array'){
            $jArray[fl()][]=mysqli_error($this->con);
        }
        else{
            if($echo != 'No'){echo mysqli_error($this->con).'<br>';}
        }
        return $row;

    }
    public function get_row_by_id(string $table, string $id,string $echo='No',&$jArray=[]){
        $query="SELECT * FROM $table WHERE id= $id";

        if($echo=='array'){
            $jArray[fl()][]=$query;
        }
        else{
            if($echo != 'No'){echo '<pre>'.$query.'</pre>';}
        }
        $sql = mysqli_query($this->con,$query);
        if(mysqli_error($this->con)!=''){
            $backtrace = debug_backtrace();
            //echo mysqli_error($this->con);
            create_log('get_row_data_error',['backtrace'=>$backtrace,'query'=>$query,'SQL Query error. '.mysqli_error($this->con)]);
        }
        $row = mysqli_fetch_assoc($sql);
        if($echo=='array'){
            $jArray[fl()][]=mysqli_error($this->con);
        }
        else{
            if($echo != 'No'){echo mysqli_error($this->con).'<br>';}
        }
        return $row;

    }
    public function update(string $table,array $data,array $where,string $echo='No',array &$jArray=[]){
        $count = count($data);$where_count = count($where);$sql = "UPDATE `".$table."` SET";
        $start = 0;
        foreach ($data as $k => $v){$start = $start + 1;if($start == $count){ $sql .= " `".$k."` = '".$this->esc($v)."'"; } else { $sql .= " `".$k."` = '".$this->esc($v)."', "; }
        }
        $sql .= " WHERE ";
        if($where_count == 1){ foreach($where as $m => $n) { $sql .= "`".$m."` = '".$this->esc($n)."'"; } }
        else{$x=0;foreach($where as $m=>$n){
            $x = $x + 1;if($x == $where_count){$sql.= "`".$m."`='".$this->esc($n)."'";}else{$sql.= "`".$m."`='".$this->esc($n)."' and ";}}}
        if($echo=='array'){$jArray[fl()][]=$sql;}
        else{if($echo != 'No'){echo '<pre>'.$sql.'</pre>';}}
        $update = mysqli_query($this->con,$sql);
        if($echo=='array'){$jArray[fl()][]=mysqli_error($this->con);}
        else{if(($echo != 'No')){echo mysqli_error($this->con);}}
        if($update){return true;}else{return false;}
    }
    
    /**
    * Insert data to table
    * @param (string) table name
    * @param (array) inserted data
    * @param (bool) if need mysqli_insert_id then true
    * @param (string) array/anything
    * @param (array) if $echo set array then here set value
    * @return bool/int
    */
    public function insert($table,$data,$getId=false,$echo='No',&$jArray=array()){
        $count = count($data);
        $sql = "INSERT INTO `".$table."` (";
        $start=0;
        foreach($data as $k=>$v){$start=$start+1;if($start==$count){$sql.= "`".$k."`";}else{$sql.="`".$k."`,";}}
        $sql.= ") value (";
        $number=0;
        foreach($data as $k=>$v){$number=$number+1;if($number==$count){$sql.="'".$this->esc($v)."'";}else{$sql.="'".$this->esc($v)."', ";}}
        $sql.= ")";
        if($echo != 'No'&&$echo!='array'){
            echo '<pre>'.$sql.';</pre>';
        }
        $insert=mysqli_query($this->con,$sql);
        if($echo=='array'){
            $jArray[__LINE__][]=$sql;
        }
        else{
            if($echo != 'No'&&$echo!='array'){
                echo '<pre>'.mysqli_error($this->con).'</pre>';
            }
        }
        if(mysqli_error($this->con)!=''){
            $backtrace = debug_backtrace();
            create_log('insertError',['backtrace'=>$backtrace,'query'=>$sql,'SQL Query error. '.mysqli_error($this->con)]);
        }
        if($echo=='array'){
            $jArray[__LINE__][]=mysqli_error($this->con);
        }
        if($insert){
            if($getId=='getId'||$getId===true){return mysqli_insert_id($this->con);}
            else{
                return true;
            }
        }else{
            return false;
        }
    }
    public function delete($table, $where,$echo = 'No'){
        $where_count = count($where);
        $sql = "DELETE FROM `".$table."` WHERE ";
        if($where_count == 1){foreach($where as $m => $n){$sql .= "`".$m."` = '".$this->esc($n)."'";}}
        else{$x = 0;foreach($where as $m => $n){$x = $x + 1;if($x == $where_count){$sql .= "`".$m."` = '".$this->esc($n)."'";} else {$sql .= "`".$m."` = '".$this->esc($n)."' and ";}}}
        if($echo != 'No'){echo '<pre>'.$sql.'</pre><br>'; }
        $delete = mysqli_query($this->con,$sql);
        if(mysqli_error($this->con)!=''){
            if($echo!='No'){
                echo '<pre>'.mysqli_error($this->con).'</pre>';
            }
            $backtrace = debug_backtrace();
            create_log('delete_error',['backtrace'=>$backtrace,'query'=>$sql,'SQL Query error. '.mysqli_error($this->con)]);
        }
        if($delete)return true;else return false;
    }
    public function last_error(){return mysqli_error($this->con);}
    public function esc($string){
        return mysqli_real_escape_string($this->con,$string);
    }
}