<?php
class Admin_LoginController extends Ctrl_Admin{
	public function IndexAction(){
		session_destroy();
		unset($_SESSION);
		$HospitalInfo = $this->HospitalInfo();
		$this->assign('HospitalInfo',$HospitalInfo);
	}
	public function LoginAction(){
        $tAMO = new AdminModel;
        $tURMO= new UserroteModel;
        $tARMO= new AdminroteModel;
        $tLMO = new LogModel;
        $pPasswd = empty($_REQUEST['password'])?Tool_Fnc::ajaxMsg('密码不能为空'):trim($_REQUEST['password']);
        $HospitalID = empty($_REQUEST['hospital'])?'0':trim($_REQUEST['hospital']);
        $pName = empty($_REQUEST['username'])?Tool_Fnc::ajaxMsg('用户名不能为空'):Tool_Fnc::safe_string($_REQUEST['username']);
       	$Sql = $Sql = "select * from ".$tAMO->table.' a left join '.$tURMO->table.' ur on ur.u_id=a.admin_id left join '.$tARMO->table.' ar on ar.admin_r_id=ur.r_id where a.hospital='.$HospitalID.' AND admin_username=\''.$pName.'\'';
       	$AdminInfo = $tAMO->query($Sql);
       	if (empty($AdminInfo)) {
       		Tool_Fnc::ajaxMsg('用户名错误');
       	}
       	$password = $AdminInfo[0]['admin_password'];
       	$QueryPwd = Tool_Fnc::markmd5($pPasswd,$AdminInfo[0]['admin_token']);
       	if ($password!=$QueryPwd) {
       		Tool_Fnc::ajaxMsg('密码错误');
       	}
        $array=array(
          'l_user'=>$AdminInfo[0]['admin_username'],
          'hospital'=>$AdminInfo[0]['hospital'],
          'l_created'=>time(),
          'l_ip'=>$_SERVER['REMOTE_ADDR'],
        );
        // print_r($array);die;
        $tLMO->insert($array);
       	$_SESSION['Admin'] = $AdminInfo[0]['admin_id'];
       	$_SESSION['UserName'] = $AdminInfo[0]['admin_username'];
       	$_SESSION['Name'] = $AdminInfo[0]['admin_name'];
       	$_SESSION['HospitalID'] = $AdminInfo[0]['hospital'];
       	$_SESSION['HospitalName'] = $AdminInfo[0]['hospital_name'];
       	$_SESSION['Rote'] = $AdminInfo[0]['admin_r_name'];
       	$_SESSION['RoteId'] = $AdminInfo[0]['admin_r_id'];
        Tool_Fnc::ajaxMsg('登陆成功',1);
        // $tRow = $tAMO->field('*')->where("admin_username = '{$pName}'")->fRow();
        // if(!count($tRow)){Tool_Fnc::ajaxMsg('用户不存在');}
        // if(Tool_Fnc::markmd5($pPasswd,$tRow['admin_token']) != $tRow['admin_password']){Tool_Fnc::ajaxMsg('密码不正确'); }
        // $_SESSION['admin'] = array('id'=> $tRow['admin_id'],'name' => $tRow['admin_username'],'roles' => $tRow['me_a_type'],'do_id' => $tRow['me_a_ip']);
        // setcookie('admin[roles]',$tRow['me_a_type'],time()+3600*24,'/');
        // setcookie('admin[id]',$tRow['me_a_id'],time()+60*60*24*365,'/');
        // setcookie('admin[name]',$tRow['me_a_username'],time()+60*60*24*365,'/');
	}


	/**
	 * [captchaAction 获取验证码]
	 * @return [type] [description]
	 */
	public function captchaAction(){
		$captcha = new Tool_Captcha(80, 35, 4);
		$_SESSION['captcha'] = $captcha;
		$captcha->showImg();
        exit;
	}

	/**
	 * [HospitalInfo 医院信息]
	 */
	public function HospitalInfo(){
		$tHMO = new HospitalModel;
		$HospitalInfo = $tHMO->field('hospital_id HospitalID,hospital_ip HospitalIP,hospital_name HospitalName,hospital_url HospitalUrl')->fList();
		return $HospitalInfo;	
	}
}