<?php
class Admin_AdminController extends Ctrl_Admin{
	public $HospitalId;
	public function init(){
		$this->HospitalId = empty($_SESSION['HospitalID'])?'1':$_SESSION['HospitalID'];
	}
	public function IndexAction(){
		if (!isset($_SESSION['Admin'])) {
           	$this->redirect('/Admin_Login/Index');
        }
		$tAMMO = new AdminmenuModel;
		$tARMO = new AdminroteModel;
		$tRMMO = new RotemenuModel;
		$HospitalID = $_SESSION['HospitalID'];								//获取医院ID
		$HospitalName=$_SESSION['HospitalName'];							//获取医院名称
		$Name=$_SESSION['Name'];											//获取用户名
		$RoteId = $_SESSION['RoteId'];										//获取权限ID
		$Rote = $_SESSION['Rote'];										    //获取权限ID
		$sSql = 'select * from '.$tAMMO->table.' am right join '.$tRMMO->table.' rm on rm.m_id=am.admin_m_id right join '.$tARMO->table.' ar on ar.admin_r_id=rm.r_id where admin_father_id=0 AND ar.admin_r_id='.$RoteId;
		$aSql = 'select * from '.$tAMMO->table.' am right join '.$tRMMO->table.' rm on rm.m_id=am.admin_m_id right join '.$tARMO->table.' ar on ar.admin_r_id=rm.r_id where admin_father_id!=0 AND ar.admin_r_id='.$RoteId;
		if (empty($_SESSION['HospitalID'])) {
			$sSql = 'select * from '.$tAMMO->table.' where admin_father_id=0 ';
			$aSql = 'select * from '.$tAMMO->table.' where admin_father_id!=0 ';
		}
		$MenuFAInfo = $tAMMO->query($sSql);
		$MenuCHInfo = $tAMMO->query($aSql);
		foreach ($MenuFAInfo as $key => $value) {
			foreach ($MenuCHInfo as $k => $va) {
				if ($va['admin_father_id']==$value['admin_m_id']) {
					$MenuFAInfo[$key]['children'][] = $va;
				}
			}
		}
		$this->assign('MenuFAInfo',$MenuFAInfo);
		$this->assign('Name',$Name);
		$this->assign('Rote',$Rote);
		$this->assign('HospitalName',$HospitalName);
		$this->assign('HospitalID',$HospitalID);
	}


	/**
	 * [AdminIndexAction 账号管理首页]
	 */
	public function AdminIndexAction(){
		$tAMO = new AdminModel;
		$tARMO = new AdminroteModel;
		$tURMO = new UserroteModel;
		$HospitalID = empty($_REQUEST['hospital'])?$this->HospitalId:$_REQUEST['hospital'];
		$HospitalInfo = $this->HospitalInfo();
		$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;
		$AdminInfo = $tAMO->query($Sql);
		$this->assign('HospitalInfo',$HospitalInfo);
		$this->assign('HospitalID',$HospitalID);
		$this->assign('AdminInfo',$AdminInfo);
	}



	/**
	 * [AddAction 用户添加页面]
	 */
	public function AddAction(){
		$tARMO = new AdminroteModel;
		$HospitalID = empty($_REQUEST['hospital'])?$this->HospitalId:$_REQUEST['hospital'];										//接受医院ID
		$HospitalInfo = $this->HospitalInfo();																		//获取医院信息
		$RoteInfo = $tARMO->field('*')->where('hospital='.$HospitalID)->fList();									//查询当前医院角色信息
		$this->assign('HospitalID',$HospitalID);
		$this->assign('RoteInfo',$RoteInfo);
	}


	/**
	 * [AdminAddAction 添加用户操作]
	 */
	public function AdminAddAction(){
		$tAMO = new AdminModel;
		$tURMO = new UserroteModel;
		$HospitalID = $_REQUEST['hospital'];
		$AdminPassword = empty($_REQUEST['AdminPassword'])?Tool_Fnc::ajaxMsg('请您填写密码'):Tool_fnc::safe_string($_REQUEST['AdminPassword']);
		$AdminName = empty($_REQUEST['AdminName'])?Tool_Fnc::ajaxMsg('请您填写姓名'):Tool_fnc::safe_string($_REQUEST['AdminName']);
		$AdminUsername = empty($_REQUEST['AdminUsername'])?Tool_Fnc::ajaxMsg('请您填写账号'):Tool_fnc::safe_string($_REQUEST['AdminUsername']);
		$AdminPasswordsure = empty($_REQUEST['AdminPasswordsure'])?Tool_Fnc::ajaxMsg('请您确认密码'):Tool_fnc::safe_string($_REQUEST['AdminPasswordsure']);
		$AdminRote = empty($_REQUEST['AdminRote'])?Tool_Fnc::ajaxMsg('请您选择角色'):Tool_fnc::safe_string($_REQUEST['AdminRote']);
		if ($AdminPassword!=$AdminPasswordsure) {
			Tool_Fnc::ajaxMsg('两次密码不一致');
		}
        $token = Tool_Fnc::newrand();
        $AdminPassword = Tool_Fnc::markmd5($AdminPassword,$token);
        $AdminArray = $tAMO->field('*')->where('admin_name=\''.$AdminName.'\' AND hospital='.$HospitalID)->fRow();
        if (!empty($AdminArray)) {
        	Tool_Fnc::ajaxMsg('账号已经添加，请勿重复添加');
        }
        $Admin = array(
        	'admin_name'    => $AdminName,
        	'admin_username'=> $AdminUsername,
        	'admin_password'=> $AdminPassword,
        	'admin_created' => time(),
        	'hospital'      => $HospitalID,
        	'hospital_name' => '',
        	'admin_status'  => 1,
        	'admin_token'   => $token,
        	'admin_pwd'     => $AdminPasswordsure,
        );

        $tAMO->begin();
        if (!$Aid = $tAMO->insert($Admin)) {
        	$tAMO->back();
        	Tool_Fnc::ajaxMsg('系统繁忙请稍后再试！');
        }
        $Rote = array(
        	'u_id' => $Aid,
        	'r_id' => $AdminRote,
        );
        if (!$tURMO->insert($Rote)) {
        	$tAMO->back();
        	Tool_Fnc::ajaxMsg('系统繁忙请稍后再试！');
        }
       	$tAMO->commit();
        Tool_Fnc::ajaxMsg('添加成功',1);

	}


	/**
	 * [EditAction 账号编辑页面]
	 */
	public function EditAction(){
		$tAMO = new AdminModel;
		$tURMO = new UserroteModel;
		$tARMO = new AdminroteModel;
		$AdminId = $_REQUEST['AdminId'];
		$ARID = $_REQUEST['ARID'];
		$HospitalID = empty($_REQUEST['hospital'])?$this->HospitalId:$_REQUEST['hospital'];										//接受医院ID
		$RoteInfo = $tARMO->field('*')->where('hospital='.$HospitalID)->fList();									//查询当前医院角色信息
		$Sql = "select * from ".$tAMO->table.' a left join '.$tURMO->table.' ur on ur.u_id=a.admin_id where a.admin_id='.$AdminId;
		$AdminInfo = $tAMO->query($Sql);

		$this->assign('AdminInfo',$AdminInfo);
		$this->assign('HospitalID',$HospitalID);
		$this->assign('ARID',$ARID);
		$this->assign('RoteInfo',$RoteInfo);
	}


	/**
	 * [AdminEditAction 账号修改操作]
	 */
	public function AdminEditAction(){
		$tAMO = new AdminModel;
		$tURMO = new UserroteModel;
		$HospitalID = $_REQUEST['hospital'];
		$ARID = $_REQUEST['ARID'];
		$AdminId = $_REQUEST['AdminId'];
		$AdminPassword = empty($_REQUEST['AdminPassword'])?Tool_Fnc::ajaxMsg('请您填写密码'):Tool_fnc::safe_string($_REQUEST['AdminPassword']);
		$AdminName = empty($_REQUEST['AdminName'])?Tool_Fnc::ajaxMsg('请您填写姓名'):Tool_fnc::safe_string($_REQUEST['AdminName']);
		$AdminUsername = empty($_REQUEST['AdminUsername'])?Tool_Fnc::ajaxMsg('请您填写账号'):Tool_fnc::safe_string($_REQUEST['AdminUsername']);
		$AdminPasswordsure = empty($_REQUEST['AdminPasswordsure'])?Tool_Fnc::ajaxMsg('请您确认密码'):Tool_fnc::safe_string($_REQUEST['AdminPasswordsure']);
		$AdminRote = empty($_REQUEST['AdminRote'])?Tool_Fnc::ajaxMsg('请您选择角色'):Tool_fnc::safe_string($_REQUEST['AdminRote']);
		if ($AdminPassword!=$AdminPasswordsure) {
			Tool_Fnc::ajaxMsg('两次密码不一致');
		}
        $token = Tool_Fnc::newrand();
        $AdminPassword = Tool_Fnc::markmd5($AdminPassword,$token);
        $Admin = array(
        	'admin_id'		=> $AdminId,
        	'admin_name'    => $AdminName,
        	'admin_username'=> $AdminUsername,
        	'admin_password'=> $AdminPassword,
        	'admin_created' => time(),
        	'hospital'      => $HospitalID,
        	'hospital_name' => '',
        	'admin_status'  => 1,
        	'admin_token'   => $token,
        	'admin_pwd'     => $AdminPasswordsure,
        );

        $tAMO->begin();
        if (!$tAMO->update($Admin)) {
        	$tAMO->back();
        	Tool_Fnc::ajaxMsg('系统繁忙请稍后再试！1');
        }
        $Rote = array(
        	'a_r_id'=>$ARID,
        	'u_id' => $AdminId,
        	'r_id' => $AdminRote,
        );
        if (!$tURMO->update($Rote)) {
        	$tAMO->back();
        	Tool_Fnc::ajaxMsg('系统繁忙请稍后再试！2');
        }
       	$tAMO->commit();
        Tool_Fnc::ajaxMsg('添加成功',1);
	}


	public function AdminStatuAction(){
		$tAMO = new AdminModel;
		$AdminId = $_REQUEST['AdminId'];
		$sta = $_REQUEST['sta'];
		$UpdateArr = array(
			'admin_id'=>$AdminId,
			'admin_status'=>$sta,
		);
		if (!$tAMO->update($UpdateArr)) {
			Tool_Fnc::ajaxMsg('系统繁忙请稍后再试！');
		}
		Tool_Fnc::ajaxMsg('操作成功',1);
	}

	public function HospitalInfo(){
		$tHMO = new HospitalModel;
		$HospitalInfo = $tHMO->field('hospital_id HospitalID,hospital_ip HospitalIP,hospital_name HospitalName,hospital_url HospitalUrl')->fList();
		return $HospitalInfo;	
	}
}
?>