<?php
require_once '../phpbean/Student.class.php';
require_once '../service/StudentService.class.php';

//创建了StudentService实例
$studentService = new StudentService();

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

		$student->setStudentNumber($array_student['studentNumber']);
		$student->setStudentName($array_student['studentName']);
		$student->setStudentPassword($array_student['studentPassword']);
		$student->setStudentSex($array_student['studentSex']);
		$student->setStudentClassNumber($array_student['studentClassNumber']);
		$student->setStudentBirthday($array_student['studentBirthday']);
		$student->setStudentState($array_student['studentState']);
		$studentPhoto = "/upload/NoImage.jpg";
		if ($_FILES['studentPhoto']['name'] != ''){
			/*--  实例化上传类  --*/
			$file = $_FILES['studentPhoto'];
			$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();
			$studentPhoto = substr($pic['path'], 2)."/".$pic['savename'];
		}
		$student->setStudentPhoto($studentPhoto);


		$student->setStudentTelephone($array_student['studentTelephone']);
		$student->setStudentEmail($array_student['studentEmail']);
		$student->setStudentQQ($array_student['studentQQ']);
		$student->setStudentAddress($array_student['studentAddress']);
		$student->setStudentMemo($array_student['studentMemo']);
		//完成添加->数据库
		$res = $studentService->AddStudent($student);
		if($res==1) {
			header("Location: /ok.php");
			exit();
		} else {
			//失败
			header("Location: /error.php");
			exit();
		}
	} else if($action == "query") {
		//查询学生信息信息
		require_once '../service/StudentService.class.php';
		require_once '../service/FenyePage.class.php';
		//获取查询参数
		$studentNumber = !empty($_POST['studentNumber']) ? $_POST['studentNumber'] : "";
		$studentName = !empty($_POST['studentName']) ? $_POST['studentName'] : "";
		$studentClassNumber = !empty($_POST['studentClassNumber']) ? $_POST['studentClassNumber'] : "";
		$studentBirthday = !empty($_POST['studentBirthday']) ? $_POST['studentBirthday'] : "";
		//创建StudentService实例
		$studentService = new StudentService();

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

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

		//调用getFenyePage，该方法可以把fenyePage完成
		$studentService->getFenyePage($fenyePage,$studentNumber,$studentName,$studentClassNumber,$studentBirthday);
		require_once '../service/ClassInfoService.class.php';
		$classInfoService = new ClassInfoService();
		$classInfo_res = $classInfoService->QueryAllClassInfo();

		include '../Student/studentManage.php';
	} else if($action == "frontQuery") {
		//查询学生信息信息
		require_once '../service/StudentService.class.php';
		require_once '../service/FenyePage.class.php';
		//获取查询参数
		$studentNumber = !empty($_POST['studentNumber']) ? $_POST['studentNumber'] : "";
		$studentName = !empty($_POST['studentName']) ? $_POST['studentName'] : "";
		$studentClassNumber = !empty($_POST['studentClassNumber']) ? $_POST['studentClassNumber'] : "";
		$studentBirthday = !empty($_POST['studentBirthday']) ? $_POST['studentBirthday'] : "";
		//创建StudentService实例
		$studentService = new StudentService();

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

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

		//调用getFenyePage，该方法可以把fenyePage完成
		$studentService->getFenyePage($fenyePage,$studentNumber,$studentName,$studentClassNumber,$studentBirthday);
		require_once '../service/ClassInfoService.class.php';
		$classInfoService = new ClassInfoService();
		$classInfo_res = $classInfoService->QueryAllClassInfo();

		include '../Student/studentFrontQuery.php';
	} else if($action == "update") {
		//说明用户希望执行更新学生信息信息
		$array_student = $_POST["student"];
		$student = $studentService->GetStudent($array_student['studentNumber']);
		/*处理图片上传*/
		require_once("../util/upload.class.php");
		require_once("../util/util.php");

		$student->setStudentNumber($array_student['studentNumber']);
		$student->setStudentName($array_student['studentName']);
		$student->setStudentPassword($array_student['studentPassword']);
		$student->setStudentSex($array_student['studentSex']);
		$student->setStudentClassNumber($array_student['studentClassNumber']);
		$student->setStudentBirthday($array_student['studentBirthday']);
		$student->setStudentState($array_student['studentState']);
		if ($_FILES['studentPhoto']['name'] != ''){
			/*--  实例化上传类  --*/
			$file = $_FILES['studentPhoto'];
			$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();
			$studentPhoto = substr($pic['path'], 2)."/".$pic['savename'];
		  $student->setStudentPhoto($studentPhoto);
		}

		$student->setStudentTelephone($array_student['studentTelephone']);
		$student->setStudentEmail($array_student['studentEmail']);
		$student->setStudentQQ($array_student['studentQQ']);
		$student->setStudentAddress($array_student['studentAddress']);
		$student->setStudentMemo($array_student['studentMemo']);
		//完成修改->数据库
		$res = $studentService->UpdateStudent($student);
		if($res != 0) {
			header("Location: /ok.php");
			exit();
		} else {
			//失败
			header("Location: /error.php");
			exit();
		}
	} else if($action == "del") {
		//这时我们知道要删除学生信息信息
		$studentNumber = $_GET['studentNumber'];
		if($studentService->DeleteStudent($studentNumber) == 1) {
			//成功
			header("Location: /ok.php");
			exit();
		} else {
			//失败
			header("Location: /error.php");
			exit();
		}
	}
}
?>
