<?php namespace App\Models;

use CodeIgniter\Database\Query;
use CodeIgniter\Model;
use App\Models\Main;

class Crud extends Model
{
    var $data = array();

    public function __construct()
    {
        $this->data['template'] = TEMPLATE;
        $this->data['path'] = PATH;
    }

    public
    function AlterDatabase()
    {
        $sql = file_get_contents(ROOT . "/db/alter.sql");

        $sqls = explode(';', $sql);
        array_pop($sqls);
        $db = db_connect();
        $db->db_debug = false;
        if (count($sqls) > 0)
            foreach ($sqls as $statement) {
                $statment = $statement . ";";
                if ($db->query($statment)) {
                    //echo $db->getLastQuery() . "<hr>";
                }
            }
        $db->close();
    }

    public
    function AddRecord($table, $records, $view = false)
    {
        $db = db_connect();
        $builder = $db->table($table);
        $builder->insert($records);
        $insertID = $db->insertID();
        if ($view) {
            echo $db->getLastQuery() . "<hr>";
        }
        $db->close();

        return $insertID;
    }

    public
    function AddBulkRecord($table, $records)
    {
        $db = db_connect();
        $db->db_debug = false;
        $db->table($table)->insertBatch($records, true);
        //echo $db->getLastQuery() . "<hr>";
        $db->close();
    }

    public
    function UpdateRecord($table, $records, $where)
    {
        $db = db_connect();
        $builder = $db->table($table);
        if (count($where) > 0) {
            $builder->where($where);
        }
        $builder->update($records);
        //echo $builder->getLastQuery() . "<hr>";
        $db->close();
        return true;
    }

    public
    function DeleteRecord($table, $where)
    {
        $db = db_connect();
        $builder = $db->table($table);
        if (count($where) > 0) {
            $builder->where($where);
        }
        $builder->delete();
        $db->close();

        return true;
    }

    public
    function ListRecords($table, $wheres = array(), $order = array(), $limit = 0)
    {
        $db = db_connect();
        $builder = $db->table($table);

        $builder->select('*');
        if (count($wheres) > 0) {
            $builder->where($wheres);
        }
        if (count($order) > 0) {
            foreach ($order as $ordK => $ordV) {
                $builder->orderBy($ordK, $ordV);
            }
        }
        if ($limit > 0) {
            $builder->limit($limit);
        }

        $query = $builder->get();
        $records = $query->getResultArray();
        //echo $db->getLastQuery() . "<hr>";

        return $records;
    }

    public
    function SingleRecord($table, $wheres = array())
    {
        $db = db_connect();
        $builder = $db->table($table);

        $builder->select('*');
        if (count($wheres) > 0) {
            $builder->where($wheres);
        }
        $query = $builder->get();
        $record = $query->getRowArray();
        //print_r($record);
        //echo $db->getLastQuery() . "<hr>";

        return $record;
    }

    public function ExecuteSQL($Query)
    {
        $db = db_connect();
        $records = $db->query($Query)->getResult('array');
        // echo $db->getLastQuery() . "<hr>";
        return $records;
    }

    public function DatabaseBackup(){
        $db = \Config\Database::connect();
        $dbname = $db->database;
        $path = WRITEPATH.'db_backup/';   		 // change path here
        $filename = $dbname.'_'.date('dMY_Hi').'.sql';   // change file name here

        $util = (new \CodeIgniter\Database\Database())->loadUtils($db);
        $prefs = [
            'tables'             => [],
            'ignore'             => [],
            'filename'           => $filename,
            'format'             => 'gzip', // gzip, txt
            'add_drop'           => true,
            'add_insert'         => true,
            'newline'            => "\n",
            'foreign_key_checks' => true,
        ];
        $backup = $util->backup($prefs,$db);

        write_file($path.$filename.'.gz', $backup);
        return $this->response->download($path.$filename.'.gz',null);
    }

}