<?php

namespace FAAPI;

$path_to_root = "../..";

class Users {

    //Get Items
    public function get($rest) {
        $req = $rest->request();

        $page = $req->get("page");

        if ($page == null) {
            $this->users_all();
        } else {
            // If page = 1 the value will be 0, if page = 2 the value will be 1, ...
            $from = --$page * RESULTS_PER_PAGE;
            $this->customer_all($from);
        }
    }

    // Get Specific User by Id
    public function getLoggedUser($rest, $id) {
        $req = $rest->request();
        $company = $req->headers('X-COMPANY');
        $user = $req->headers('X-USER');
        $password = $req->headers('X-PASSWORD');

        // TESTING
         //$company = 0;
          //$user = 'admin';
          //$password = '1234'; 

        $succeed = $_SESSION["wa_current_user"]->login($company, $user, $password);
        if (!$succeed) {
            api_error(403, 'Bad Login For Company: ' . $company . ' With User: ' . $user);
        } else {
            
            $info = get_user($_SESSION["wa_current_user"]->user);
            api_success_response(json_encode($info));
        }
    }

    public function getById($rest, $id) {
        $info = get_user($id);
        api_success_response(json_encode($info));
    }

    private function users_all($from = null) {
        if ($from == null)
            $from = 0;

        $sql = "SELECT * FROM " . TB_PREF . "users WHERE !inactive LIMIT " . $from . ", " . RESULTS_PER_PAGE;

        $query = db_query($sql, "error");

        $info = array();

        while ($data = db_fetch_assoc($query, "error")) {
            $info[] = $data;
        }

        api_success_response(json_encode($info));
    }

    // Add Item
    public function post($rest) {
        $req = $rest->request();
        $info = $req->post();
        $new = TRUE;
        $selected_id = -1;
        if(!empty($info['id'])){
            $new = FALSE;
            $selected_id = $info['id'];
        }

        // Validate Required Fields
        if (strlen($info['user_id']) < 4) {
            api_error(200, 'The user login entered must be at least 4 characters long [user_id]');
        }
        
        if (check_userid_exist($info['user_id'])) {
            api_error(200, 'The user login already exist. Please login to continue [user_id]');
        }

        if (!$new && ($info['password'] != "")) {
            if (strlen($info['password']) < 4) {
                api_error(200, 'The user password entered must be at least 4 characters long [password]');
            }

            if (strstr($info['password'], $info['user_id']) != false) {
                api_error(200, 'The user password entered must be at least 4 characters long [password]');
            }
        }

        $id = null;
        if ($selected_id != -1) {
            update_user_prefs($selected_id, array('user_id'=>$info[''], 'real_name'=>$info[''], 'phone'=>$info[''], 'email'=>$info[''], 'role_id'=>$info[''], 'language'=>"C",
                'print_profile'=>"", 'rep_popup' => 1, 'pos'=>1));

            if ($info['password'] != "")
                update_user_password($selected_id, $info['user_id'], md5($info['password']));
            $id = $selected_id;
        } else {
            add_user($info['user_id'], $info['real_name'], md5($info['password']), $info['phone'], $info['email'], $info['role_id'], 'C', "", 1, 1);
            $id = db_insert_id();
            // use current user display preferences as start point for new user
            $prefs = $_SESSION['wa_current_user']->prefs->get_all();

            update_user_prefs($id, array_merge($prefs, (array('print_profile'=>1,
                'rep_popup' => 0, 'language'=>'C'))));
        }

        $cust = get_user($id);
        if ($cust['user_id'] != null) {
            api_success_response(json_encode($cust));
        } else {
            api_error(500, 'Could Not Save to Database');
        }
    }

}
