<?php
class web_functions extends object_class
{
    use common_function;
    private $webProduct_handle;

    function __construct()
    {
        parent::__construct('3');
    }


    public function ibms_setting($name)
    {
        return $this->functions->ibms_setting($name);
    }


    /**
     * @param $image
     * @param string $width
     * @param string $height
     * @param bool $echo
     * @param bool $cache
     * @param bool $imageWithWebUrl
     * @return string
     * Bing image to small, not small to big
     */
    public function resizeImage($image, $width = 'auto', $height = 'auto', $echo = true, $pngBgColor = false, $imageWithWebUrl = true, $cache = true)
    {
        return $this->functions->resizeImage($image, $width, $height, $echo, $pngBgColor, $imageWithWebUrl, $cache);
    }

    public function webUserId()
    {
        return webUserId();
    }

    public function userLoginCheck()
    {
        return userLoginCheck();
    }

    public function webUserType()
    {
        return webUserType();
    }

    public function userLogin($checkToken = true)
    {
        if (
            isset($_POST['email']) && !empty($_POST['email'])
            && isset($_POST['pass']) && !empty($_POST['pass'])
        ) {
            if ($checkToken == true) {
                if (!$this->functions->getFormToken('signInUser')) {
                    return false;
                }
            }

            $email = $_POST['email'];
            $pass = $_POST['pass'];
            $pass = $this->functions->encode($pass);
            $sql = "SELECT * FROM `accounts_user` WHERE acc_email = ? AND acc_pass = ?";
            $data = $this->dbF->getRow($sql, [$email, $pass]);

            if ($this->dbF->rowCount > 0) {
                $this->create_webLogin_session($data);

                $_SESSION['webUser']['remember'] = '0';
                if (isset($_POST['remember'])) {
                    $_SESSION['webUser']['remember'] = '1';
                    setRememberMe("webUser", $_SESSION['webUser'], 90);
                }

                $sqlUpdate = "UPDATE `accounts_user` SET `last_login` = ? WHERE `acc_email` = ?";
                $this->dbF->setRow($sqlUpdate, [date('Y-m-d H:i:s'), $email]);

                return true;
            } else {
                $var = 'Account Not Found Or Account Not Verify.';
                return $var;
            }
        }
    }


    public function create_webLogin_session($data)
    {
        $_SESSION['webUser']['login'] = '1';
        $_SESSION['webUser']['id'] = $data['acc_id'];
        $_SESSION['webUser']['oldTempId'] = $_SESSION['webUser']['tempId'];
        $_SESSION['webUser']['tempId'] = "";
        $_SESSION['webUser']['email'] = $data['acc_email'];
        $_SESSION['webUser']['name'] = $data['acc_name'];
        $_SESSION['webUser']['type'] = $data['acc_type'];

        $sql = "SELECT * FROM accounts_user_detail WHERE id_user = '$data[acc_id]' AND setting_name = 'user_type'";
        $data = $this->dbF->getRow($sql);
        if ($this->dbF->rowCount > 0) {
            $user_type = $data["setting_val"];
        } else {
            $user_type = false;
        }
        $_SESSION['webUser']['user_type'] = $user_type;
    }


    public function webUserInfoArray($data, $settingName)
    {
        foreach ($data as $val) {
            if ($val['setting_name'] == $settingName) {
                return $val['setting_val'];
            }
        }
        return "";
    }
}
