<?php

namespace Functions;

use Classes\Database;
use \DateTime;


class Custom_Info extends Database
{

    public function add($values)
    {
        if (!is_array($values)) return FALSE;
        if (count($values) <> 4) return FALSE;

        $sql = "INSERT INTO custom_info (userid, data, from_date, app) VALUES (?,?,?,?)";
        $this->prepare($sql)->execute($values);
        return $this->lastInsertId();
    }

    public function update($id, $changes)
    {
        if (!is_array($changes)) return FALSE;
        $keys_sql = "";
        $values = [];
        foreach ($changes as $key => $value) {
            $keys_sql .= "$key=?, ";
            $values[] = $value;
        }
        $keys_sql = substr($keys_sql, 0, -2);
        $sql = "UPDATE custom_info SET $keys_sql WHERE id=$id";
        $this->prepare($sql)->execute($values);
        return $this->lastInsertId();
    }


    public function get($where = NULL, $order_by = NULL, $offset = NULL, $limit = NULL)
    {
        $sql = "SELECT * FROM custom_info";

        if (!is_null($where) && !is_array($where)) return FALSE;
        if (is_array($where)) $sql .= " WHERE {$where[0]}=?";
        if (!is_null($order_by)) $sql .= " ORDER BY $order_by";
        if (!is_null($offset)) $sql .= " OFFSET $offset";
        if (!is_null($limit)) $sql .= " LIMIT $limit";

        $this->prepare($sql);
        if (is_array($where)) $this->execute([$where[1]]);
        $results = $this->resultStr();
        if (count($results) > 0) {
            return $results;
        } else {
            return false;
        }

    }

    public function remove($id)
    {
        $sql = "DELETE FROM custom_info WHERE id = ?";
        return $this->prepare($sql)->execute([$id]);
    }

    public function get_by_id($id)
    {
        return $this->get(['id',$id]);
    }


    public function search( $from_date = NULL, $to_date = NULL, $order_by = NULL, $offset = NULL, $limit = NULL)
    {
        $sql = "SELECT * FROM sends WHERE id > 0";
        $execute_array = [];


        if (!is_null($from_date)) {
            $sql .= " AND created_time >= ?";
            $execute_array[] = $from_date;
        }

        if (!is_null($to_date)) {
            $sql .= " AND created_time <= ?";
            $execute_array[] = $to_date;
        }


        if (!is_null($order_by)) $sql .= " ORDER BY $order_by";
        if (!is_null($limit)) $sql .= " LIMIT $limit";
        if (!is_null($offset)) $sql .= " OFFSET $offset";

        $this->prepare($sql);
        if (count($execute_array) > 0) {
            $this->execute($execute_array);
        } else {
            $this->execute();
        }

        $results = $this->resultStr();
        if (count($results) > 0) {
            return $results;
        } else {
            return false;
        }
    }

    public function search_count( $from_date = NULL, $to_date = NULL)
    {
        $sql = "SELECT * FROM sends WHERE id > 0";
        $execute_array = [];


        if (!is_null($from_date)) {
            $sql .= " AND created_time >= ?";
            $execute_array[] = $from_date;
        }

        if (!is_null($to_date)) {
            $sql .= " AND created_time <= ?";
            $execute_array[] = $to_date;
        }


        $this->prepare($sql);
        if (count($execute_array) > 0) {
            $this->execute($execute_array);
        } else {
            $this->execute();
        }

        return $this->rowCount();
    }

}