<?php

class apiModel extends mainModel {

    protected $info = array();
    private $db;

    public function __construct() {
        $this->db = DBConnectorFactory::loadDriver();
    }

    public function setData($data) {
        $this->info = $data;
        return $this;
    }

    /**
     * @example asdasd descriptasddion
     * @return type
     */
    public function getProducts() {
        $data = array();
        $query_class = $this->db->query("SELECT `setting_value` FROM `tbl_default_settings` WHERE setting_name = 'pricing_class_id'");
        $class_id = $this->db->assoc($query_class)['setting_value'];
        $query = $this->db->query('select
                                `u`.`UOM_ID` AS `uom_id`,`c`.`Product_ID` AS `product_id`,
                                `c`.`Bar_Code` AS `bar_code`,`p`.`Product_Name` AS `product_name`,
                                `u`.`UOM_Name` AS `uom_name`
                                from `tbl_uom_conversions` `c` 
                                 join `tbl_stock_products` `p` 
                                 on`c`.`Product_ID` = `p`.`Product_ID`
                                 left join `tbl_stock_uom` `u`
                                 on`c`.`uom_from` = `u`.`UOM_ID`
                                 
                ');
        while ($row = $this->db->assoc($query)) {
            $row['product_price'] = Product::get_product_price($row['product_id'], $class_id, $row['uom_id']);
            $data[] = $row;
        }
        return $data;
    }

    public function login($user_name, $password) {
        $val = null;
        $pass = md5(sha1(md5($password)));
        $query_string = '
            SELECT
                    tbl_common_users.User_ID,
                    tbl_common_users.User_Name,
                    tbl_common_users.User_Password,
                    tbl_hrm_employee.Employee_FirstName AS User_FirstName,
                    tbl_hrm_employee.Employee_LastName AS User_LastName
                FROM
                    tbl_common_users
                INNER JOIN
                    tbl_hrm_employee
                ON
                    tbl_common_users.Employee_ID	=	tbl_hrm_employee.Employee_ID
                AND
                    BINARY tbl_common_users.User_Name		=	BINARY ?
                AND
                    BINARY tbl_common_users.User_Password	=	BINARY ?
                AND
                    tbl_common_users.Status_ID = 1
            ';
        $results = $this->db->preparedStmt($query_string, array(&$user_name, &$pass));
//        if (count($results) > 0) {
        if (!empty($results)) {
            $unique = false;
            $user_id = $results[0][0];
            while (!$unique) {
                $token = bin2hex(openssl_random_pseudo_bytes(16));
                $val = ["token" => $token, "id" => $user_id];
                $arr = [&$token, &$user_id];
                $this->db->prepare_Stmt("UPDATE 
                                            tbl_common_users
                                        SET
                                            token = ?
                                        WHERE
                                            User_ID = ?
                                        ", $arr);
                $error = $this->db->error();
                if (empty($error)) {
                    $unique = true;
                }
            }
        }
        return $val;
    }

    public function validateToken($token) {
        $query = $this->db->query('SELECT User_ID FROM tbl_common_users WHERE token = "' . $token . '"');
        $row = $this->db->assoc($query);
        if (!empty($row))
            return true;
        else
            return false;
    }

    public function addFile() {
        $mydate = date("Y-m-d h:i:s a");
//        echo appFiles.$this->info['file_name'];
//        die();
//        $path = .$this->info['file_name'];
        $data = [
            &$this->info['file_name'],
            &$this->info['type_id'],
            &$this->info['user_id'],
            &$mydate
        ];
//        echo "<pre>";
//       die(print_r($data));
        $this->db->prepare_Stmt("INSERT INTO
                                    tbl_app_file_main_data(
                                        file_name,
                                        type_id,
                                        Logged_ID,
                                        date_time
                                    )
                                    VALUES(
                                        ?,?,?,?
                                    )
                ", $data);
        $error = $this->db->error();
        return $error;
    }

    public function getStocks() {
        $data = array();
        $query = $this->db->query("SELECT Depot_ID AS id, Depot_Name as stock_name FROM tbl_stock_depot_warehouse");
        while ($row = $this->db->assoc($query))
            $data[] = $row;
        return $data;
    }

}
