<?php
require_once '../phpbean/Teacher.class.php';
require_once '../service/TeacherService.class.php';

//创建了TeacherService实例
$teacherService = new TeacherService();

//根据action参数决定用户要执行什么样的操作
if(!empty($_REQUEST['action'])) {
	//接收action值
	$action = $_REQUEST['action'];
	if($action == "add") {
		//说明用户希望执行添加教师信息信息
		$teacher = new Teacher();
		$array_teacher = $_POST["teacher"];
		/*处理图片上传*/
		require_once("../util/upload.class.php");
		require_once("../util/util.php");

		$teacher->setTeacherNumber($array_teacher['teacherNumber']);
		$teacher->setTeacherName($array_teacher['teacherName']);
		$teacher->setTeacherPassword($array_teacher['teacherPassword']);
		$teacher->setTeacherSex($array_teacher['teacherSex']);
		$teacher->setTeacherBirthday($array_teacher['teacherBirthday']);
		$teacher->setTeacherArriveDate($array_teacher['teacherArriveDate']);
		$teacher->setTeacherCardNumber($array_teacher['teacherCardNumber']);
		$teacher->setTeacherPhone($array_teacher['teacherPhone']);
		$teacherPhoto = "/upload/NoImage.jpg";
		if ($_FILES['teacherPhoto']['name'] != ''){
			/*--  实例化上传类  --*/
			$file = $_FILES['teacherPhoto'];
			$upload_path = '../upload';
			$allow_type = array('jpg','bmp','png','gif','jpeg');
			$max_size=2048000;
			$upload = new upFiles($file, $upload_path, $max_size, $allow_type);
			$upload->upload();
			$pic = $upload->getSaveFileInfo();
			$teacherPhoto = substr($pic['path'], 2)."/".$pic['savename'];
		}
		$teacher->setTeacherPhoto($teacherPhoto);


		$teacher->setTeacherAddress($array_teacher['teacherAddress']);
		$teacher->setTeacherMemo($array_teacher['teacherMemo']);
		//完成添加->数据库
		$res = $teacherService->AddTeacher($teacher);
		if($res==1) {
			header("Location: /ok.php");
			exit();
		} else {
			//失败
			header("Location: /error.php");
			exit();
		}
	} else if($action == "query") {
		//查询教师信息信息
		require_once '../service/TeacherService.class.php';
		require_once '../service/FenyePage.class.php';
		//获取查询参数
		$teacherNumber = !empty($_POST['teacherNumber']) ? $_POST['teacherNumber'] : "";
		$teacherName = !empty($_POST['teacherName']) ? $_POST['teacherName'] : "";
		$teacherBirthday = !empty($_POST['teacherBirthday']) ? $_POST['teacherBirthday'] : "";
		$teacherArriveDate = !empty($_POST['teacherArriveDate']) ? $_POST['teacherArriveDate'] : "";
		//创建TeacherService实例
		$teacherService = new TeacherService();

		//创建一个FenyePage对象实例
		$fenyePage = new FenyePage();
		//给fenyePage指定必须的参数
		$fenyePage->pageNow = 1;
		$fenyePage->pageSize = 5;
		$fenyePage->gotoUrl = "TeacherAction.php";

		//在此我们需要根据用户的点击来修改pageNow的值，不要判断是否有这个pageNow发送
		if(!empty($_REQUEST['pageNow'])){
			$fenyePage->pageNow=$_REQUEST['pageNow'];
		}

		//调用getFenyePage，该方法可以把fenyePage完成
		$teacherService->getFenyePage($fenyePage,$teacherNumber,$teacherName,$teacherBirthday,$teacherArriveDate);
		include '../Teacher/teacherManage.php';
	} else if($action == "frontQuery") {
		//查询教师信息信息
		require_once '../service/TeacherService.class.php';
		require_once '../service/FenyePage.class.php';
		//获取查询参数
		$teacherNumber = !empty($_POST['teacherNumber']) ? $_POST['teacherNumber'] : "";
		$teacherName = !empty($_POST['teacherName']) ? $_POST['teacherName'] : "";
		$teacherBirthday = !empty($_POST['teacherBirthday']) ? $_POST['teacherBirthday'] : "";
		$teacherArriveDate = !empty($_POST['teacherArriveDate']) ? $_POST['teacherArriveDate'] : "";
		//创建TeacherService实例
		$teacherService = new TeacherService();

		//创建一个FenyePage对象实例
		$fenyePage = new FenyePage();
		//给fenyePage指定必须的参数
		$fenyePage->pageNow = 1;
		$fenyePage->pageSize = 5;
		$fenyePage->gotoUrl = "TeacherAction.php";

		//在此我们需要根据用户的点击来修改pageNow的值，不要判断是否有这个pageNow发送
		if(!empty($_REQUEST['pageNow'])){
			$fenyePage->pageNow=$_REQUEST['pageNow'];
		}

		//调用getFenyePage，该方法可以把fenyePage完成
		$teacherService->getFenyePage($fenyePage,$teacherNumber,$teacherName,$teacherBirthday,$teacherArriveDate);
		include '../Teacher/teacherFrontQuery.php';
	} else if($action == "update") {
		//说明用户希望执行更新教师信息信息
		$array_teacher = $_POST["teacher"];
		$teacher = $teacherService->GetTeacher($array_teacher['teacherNumber']);
		/*处理图片上传*/
		require_once("../util/upload.class.php");
		require_once("../util/util.php");

		$teacher->setTeacherNumber($array_teacher['teacherNumber']);
		$teacher->setTeacherName($array_teacher['teacherName']);
		$teacher->setTeacherPassword($array_teacher['teacherPassword']);
		$teacher->setTeacherSex($array_teacher['teacherSex']);
		$teacher->setTeacherBirthday($array_teacher['teacherBirthday']);
		$teacher->setTeacherArriveDate($array_teacher['teacherArriveDate']);
		$teacher->setTeacherCardNumber($array_teacher['teacherCardNumber']);
		$teacher->setTeacherPhone($array_teacher['teacherPhone']);
		if ($_FILES['teacherPhoto']['name'] != ''){
			/*--  实例化上传类  --*/
			$file = $_FILES['teacherPhoto'];
			$upload_path = '../upload';
			$allow_type = array('jpg','bmp','png','gif','jpeg');
			$max_size=2048000;
			$upload = new upFiles($file, $upload_path, $max_size, $allow_type);
			$upload->upload();
			$pic = $upload->getSaveFileInfo();
			$teacherPhoto = substr($pic['path'], 2)."/".$pic['savename'];
		  $teacher->setTeacherPhoto($teacherPhoto);
		}

		$teacher->setTeacherAddress($array_teacher['teacherAddress']);
		$teacher->setTeacherMemo($array_teacher['teacherMemo']);
		//完成修改->数据库
		$res = $teacherService->UpdateTeacher($teacher);
		if($res != 0) {
			header("Location: /ok.php");
			exit();
		} else {
			//失败
			header("Location: /error.php");
			exit();
		}
	} else if($action == "del") {
		//这时我们知道要删除教师信息信息
		$teacherNumber = $_GET['teacherNumber'];
		if($teacherService->DeleteTeacher($teacherNumber) == 1) {
			//成功
			header("Location: /ok.php");
			exit();
		} else {
			//失败
			header("Location: /error.php");
			exit();
		}
	}
}
?>
