<?php

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

    class Login extends CI_Controller {

        function __construct() {

            parent::__construct();
            if($this->session->lang != "" && $this->session->langfolder != ""){
                $this->lang->load($this->session->lang, $this->session->langfolder);
            }
        }

        function index()
        {
            if($this->session->user != ""){
                redirect('dashboard/welcome');
            }
                      $this->load->view('common/head');
              $this->load->view('login_view');
              $this->load->view('common/footer');

            // $this->load->view('login_view');
        }

        public function validate1()
        {
            $data = array(
                        'user' => $this->input->post('user'),
                        'logged_in' => true
                    );
                    $this->session->set_userdata($data);
                    redirect(base_url().'dashboard/welcome');
        }
        public function validate() {
            if ($this->session->userdata('logged_in') == true) die(redirect(base_url()));
            if ($this->input->post('login')) {
                $this->load->model('settings/users_model');
                if ($this->users_model->validateLogin($this->input->post('user'), $this->input->post('password'))) {
                    // $this->session->sess_destroy();
                    $loginDetails=$this->users_model->getLogindetails($this->input->post('user'), $this->input->post('password'));
                    $data = array(
                        'user' => $this->input->post('user'),
                        'userid' => $loginDetails->ID,
                        'logged_in' => true
                    );

                    $this->session->set_userdata($data);
                    if($this->validateLoginType($this->input->post('user')))
                        redirect(base_url().'pos/tables');
                   else
                   redirect(base_url().'dashboard/welcome');
                } else {
                    $this->session->set_flashdata('errormsg', '<font color="red"><p align="center">Incorrect Username or Password.</p></font>');
                    $this->session->set_flashdata('username_val', $this->input->post('user'));
                    $this->session->set_flashdata('password_val', $this->input->post('password'));
                    redirect(base_url() . 'login');
                }
            } else {
                $this->load->view('login');
            }
        }


            public function validateLoginType($userName) {

    //UPDATE QUANTITY IN XML       
            $xml = new DOMDocument('1.0');
            $xml->loadXml($this->readPermission($userName));
            $xml->saveXML();
    //SELECT ROOT TAG TO UPDATE 
            $Xpath = new DOMXPath($xml);
            $results = $Xpath->query('/');
            $baz_node_of_xml = $results->item(0);
            $root = $xml->documentElement;
    //         $status = $tickets->getElementsByTagName('webcontrollers')->item(0)->textContent;


            foreach ($results as $ticket) {
                $status = $ticket->getElementsByTagName('webcontrollers')->item(0)->textContent;

                if (strpos($status,'Tables') !== false) {
                    return true;

    }
            }
            return false;

        }

          function readPermission($userName) {

            $this->load->model('settings/users_model');
            $result = $this->users_model->getPermission($userName);

            if ($result) {
                return $content = $result[0]['PERMISSIONS'];

            }
            else
                return null;
        }

        public function logout() {
            $this->session->sess_destroy();
            redirect(base_url() . 'login');
        }

    }
