<?php
require_once (PATH_CONTROLLERS . "Common.php") ;
require_once (PATH_MODELS . "admin/UserGroupClass.php") ;
require_once (PATH_MODELS . "admin/UserRight.php") ;
// require_once (PATH_MODELS . "admin/OrganizationOptions.php") ;
// require_once (PATH_MODELS . "admin/OrganizationClass.php") ;
// require_once (PATH_MODELS . "hr/EmployeePasswordClass.php") ;
// require_once (PATH_MODELS . "hr/EmployeeClass.php") ;
require_once (PATH_LIB . "LicenseFile.php") ;

class Main extends ControllerBase {
	private $clsgroup ;
	private $clsright ;
	private $clsorg ;

	function __construct() {
		$this->db = $_SESSION[SE_DB] ;
		$this->clsgroup = new UserGroupClass($this->db) ;
		$this->clsright = new UserRight() ;
		$this->clsorg = new OrganizationClass($this->db) ;
	}
	function __destruct() {
		unset($this->db) ;
		unset($this->clsgroup) ;
		unset($this->clsright) ;
		unset($this->clsorg) ;
	}
	public function processRequest($params) {
		try {
			$this->db->open() ;
			$this->loadSetting() ;
			$this->processView() ;
			$this->db->close() ;
			return true ;
		} catch (Exception $e) {
			$this->db->close() ;
			die ($e->getMessage()) ;
		}
	}
	private function processView() {
		ob_start() ;
		$this->clsright->initRights() ;
		if ($_SESSION[SE_USERGROUP] != "")
		{
			$row = $this->clsgroup->getRecord($_SESSION[SE_USERGROUP]) ;
			if (!is_null($row)) {
				if (!is_null($row[UserGroupTable::C_RIGHTS]))
					$this->clsright->toRights($row[UserGroupTable::C_RIGHTS]); ;
			}
		}
		include (PATH_VIEWS . "web/general/MainView.php") ;
		echo Util::minifyHtml(ob_get_clean()) ;
	}
	private function getTheme() {
		if (isset($_SESSION[SE_THEME]) && $_SESSION[SE_THEME] != "")
			return $_SESSION[SE_THEME] ;
		else
			return "redmond" ;
	}
	private function getOrganizationName() {
		return $_SESSION[SE_ORGNAME] ;
	}
	private function getFullName() {
		return $_SESSION[SE_FULLNAME] ;
	}
	private function loadSetting() {
		// $row = $this->clsorg->getRecord($_SESSION[SE_ORGID]) ;
		
		// if (!is_null($row)) {
		// 	$opt = new OrganizationOptions() ;
		// 	$opt->loadXml($row[OrganizationTable::C_OPTIONS]) ;
		// 	$op = $opt->getOption() ;
		// 	$_SESSION[SE_ORGNAME] = $op[OrganizationOptions::C_CONTACT][OrganizationOptions::C_CONT_NAME1] ;
		// 	$_SESSION[SE_ORGCODE] = $row[OrganizationTable::C_CODE] ;
		// 	unset($opt) ;
		// }
		// else {
		// 	$_SESSION[SE_ORGNAME] = "default company" ;
		// 	$_SESSION[SE_ORGCODE] = "" ;
		// }
		// unset($row) ;
	}
	private function loadCompany_unuse() {
		$arr = array() ;
		$arr[] = array ('code'=>'','desc'=>'--- Select a Company ---' ) ;
		$arr[] = array ('code'=>'sbg','desc'=>'Strategic Business Group' );
		$arr[] = array ('code'=>'pzs','desc'=>'PlanetZ System' ) ;
		return Util::createOptionValue($arr) ;
	}
	private function loadNews() {
		$c = "<div>1 company news to be place at the right of the home page.</div>" ;
		$c .= "<div>2 company news to be place at the right of the home page.</div>" ;
		$c .= "<div>3 company news to be place at the right of the home page.</div>" ;
		
		return "" ;
	}
	private function loadProfile() {
		$c = "<div>Administrator</div>" ;
		$c .= "<div>Last Login</div>" ;
		$c .= "<div><a href=\"javascript:changePwd();\">Change Password</a></div>" ;
		$c .= "<div><a href=\"javascript:changeProfile();\">Update Profile</a></div>" ;
		return $c ;
	}
	private function loadMessage() {
		$c = "<div>No Message</div>" ;
		$c .= "<div>2 message</div>" ;
		return "" ;
	}
	private function loadFooter() {
		$c = "<div>copyright info</div>";
		return $c ;
	}

	private function getUserLogout() {
		$c = "" ;
		$c .= '<li class="m-nav__separator m-nav__separator--fit"></li>
																<li class="m-nav__item">
																	<a href="'.$this->createJsFunc("onLogout()").'" class="btn m-btn--pill    btn-secondary m-btn m-btn--custom m-btn--label-brand m-btn--bolder">
																	<span>Logout</span>
																		
																	</a>
																</li>
											';
		return $c ;


	}

	private function getUserProfile() {
		$c = "" ;
		$c .= '<li class="m-nav__item">
					<a href="'.$this->createMenuFunc("ChangePwd","Change Password").'" class="m-nav__link">
						<i class="m-nav__link-icon flaticon-profile-1"></i>
						<span class="m-nav__link-title">
							<span class="m-nav__link-wrap">
								<span class="m-nav__link-text">
									My Profile
								</span>	
							</span>
						</span>
					</a>
				</li>';
		return $c ;

	
	}

	private function createMenuAdmin() {
		$c = "";
		$submenu = "";
		$right = $this->clsright->getAdminRight() ;
		// if ($right[UserRight::C_ADMIN_COY][UserRight::C_ENABLE])
		// $submenu .= $this->createMenuFuncFull("Company", "Company Profile");

		//if ($right[UserRight::C_ADMIN_SETTING][UserRight::C_ENABLE])
		   $submenu .= $this->createMenuFuncFull("Setting", "System Setting");
		   	
		// if ($right[UserRight::C_ADMIN_BU][UserRight::C_ENABLE]) 										
			$submenu .= $this->createMenuFuncFull("BusinessUnit", "Business Unit");		

		// if ($right[UserRight::C_ADMIN_USER][UserRight::C_ENABLE]) 
			$submenu .= $this->createMenuFuncFull("Users", "User Profile");													
					
		// if ($right[UserRight::C_ADMIN_GROUP][UserRight::C_ENABLE])
			$submenu .= $this->createMenuFuncFull("UserGroup", "User Group");							
					
		// if ($right[UserRight::C_ADMIN_RESET][UserRight::C_ENABLE])
			$submenu .= $this->createMenuFuncFull("ResetPwd", "Reset Password");								

		// if (
			//$right[UserRight::C_ADMIN_COY][UserRight::C_ENABLE] or
			//$right[UserRight::C_ADMIN_SETTING][UserRight::C_ENABLE] or
			// $right[UserRight::C_ADMIN_USER][UserRight::C_ENABLE] or
			// $right[UserRight::C_ADMIN_GROUP][UserRight::C_ENABLE] or
			// $right[UserRight::C_ADMIN_BU][UserRight::C_ENABLE] or
			// $right[UserRight::C_ADMIN_RESET][UserRight::C_ENABLE]){								
		$c  = '<li class="m-menu__item m-menu__item--submenu" aria-haspopup="true" data-menu-submenu-toggle="hover">
				<a href="#" class="m-menu__link m-menu__toggle">
					<span class="m-menu__item-here"></span>
					<i class="m-menu__link-icon flaticon-open-box"></i>
					<span class="m-menu__link-text">
						Admin
					</span>
					<i class="m-menu__ver-arrow la la-angle-right"></i>
				</a>
				<div class="m-menu__submenu ">
					<span class="m-menu__arrow"></span>
					<ul class="m-menu__subnav">
						<li class="m-menu__item  m-menu__item--parent" aria-haspopup="true">
							<span class="m-menu__link">
								<span class="m-menu__item-here"></span>
								<span class="m-menu__link-text">
									Admin
								</span>
							</span>
						</li>
						'.$submenu.'
					</ul>
				</div>
			</li>';
		// }

		return $c ;
	}	
	private function gettimeconvert($minute){
		
		if ($minute<= 60){
			return ($minute)." minute ago";	
		}else if($minute>=60 && $minute<=1440){
			$minute = $minute/60;
			return round($minute)." hour(s) ago";
		}else if ($minute>=1440 && $minute<=43800){
			$minute = $minute/1440;
			return round($minute)." day(s) ago";
		}else if ($minute>=1440 && $minute<=525600){
			$minute = $minute/43800;
			return round($minute)." month(s) ago";
		}else if ($minute>=525600){
			$minute = $minute/525600;
			return round($minute)." year(s) ago";
		}
		 
	}
	
	private function createTopItem($imgfile,$title,$link="",$caption="") {
		$c = "" ;
		$c .= "<a title=\"" . $title . " \" href=\"" . $link . "\">";
		if ($imgfile != "") {
			$c .= "<img src=\"image/" . $imgfile . "\"></img>" ;
		}
		$c .= "</a>" ;
		return $c ;
	}
	private function createMenuFunc($href,$desc) {
		return "javascript:showPage('" . Util::convertLink($href) . "','". $desc . "')" ;
	}
	private function createMenuFuncFull($href,$desc) {
		$ret = '<li class="m-menu__item " aria-haspopup="true" >
					<a href="' .$this->createMenuFunc($href,$desc) . '"class="m-menu__link ">
						<i class="m-menu__link-bullet m-menu__link-bullet--dot"><span></span></i>
						<span class="m-menu__link-text">' . $desc . '</span>
					</a>
				</li>';	
		return $ret ;
	}

	private function createJsFunc($func) {
		return "javascript:" . $func ;
	}

}
?>