<?php

class smc extends mainController {

    public function __construct() {
        parent::__construct(false);
        $this->load->model('auth');
        $this->modal = new authModel();
    }

    public function landing() {
        $vars = array();
        $vars['companies'] = $this->modal->get_companies();
        $vars['is_production'] = $this->modal->get_is_production();
        $vars['remain_expiry_date'] = $this->modal->get_remain_expiry_date();
        $vars['is_pin_code'] = $this->modal->get_is_pin_code_login();
        $vars['open_product_page'] = $this->modal->open_product_page();
        $vars['open_customer_points_page'] = $this->modal->open_customer_points_page();

        $vars['expiry_message'] = '';
        $vars['unique_id'] = token_unique::generate_token('login');
        if (is_numeric($vars['remain_expiry_date'])) {
            if ($vars['remain_expiry_date'] >= 0)
                $vars['expiry_message'] = "Your license will expire in {$vars['remain_expiry_date']} days.";
            else
                $vars['expiry_message'] = "Your license already expired.";
            $vars['expiry_message'] .= "<br/>Please contact <a class='company_link' href='http://www.trustotech.com/' target='_blank'>Trusto Tech</a>";
        }
        $this->load->view('login', $vars, true);
    }

    public function doLogin() {
        if (empty($_POST)) {
            header('Location: ' . SITE_LINK . 'smc?errorcode=1');
            exit;
        }
        $error = token_unique::is_valid_token();
        if (!empty($error)) {
            header('Location: ' . SITE_LINK . 'smc?errorcode=duplicate');
            exit;
        }
        $logged_id = Session::getUserID();
        session_start();
        $_SESSION['getUserID'] =Session::getUserID();

        $vars['pageName'] = 'Login';
        $vars['error'] = false;
        $vars['title'] = 'Please wait...';
        $vars['statusCode'] = $this->_registry->person->setLoginInformation($_POST)->login();
        token_unique::remove_token();
        $vars['login'] = $this->_registry->person->redirectFromStatusCode($vars['statusCode']);
    }

    public function doLogout($id = null) {
        $this->_registry->person->logout($id);
    }

    public function dologinpos() {
        if (!empty($_POST['secret_code']))
            $vars['statusCode'] = $this->_registry->person->setLoginPOSInformation($_POST)->login(true);
        else
            $vars['statusCode'] = 3;
        //
        $vars['login'] = $this->_registry->person->redirectFromStatusCodePOS($vars['statusCode']);
    }

}
