<?php


abstract class Model_New
{
    public $table;
    public $conNew;
    public $columns = [];

    public function __construct()
    {
        if ($_SERVER['SERVER_NAME'] == 'localhost') {
            $this->conNew = new mysqli(
                "localhost", "root", "", "emfood_system"
            );
        } else {
            $this->conNew = new mysqli(
                "localhost","emfoodapp_user","x-g}ZxppJmn,","emfoodapp_emfoodsystem"
            );
        }
        mysqli_query($this->conNew,"set character_set_server='utf8'");
        mysqli_query($this->conNew,"set names 'utf8'");
        mysqli_query($this->conNew, "SET sql_mode=''");

        // Check connection
        if ($this->conNew->connect_error) {
            die("Connection failed: " . $this->conNew->connect_error);
        }
    }


    public function create(array $data)
    {
        $columns = '';
        $values = '';
        foreach ($this->columns as $column) {
            $columns .= "`" . $column . "`, ";
            if (isset($data[$column])) {
                $values .= "'" . htmlspecialchars($data[$column]) . "', ";
            } else {
                $values .= "NULL , ";
            }
        }
        $columns = rtrim($columns, ', ');
        $values = rtrim($values, ', ');


        $sql = "INSERT INTO `" . $this->table . "` (" . $columns . ") VALUES (" . $values . ")";

        if ($this->conNew->query($sql) === TRUE) {
            return $this->conNew->insert_id;
        } else {
            echo get_error("Error: " . $this->conNew->error);
        }
    }

    public function update(array $data)
    {
        $fields = '';
        foreach ($data as $key => $value) {
            $fields .= "`" . $key . "` = '" . htmlspecialchars($value) . "', ";
        }
        $fields = rtrim($fields, ', ');


        $sql = "UPDATE `" . $this->table . "` SET $fields WHERE `" .
            $this->table . "`.`id` = " . $data['id'];

        if ($this->conNew->query($sql) === TRUE) {
            return true;
        } else {
            echo get_error("Error: " . $this->conNew->error);
            return false;
        }
    }

    public function delete($id)
    {
        $sql = "DELETE FROM `" . $this->table .
            "` WHERE `" . $this->table . "`.`id` = " . $id;

        return $this->conNew->query($sql);
    }
    public function deleteFeatureImages($id)
    {
        $sql = "DELETE FROM `" . $this->table .
            "` WHERE `" . $this->table . "`.`id_feature` = " . $id;

        return $this->conNew->query($sql);
    }

    public function count($get)
    {
        $sql = " SELECT id FROM `" . $this->table . "` WHERE display = 1";

        $result = $this->conNew->query($sql);

        return $result->num_rows;
    }
    public function countPremium($get)
    {
        $sql = " SELECT id FROM `" . $this->table . "` WHERE display = 1 and type = 1";

        $result = $this->conNew->query($sql);

        return $result->num_rows;
    }
    public function countFree($get)
    {
        $sql = " SELECT id FROM `" . $this->table . "` WHERE display = 1 and type = 0";

        $result = $this->conNew->query($sql);

        return $result->num_rows;
    }

    public function countAll() {

        $sql = " SELECT * FROM `" . $this->table . "`  WHERE display = 1";

        $result = $this->conNew->query($sql);

        return $result->num_rows;
    }

    public function getAll($aStart = 0, $aLimit = 0, $get = []) {
        $arr = array();
        $sql = " SELECT * FROM `" . $this->table . "` ORDER BY `id` DESC ";

        $sql .= $aLimit > 0 ? "LIMIT {$aStart},{$aLimit}" : "";
        $result = $this->conNew->query($sql);
        $x = 1;
        while ($row = mysqli_fetch_assoc($result)) {
            array_push($arr, $row);
            $x++;
        }
        return $arr;
    }

    public function show() {
        $arr = array();
        $sql = " SELECT * FROM `" . $this->table . "` ORDER BY `id` DESC ";

        $result = $this->conNew->query($sql);
        $x = 1;
        while ($row = mysqli_fetch_assoc($result)) {
            array_push($arr, $row);
            $x++;
        }
        return $arr;
    }

    public function getActive() {
        $arr = array();
        $sql = " SELECT * FROM `" . $this->table . "` WHERE display = 1 ORDER BY `id` DESC ";

        $result = $this->conNew->query($sql);
        $x = 1;
        while ($row = mysqli_fetch_assoc($result)) {
            array_push($arr, $row);
            $x++;
        }
        return $arr;
    }

    public function getById($id)
    {
        $sql = " SELECT * FROM `" . $this->table . "` WHERE id = '$id'  ";

        $result = $this->conNew->query($sql);
        return mysqli_fetch_assoc($result);
    }
    public function getByIdImages($id)
    {
        $arr = array();
        $sql = " SELECT * FROM `" . $this->table . "` WHERE id_feature = '$id'  ";

        $result = $this->conNew->query($sql);
        $x = 1;
        while ($row = mysqli_fetch_assoc($result)) {
            array_push($arr, $row);
            $x++;
        }
        return $arr;
    }

    public function getActivePremium() {
        $arr = array();
        $sql = " SELECT * FROM `" . $this->table . "` WHERE display = 1 and type = 1 ORDER BY `arrangement`";

        $result = $this->conNew->query($sql);
        $x = 1;
        while ($row = mysqli_fetch_assoc($result)) {
            array_push($arr, $row);
            $x++;
        }
        return $arr;
    }

    public function getActiveNotPremium() {
        $arr = array();
        $sql = " SELECT * FROM `" . $this->table . "` WHERE display = 1 and type = 0 ORDER BY `arrangement`";

        $result = $this->conNew->query($sql);
        $x = 1;
        while ($row = mysqli_fetch_assoc($result)) {
            array_push($arr, $row);
            $x++;
        }
        return $arr;
    }

}



