<?php
require_once '../phpbean/CourseInfo.class.php';
require_once '../service/CourseInfoService.class.php';

//创建了CourseInfoService实例
$courseInfoService = new CourseInfoService();

//根据action参数决定用户要执行什么样的操作
if(!empty($_REQUEST['action'])) {
	//接收action值
	$action = $_REQUEST['action'];
	if($action == "add") {
		//说明用户希望执行添加课程信息信息
		$courseInfo = new CourseInfo();
		$array_courseInfo = $_POST["courseInfo"];
		$courseInfo->setCourseNumber($array_courseInfo['courseNumber']);
		$courseInfo->setCourseName($array_courseInfo['courseName']);
		$courseInfo->setCourseTeacher($array_courseInfo['courseTeacher']);
		$courseInfo->setCourseTime($array_courseInfo['courseTime']);
		$courseInfo->setCoursePlace($array_courseInfo['coursePlace']);
		$courseInfo->setCourseScore($array_courseInfo['courseScore']);
		$courseInfo->setCourseMemo($array_courseInfo['courseMemo']);
		//完成添加->数据库
		$res = $courseInfoService->AddCourseInfo($courseInfo);
		if($res==1) {
			header("Location: /ok.php");
			exit();
		} else {
			//失败
			header("Location: /error.php");
			exit();
		}
	} else if($action == "query") {
		//查询课程信息信息
		require_once '../service/CourseInfoService.class.php';
		require_once '../service/FenyePage.class.php';
		//获取查询参数
		$courseNumber = !empty($_POST['courseNumber']) ? $_POST['courseNumber'] : "";
		$courseName = !empty($_POST['courseName']) ? $_POST['courseName'] : "";
		$courseTeacher = !empty($_POST['courseTeacher']) ? $_POST['courseTeacher'] : "";
		//创建CourseInfoService实例
		$courseInfoService = new CourseInfoService();

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

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

		//调用getFenyePage，该方法可以把fenyePage完成
		$courseInfoService->getFenyePage($fenyePage,$courseNumber,$courseName,$courseTeacher);
		require_once '../service/TeacherService.class.php';
		$teacherService = new TeacherService();
		$teacher_res = $teacherService->QueryAllTeacher();

		include '../CourseInfo/courseInfoManage.php';
	} else if($action == "frontQuery") {
		//查询课程信息信息
		require_once '../service/CourseInfoService.class.php';
		require_once '../service/FenyePage.class.php';
		//获取查询参数
		$courseNumber = !empty($_POST['courseNumber']) ? $_POST['courseNumber'] : "";
		$courseName = !empty($_POST['courseName']) ? $_POST['courseName'] : "";
		$courseTeacher = !empty($_POST['courseTeacher']) ? $_POST['courseTeacher'] : "";
		//创建CourseInfoService实例
		$courseInfoService = new CourseInfoService();

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

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

		//调用getFenyePage，该方法可以把fenyePage完成
		$courseInfoService->getFenyePage($fenyePage,$courseNumber,$courseName,$courseTeacher);
		require_once '../service/TeacherService.class.php';
		$teacherService = new TeacherService();
		$teacher_res = $teacherService->QueryAllTeacher();

		include '../CourseInfo/courseInfoFrontQuery.php';
	} else if($action == "update") {
		//说明用户希望执行更新课程信息信息
		$array_courseInfo = $_POST["courseInfo"];
		$courseInfo = $courseInfoService->GetCourseInfo($array_courseInfo['courseNumber']);
		$courseInfo->setCourseNumber($array_courseInfo['courseNumber']);
		$courseInfo->setCourseName($array_courseInfo['courseName']);
		$courseInfo->setCourseTeacher($array_courseInfo['courseTeacher']);
		$courseInfo->setCourseTime($array_courseInfo['courseTime']);
		$courseInfo->setCoursePlace($array_courseInfo['coursePlace']);
		$courseInfo->setCourseScore($array_courseInfo['courseScore']);
		$courseInfo->setCourseMemo($array_courseInfo['courseMemo']);
		//完成修改->数据库
		$res = $courseInfoService->UpdateCourseInfo($courseInfo);
		if($res != 0) {
			header("Location: /ok.php");
			exit();
		} else {
			//失败
			header("Location: /error.php");
			exit();
		}
	} else if($action == "del") {
		//这时我们知道要删除课程信息信息
		$courseNumber = $_GET['courseNumber'];
		if($courseInfoService->DeleteCourseInfo($courseNumber) == 1) {
			//成功
			header("Location: /ok.php");
			exit();
		} else {
			//失败
			header("Location: /error.php");
			exit();
		}
	}
}
?>
