<?php


namespace CUSTOMER;

class CUSTOMER{

    private $customer_id;
    private $name;
    private $phone;
    private $lastname;
    private $tip;
    private $email;
    private $sesion;
    private $db;
    private $permission = array();

    public function __construct($db) {
        @session_start();
        $this->db = $db;
        //print_r($_SESSION);die();
        if (isset($_SESSION['customer_id'])) {
            $user_query = $db->query("SELECT * FROM " . DBPREFIX . "customer WHERE id = '" . (int)$_SESSION['customer_id'] . "' AND status = '1'");

            if ($user_query->num_rows) {
                $this->customer_id = $user_query->row['id'];
                $this->name = $user_query->row['name'];
                $this->email = $user_query->row['email'];
                $this->phone = $user_query->row['phone'];
                /*
                                $this->db->query("UPDATE " . DBPREFIX . "user SET ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "' WHERE user_id = '" . (int)$this->session->data['user_id'] . "'");

                                $user_group_query = $this->db->query("SELECT permission FROM " . DBPREFIX . "user_group WHERE user_group_id = '" . (int)$user_query->row['user_group_id'] . "'");

                                $permissions = json_decode($user_group_query->row['permission'], true);

                                if (is_array($permissions)) {
                                    foreach ($permissions as $key => $value) {
                                        $this->permission[$key] = $value;
                                    }
                                }
                */
            } else {
                $this->logout();
            }
        }
    }

    public function login($username, $password) {
        $user_query = $this->db->query("SELECT * FROM " . DBPREFIX . "customer WHERE email = '" . $this->db->escape($username) . "' AND password = '" . $this->db->escape(md5($password)) . "' AND status = '1'");
//print_r($user_query);die();
        if ($user_query->num_rows) {
            $_SESSION['customer_id'] = $user_query->row['id'];
            //print_r($_SESSION);die();
            $this->customer_id = $user_query->row['id'];
            $this->name = $user_query->row['name'];
            $this->email = $user_query->row['email'];
            $this->phone = $user_query->row['phone'];
            //print_r( $this->user_id);die();
            /* $this->user_group_id = $user_query->row['user_group_id'];

             $user_group_query = $this->db->query("SELECT permission FROM " . DBPREFIX . "user_group WHERE user_group_id = '" . (int)$user_query->row['user_group_id'] . "'");

             $permissions = json_decode($user_group_query->row['permission'], true);

             if (is_array($permissions)) {
                 foreach ($permissions as $key => $value) {
                     $this->permission[$key] = $value;
                 }
             }
             */
            return true;
        } else {
            return false;
        }
    }

    public function logout() {

        unset($_SESSION['customer_id']);

        $this->customer_id = '';
        $this->name = '';

    }

    public function hasPermission($key, $value) {
        if (isset($this->permission[$key])) {
            return in_array($value, $this->permission[$key]);
        } else {
            return false;
        }
    }

    public function isLogged() {

        return $this->customer_id;

    }

    public function getId() {
        return $this->customer_id;
    }

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

    public function getEmail() {
        return $this->email;
    }

    public function getPhone() {
        return $this->phone;
    }

  
    public function __destruct()
    {

        // TODO: Implement __destruct() method.
    }

}