<?php

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

class Login extends Web_Controller {

    public function index() {
        // echo rand(1000, 9999);
        //  print_r($this->input->get());
        $data = array();
        $data['username'] = $data['pass'] = $data['error_message']='';

        if ($_POST) {

            $data['username'] = trim($this->input->post('username'));
            $data['pass'] = htmlspecialchars($this->input->post('pass'));
            //$this->load->view('provider/dash');
            $outData = array();
            //print_r($data);
            $loginResult = $this->verifyLoginInput($data); // initial test
            if ($loginResult == true) {

                $loginResult = false; // reset again for real login
                $loginResult = $this->loginUser($data, $outData);
                if (true == $loginResult) {
                    $out = array();
                    $this->load->model('patient_model');
                    $out = $this->patient_model->getPatientList();
                    $data["patient_list"] = $out["patient_list"];
                    $this->renderProviderSecurePage('dash', $data);
                }
            }// if valid input was supplied

            if (false == $loginResult) {
                $this->renderExternalPage('welcome_message', $data); // get here if login galis
            }
        } else {
            //$this->load->view('welcome_message');
            //  $this->load->view('provider/dash');
            $this->renderExternalPage('welcome_message', $data);
        }

        //$this->load->view('provider/dash');
        //$this->load->view('welcome_message');
    }

    private function verifyLoginInput(&$data) {

        $ret = false;
        if ($data['username'] == '' or $data['pass'] == '') {
            $data['error_message']="Username and password required";
        }


        if (trim($data['username']) != '' or trim($data['pass']) != '') {
            $ret = true;
        }

        return $ret;
    }

    public function logout() {
        $data = array();
        $this->destroySession();
        $this->renderExternalPage('welcome_message', $data);
    }

    private function destroySession() {
        
    }

}
