<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Site extends MDT_Controller {

    public function index() {

        $data['action_message'] = '';

        if ($this->input->post()) {
            $valid_entry = false;
            $username = $password = $error_message = '';
            $this->testLoginInput($username, $password, $error_message, $valid_entry);
            //  echo $valid_entry;
            if ($valid_entry == true) {
                $in['username'] = $username;
                $in['password'] = $password;
                $in['action'] = MOBIDELIV_BKO_LOGIN;
                $out = array();
                $ret = $this->medtrans_api($in, $out);
//print_r($out);
//exit();
                if ($ret == PHP_API_OK) {
                    //$out =  $this->dummyTestOut(); // just for testing - will be removed
                    $this->buildUserSession($ret, $out);
                    redirect('dash');
                } else {
                    $data['action_message'] = $this->formatedMesage('ERROR', 'Invalid Username/Password');
                }
            } else {

                $data['action_message'] = $this->formatedMesage('ERROR', $error_message);
            }
        }
        //  echo rand(100,999);
        $this->load->view('home/view_home', $data);
    }

    public function logout() {
        
        $data = array();
        $data['action_message'] = '';
        $_SESSION['session_id'] = $_SESSION['sessionid'] = $_SESSION['username'] = $_SESSION['backoffice_id'] = $_SESSION['username'] ='';
        unset($_SESSION);
        redirect('site');
      //  $this->load->view('home/view_home', $data);
    }

    private function testLoginInput(&$username, &$password, &$error_message, &$valid_entry) {
        $valid_entry = true;
        $username = trim($this->input->post('username'));
        $password = trim($this->input->post('password'));
        if ($username == '' or $password == '') {
            $valid_entry = false;
            $error_message = 'Enter a Username/Password to continue';
        }
    }

    private function buildUserSession($ret, $out) {

        if ($ret == PHP_API_OK) {
            $_SESSION['session_id'] = $out["sessionid"];
            $_SESSION['sessionid'] = $out["sessionid"];
            $_SESSION['username'] = $out["username"]; // $this->input->post('username');          
            $_SESSION['firstname'] = $out["firstname"]; //  $ret->firstname;
            $_SESSION['lastname'] = $out["lastname"]; //  $ret->lastname;
            $_SESSION['email'] = $out["email"]; //  $ret->email;
            $_SESSION['backoffice_id'] = $out["username"]; // $ret->id;
            $_SESSION['pid'] = $out["pid"]; // $ret->id;
            $_SESSION['loc'] = $out["loc"];
        }
    }

    private function dummyTestOut() {

        $out["added"] = "2017-08-09 14:49:16.995988";
        $out["backoffice_id"] = "1";
        $out["email"] = "ameye@chiefsoft.com";
        $out["firstname"] = "Mobideliv";
        $out["id"] = "1";
        $out["last_login"] = "2017-08-09 14:49:16.995988";
        $out["lastname"] = "Administartor";
        $out["loc"] = "192.168.2.200";
        $out["password"] = "f7099e89fa387d6f2a7a7eab72a3e7f0";
        $out["pid"] = "100";
        $out["plevel"] = "100";
        $out["result"] = "YES I GET TO BACK END";
        $out["sessionid"] = "9a40118da2fae97b33eabdca064072bf";
        $out["status"] = "1";
        $out["stauts"] = "OK";
        $out["username"] = "mobideliv";
        return $out;
    }
    
}

//$something = $this->input->post('something');
