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

class cpanel_login
{

	function cpanel_login()
	{
		$this->obj =& get_instance();
	}

	function is_logged_in()
	{


		if ($this->obj->session) {

			//If user has valid session, and such is logged in
			if ($this->obj->session->userdata('logged_in')) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	function login_routine()
	{

		$password = $this->obj->input->post('userPassword');
		$username = $this->obj->input->post('userName');

		$login_result = 0;

		$query = $this->obj->db->query("SELECT * FROM resellers3 WHERE login = '$username' AND password = '$password' AND type%2=1");
		if($query->num_rows() > 0)
		 {
    		$row = $query->row();
    		$userID  =$row ->id;
    		$type  =$row ->type;
    		$id_tariff  =$row ->id_tariff;
    		$clientsLimit  =$row ->clientsLimit;
    		$login_result = 1;
    		$level = "R-III";
    		$levelNum = "3";
		 }
         else
         {

           	$query = $this->obj->db->query("SELECT * FROM resellers2 WHERE login = '$username' AND password = '$password' AND type%2=1");
            if($query->num_rows() > 0)
            {
            $row = $query->row();
			$userID  =$row ->id;
			$type  =$row ->type;
			$id_tariff  =$row ->id_tariff;
			$clientsLimit  =$row ->clientsLimit;
			$login_result = 1;
    		$level = "R-II";
    		$levelNum = "2";
    		}
    		else
    		{

				$query = $this->obj->db->query("SELECT * FROM resellers1 WHERE login = '$username' AND password = '$password' AND type%2=1");
				if($query->num_rows() > 0)
				{
				$row = $query->row();
				$userID  =$row ->id;
				$type  =$row ->type;
				$id_tariff  =$row ->id_tariff;
				$clientsLimit  =$row ->clientsLimit;
				$login_result = 1;
				$level = "R-I";
				$levelNum = "1";
				}


    		}

         }


        $GW_Client = 0;
        $PC2Phone = 0;
        $GK_Client = 0;
        $Call_Back_Clients = 0;
        $IVR_Clients = 0;
        $Common_Clients = 0;
        $Call_Shop_Client = 0;
        $disTariffAddSession = 0;

        if($login_result==1)
        {
           $itemValue = array(3, 5, 9, 17, 33, 65, 129, 257, 513, 1025);
		   $permission = $this->obj->generallib->outputTheArray($type);
		   foreach($permission as $key => $itemName)
           {
              $typeValue = $itemValue[$key];
              if($typeValue==5)
              {
                 $GW_Client = 1;
                 //$_SESSION["GW_Client"] = 1;
              }

              if($typeValue==9)
              {
                 $PC2Phone = 1;
                 //$_SESSION["PC2Phone"] = 1;
              }


              if($typeValue==17)
              {
                 $GK_Client = 1;
                 //$_SESSION["GK_Client"] = 1;
              }


              if($typeValue==33)
              {
                 $Call_Back_Clients = 1;
                 //$_SESSION["Call_Back_Clients"] = 1;
              }


              if($typeValue==65)
              {
                 $IVR_Clients = 1;
                 //$_SESSION["IVR_Clients"] = 1;
              }


              if($typeValue==129)
              {
                 $Common_Clients = 1;
                 //$_SESSION["Common_Clients"] = 1;
              }


              if($typeValue==257)
              {
                 $Call_Shop_Client = 1;
                 //$_SESSION["Call_Shop_Client"] = 1;
              }


              if($typeValue==513)
              {
                 $disTariffAddSession = 1;
                 //$_SESSION["Call_Shop_Client"] = 1;
              }


           }


        }







		//If username and password match set the a logged in flag in 'ci_sessions'
		if ($login_result==1) {

		    // Add User Log //

		    $this->obj->generallib->addUserLog($userID,$levelNum,'Login','Reseller Login');

			$credentials = array('user_name' => $username, 'logged_in' => $login_result,'userID'=>$userID,'level'=>$level,'type'=>$type,'id_tariff'=>$id_tariff,'GW_Client'=>$GW_Client,'PC2Phone'=>$PC2Phone,'GK_Client'=>$GK_Client,'Call_Back_Clients'=>$Call_Back_Clients,'IVR_Clients'=>$IVR_Clients,'Common_Clients'=>$Common_Clients,'Call_Shop_Client'=>$Call_Shop_Client,'clientsLimit'=>$clientsLimit,'disTariffAddSession'=>$disTariffAddSession);

			$this->obj->session->set_userdata($credentials);
			redirect('admin/cpanel/','location'); //On success redirect user to default page

		} else {
			//On error send user back to login page, and add error message
			redirect('login/login_fail', 'location'); //On success redirect user to default page
		}
	}



	function get_userID()
	{
	//$this->obj->load->library('session');
		//	$this->obj->load->helper('url');

			$this->obj->load->database();

			if ($this->obj->session) {
				return $this->obj->session->userdata('userID');

			}
	}




	function getUserName()
	{
	//$this->obj->load->library('session');
		//	$this->obj->load->helper('url');

			$this->obj->load->database();

			if ($this->obj->session) {
				return $this->obj->session->userdata('user_name');

			}
	}



	function getUserLevel()
	{
	//$this->obj->load->library('session');
		//	$this->obj->load->helper('url');

			$this->obj->load->database();

			if ($this->obj->session) {
				return $this->obj->session->userdata('level');

			}
	}




	function getUserType()
	{
	//$this->obj->load->library('session');
		//	$this->obj->load->helper('url');

			$this->obj->load->database();

			if ($this->obj->session) {
				return $this->obj->session->userdata('type');

			}
	}




	function getUserBaseTariffID()
	{
	//$this->obj->load->library('session');
		//	$this->obj->load->helper('url');

			$this->obj->load->database();

			if ($this->obj->session) {
				return $this->obj->session->userdata('id_tariff');

			}
	}



	function getGW_Client()
	{
	//$this->obj->load->library('session');
		//	$this->obj->load->helper('url');

			$this->obj->load->database();

			if ($this->obj->session) {
				return $this->obj->session->userdata('GW_Client');

			}
	}








	function get_logID()
	{
	//$this->obj->load->library('session');
		//	$this->obj->load->helper('url');

			$this->obj->load->database();

			if ($this->obj->session) {
				return $this->obj->session->userdata('log_id');

			}
	}




	function logout()
	{
		//$this->obj->load->library('session');
		$this->obj->session->destroy();
		redirect('login/logout', 'location');
	}





	function add_login_time($user_id)
	{

	$data['log_id'] = "";
	$data['user_id'] = $user_id;
	$data['login_time'] = date("g:i a");                 ;
	$data['login_dt'] = date('Y-m-d');
	$data['logout_time'] = "";
	$data['logout_dt'] = "";
	$sql = $this->obj->db->insert_string('login', $data);
	$query = $this->obj->db->query($sql);
	return $this->obj->db->insert_id();
	}








}
?>