<?php
require "Config.php";
class Crud
{
    private $lundry;

    public function __construct($lundry)
    {
        $this->lundry = $lundry;
    }

    public function lundrylogin($username, $password, $tblname)
    {
        if ($tblname == "admin") {
            $q = "SELECT * FROM " . $tblname . " WHERE username='" . $username . "' AND password='" . $password . "'";
            return $this->lundry->query($q)->num_rows;
        } else {
            $q = "SELECT * FROM " . $tblname . " WHERE email='" . $username . "' AND password='" . $password . "'";
            return $this->lundry->query($q)->num_rows;
        }
    }

    public function lundryinsertdata($field, $data, $table)
    {
        $field_values = implode(",", $field);
        $data_values = implode("','", $data);

        $sql = "INSERT INTO $table($field_values)VALUES('$data_values')";
        return $this->lundry->query($sql);
    }

    public function str_replace_first($search, $replace, $subject)
    {
        $search = "/" . preg_quote($search, "/") . "/";
        return preg_replace($search, $replace, $subject, 1);
    }

    public function lundryzoneinsertdata($field, $data, $table)
    {
        $field_values = implode(",", $field);
        $data_values = implode("','", $data);
        $data_values = $this->str_replace_first("','", ",'", $data_values) . "'";
        $sql = "INSERT INTO $table($field_values)VALUES($data_values)";
        return $this->lundry->query($sql);
    }

    public function insmulti($field, $data, $table)
    {
        $field_values = implode(",", $field);
        $data_values = implode("','", $data);

        $sql = "INSERT INTO $table($field_values)VALUES('$data_values')";
        return $this->lundry->multi_query($sql);
    }

    public function lundryinsertdata_id($field, $data, $table)
    {
        $field_values = implode(",", $field);
        $data_values = implode("','", $data);

        $sql = "INSERT INTO $table($field_values)VALUES('$data_values')";
        $result = $this->lundry->query($sql);
        return $this->lundry->insert_id;
    }

    public function lundryinsertdata_Api($field, $data, $table)
    {
        $field_values = implode(",", $field);
        $data_values = implode("','", $data);

        $sql = "INSERT INTO $table($field_values)VALUES('$data_values')";
        return $this->lundry->query($sql);
    }

    public function lundryinsertdata_Api_Id($field, $data, $table)
    {
        $field_values = implode(",", $field);
        $data_values = implode("','", $data);

        $sql = "INSERT INTO $table($field_values)VALUES('$data_values')";
        $result = $this->lundry->query($sql);
        return $this->lundry->insert_id;
    }

    public function lundryupdateData($field, $table, $where)
    {
        $cols = [];

        foreach ($field as $key => $val) {
            if ($val != null) {
                // check if value is not null then only add that colunm to array
                $cols[] = "$key = '$val'";
            }
        }
        $sql = "UPDATE $table SET " . implode(", ", $cols) . " $where";
        return $this->lundry->query($sql);
    }

    public function lundryzoneupdateData($field, $table, $where)
    {
        $cols = [];

        foreach ($field as $key => $val) {
            if ($val != null) {
                // check if value is not null then only add that colunm to array
                if ($key == "coordinates") {
                    $cols[] = "$key = $val";
                } else {
                    $cols[] = "$key = '$val'";
                }
            }
        }
        $sql = "UPDATE $table SET " . implode(", ", $cols) . " $where";
        return $this->lundry->query($sql);
    }

    public function lundryupdateData_Api($field, $table, $where)
    {
        $cols = [];

        foreach ($field as $key => $val) {
            if ($val != null) {
                // check if value is not null then only add that colunm to array
                $cols[] = "$key = '$val'";
            }
        }
        $sql = "UPDATE $table SET " . implode(", ", $cols) . " $where";
        return $this->lundry->query($sql);
    }

    public function lundryupdateData_single($field, $table, $where)
    {
        $query = "UPDATE $table SET $field";

        $sql = $query . " " . $where;
        return $this->lundry->query($sql);
    }

    public function lundryDeleteData($where, $table)
    {
        $sql = "Delete From $table $where";
        return $this->lundry->query($sql);
    }

    public function lundryDeleteData_Api($where, $table)
    {
        $sql = "Delete From $table $where";
        return $this->lundry->query($sql);
    }
}
?>
