<?php
require_once '../../Config.php';
header ( "Content-Type: text/html; charset=utf-8" );
new BackUSL_Controller ( $_REQUEST ['action'] );

/**
 * 管理员控制器
 *
 * @author Wzb
 *        
 */
class BackUSL_Controller {
	/**
	 * 构造函数
	 *
	 * @param string $action 处理方法名
	 */
	public function __construct($action) {
		if (! method_exists ( $this, $action )) {
			header ( 'Location:../index.html' );
			die ();
		}
		switch ($action) {
			case "login" : // 管理员登陆
			case "outLogin" : // 退出登录
				break;
			case "add" : // 添加管理员
			case "edit" : // 修改管理员
			case "delete" : // 删除管理员
				if (intval ( BLL_Admin::isLogin () ) === 0) {
					header ( 'Location:../index.html' );
					die ();
				}
				break;
			default :
				die ( "该方法错误" );
				break;
		}
		$this->{$action} ();
	}
	
	/**
	 * 管理员登陆
	 */
	private function login() {
		$ckCode = $_POST ['ckCode'];
		if (strtolower ( $_SESSION ['randcode'] ) != strtolower ( $ckCode ) || empty ( $_SESSION ['randcode'] )) {
			$_SESSION ['randcode'] = "";
			die ( COM_OftenJavaScript::alertGoToUrl ( "验证码输入错误!!!", "../Index.html" ) );
		} else {
			$_SESSION ['randcode'] = "";
		}
		$isLogin = BLL_Admin::login ( array ('username' => $_POST ['user'],'password' => $_POST ['pass'] ) );
		if ($isLogin) {
			header ( "location:../HTML/Frame.php" );
		} else {
			die ( COM_OftenJavaScript::alertGoToUrl ( "用户名或者密码错误,请仔细输入!!!", "../Index.html" ) );
		}
	}
	
	/**
	 * 退出登录
	 */
	private function outLogin() {
		BLL_Admin::outLogin ();
		header ( 'Location:../Index.html' );
	}
	
	/**
	 * 添加管理员
	 */
	private function add() {
		if (BLL_Admin::add ( $_POST ) > 0) {
			echo COM_OftenJavaScript::alertGoToUrl ( "新增成功!!!", "Admin_List.php" );
		} else {
			echo COM_OftenJavaScript::alertGoBack ( "新增失败!!!" );
		}
	}
	
	/**
	 * 修改管理员
	 */
	private function edit() {
		$newArray = array ();
		$newArray ['id'] = $_POST ['theId'];
		if (! empty ( $_POST ['pass1'] )) {
			$newArray ['password_txt'] = $_POST ['pass1'];
		}
		$newArray ['nickname_txt'] = $_POST ['nickname'];
		if (BLL_Admin::edit ( $newArray ) > 0) {
			echo COM_OftenJavaScript::alertGoToUrl ( "修改成功!!!", "Admin_Edit.php?theId=" . $_POST ['theId'] );
		} else {
			echo COM_OftenJavaScript::alertGoBack ( "修改失败!!!" );
		}
	}
	
	/**
	 * 删除管理员
	 */
	private function delete() {
		$idList = $_GET ['theList'];
		if (BLL_Admin::delete ( $idList ) == "ok") {
			echo COM_OftenJavaScript::parentRefurbish ();
		} else {
			echo COM_OftenJavaScript::parentAlert ( "删除失败!!!" );
		}
	}
}
?>