package cn.hmsoft.ltc.controller;

import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.util.ResourceBundle;
import cn.hmsoft.ltc.common.constants.LtContext;
import cn.hmsoft.ltc.common.util.DialogUtil;
import cn.hmsoft.ltc.common.util.StageManager;
import cn.hmsoft.ltc.model.Course;
import cn.hmsoft.ltc.service.iface.CourseService;
import cn.hmsoft.ltc.service.impl.CourseServiceImpl;
import de.felixroske.jfxsupport.FXMLController;
import de.felixroske.jfxsupport.GUIState;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

/**
 * 新增课程信息
 * @author Administrator
 *
 */
@FXMLController
public class CourseController implements Initializable {
	@FXML 
	private GridPane addCourseGridPane;
	@FXML
	private Label addCourserLabel;
	@FXML 
	private Label courseCodeLabel;
	@FXML 
	private Label courseNameLabel;
	@FXML
	private Label courseStatusLabel;
	@FXML
	private Label courseChapterNumLabel;
	@FXML
	private Label courseRemarkLabel;
	@FXML
	private TextField courseCodeTextField;
	@FXML
	private TextField courseNameTextField;
	@FXML
	private TextField courseChapterNumTextField;
	@FXML
	private TextField batchNumberTextField;
	@FXML
	private TextField courseRemarkTextField;
	@FXML
	private TextField teachBookTextField;//教材
	@FXML
	private TextField teachOutlineTextField;//教材大纲
	@FXML
	private Button addCourseSaveBtn;
	@FXML
	private Button addCourseCancelBtn;
	
	private CourseService courseService = new CourseServiceImpl();
	
	public static final ObservableList<Course> DATALIST = FXCollections.observableArrayList();

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {

	}

	/**
	 *  新增课程保存
	 * @param event
	 * @throws SQLException 
	 */
	public void doCourseSaveFunc(ActionEvent event) throws SQLException {
		try {
			// 校验数据
			Course course = checkCourse();
			course.setStatusName(course.getStatus() == 0 ? "停用" : "启用");
			if (LtContext.LOGIN_SCHOOL == null) {
				DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.WARNING, "提示", "请登录！");
				return;
			}
			course.setSchoolName(LtContext.LOGIN_SCHOOL);
			Integer courseId = this.courseService.insertCourse(course);
			course.setId(courseId);
			DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.INFORMATION, "成功", "课程保存成功！");
			//关闭当前窗体
			((Node) (event.getSource())).getScene().getWindow().hide();
			
			MainPaneController mainController = (MainPaneController)StageManager.CONTROLLER.get("mainPaneController");
			mainController.COURSE_TABLE_DATALIST.add(course);
			mainController.courseTableView.refresh();
			course.getOperBtn().setText("关联题型");
			course.getOperBtn().setOnAction(new EventHandler<ActionEvent>() {
				@Override
				public void handle(ActionEvent arg0) {
					try {
						LtContext.CURR_COURSE = course;
						FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/setQuestionType.fxml"));
						Parent root = fxmlLoader.load();
						Stage stage = new Stage();
						stage.setResizable(false);
						stage.setTitle("(" + course.getCode() + ") " + course.getName() + "-关联题型");
						stage.setScene(new Scene(root, 1181, 663));
						//stage.setAlwaysOnTop(true);
						stage.setMaximized(false);
						stage.show();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			});
			
			course.getOperUpdateBtn().setText("修改课程章节");
			course.getOperUpdateBtn().setOnAction(new EventHandler<ActionEvent>() {
				@Override
				public void handle(ActionEvent arg0) {
					try {
						LtContext.CURR_COURSE = course;
						FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/UpdateCourseView.fxml"));
						Parent root = fxmlLoader.load();
						Stage stage = new Stage();
						stage.setResizable(false);
						stage.setTitle("(" + course.getCode() + ") " + course.getName() + "-修改课程章节");
						stage.setScene(new Scene(root, 414,184));
						stage.setAlwaysOnTop(true);
						stage.setMaximized(false);
						stage.show();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			});
		} catch (Exception ee) {
			ee.printStackTrace();
		}
	}
	
	/**
	 * 取消
	 * @param event
	 */
	public void doCancelFunc(ActionEvent event) {
		((Node) (event.getSource())).getScene().getWindow().hide();
	}

	/**
	 * 校验输入的相关信息
	 * @return
	 */
	private Course checkCourse() {
		String code = courseCodeTextField.getText().trim();
		courseCodeTextField.setText(code);
		String name = courseNameTextField.getText().trim();
		courseNameTextField.setText(name);
		String chapterNum = courseChapterNumTextField.getText().trim();
		courseChapterNumTextField.setText(chapterNum);
		String batchNumber = batchNumberTextField.getText().trim();
		batchNumberTextField.setText(batchNumber);
		String remark = courseRemarkTextField.getText().trim();
		courseRemarkTextField.setText(remark);
		if (code.length() == 0) {
			DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.WARNING, "提示", "课程编号不能为空!");
	        return null;
		}
		if (name.length() == 0) {
			DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.WARNING, "提示", "课程名称不能为空!");
			return null;
		}
		if (chapterNum.length() == 0) {
			DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.WARNING, "提示", "总章数不能为空!");
	        return null;
		}
		if (this.teachBookTextField.getText().length() == 0) {
			DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.WARNING, "提示", "教材不能为空!");
	        return null;
		}
		if (this.teachOutlineTextField.getText().length() == 0) {
			DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.WARNING, "提示", "教材大纲不能为空!");
	        return null;
		}
		try {
			double chap = Double.valueOf(chapterNum);
			if (chap > 25) {
				DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.WARNING, "提示", "最多只能有25个章节!");
				return null;
			}
		} catch(Exception ee) {
			DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.WARNING, "提示", "总章数只能为数字！");
			return null;
		}
		if (batchNumber.trim().length() == 0) {
			DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.WARNING, "提示", "批次号不能为空!");
	        return null;
		}
		int chapterNumber = Integer.valueOf(chapterNum) ; //总章数
		Course course = new Course();
		course.setCode(code);
		course.setName(name);
		course.setStatus(1);//默认启用
		course.setRemark(remark);
		course.setChapterNumber(chapterNumber);
		course.setTeachBook(this.teachBookTextField.getText().trim());
		course.setTeachOutline(this.teachOutlineTextField.getText());
		course.setBatchNumber(batchNumber);
		return course;
	}
	

}
