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.CourseQuestionService;
import cn.hmsoft.ltc.service.iface.CourseService;
import cn.hmsoft.ltc.service.iface.DetailTableService;
import cn.hmsoft.ltc.service.iface.TestPaperService;
import cn.hmsoft.ltc.service.impl.CourseQuestionServiceImpl;
import cn.hmsoft.ltc.service.impl.CourseServiceImpl;
import cn.hmsoft.ltc.service.impl.DetailTableServiceImpl;
import cn.hmsoft.ltc.service.impl.TestPaperServiceImpl;
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 UpdateCourseController implements Initializable {
	@FXML 
	private GridPane updateCourseGridPane;

	@FXML
	private Label updateCourserLabel;
	@FXML
	private Label updateCourseChapterNumLabel;
	@FXML
	private TextField updateCourseChapterNumTextField;
	@FXML
	private Button updateCourseSaveBtn;
	@FXML
	private Button updateCourseCancelBtn;
	
	private CourseService courseService = new CourseServiceImpl();//课程管理
	public TestPaperService testPaperService = new TestPaperServiceImpl();  //试卷管理
	private DetailTableService detailTableService = new DetailTableServiceImpl(); //细目表
	
	public static final ObservableList<Course> DATALIST = FXCollections.observableArrayList();

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

	}

	
	/**
	 *  修改课程章节保存
	 * @param event
	 * @throws SQLException 
	 */
	public void doUpdateCourseSaveFunc(ActionEvent event) throws Exception {
		try {
			// 校验数据
			String chapterNum = updateCourseChapterNumTextField.getText().trim();
			updateCourseChapterNumTextField.setText(chapterNum);
			if (chapterNum.length() == 0) {
				DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.WARNING, "提示", "总章数不能为空!");
				return;
			}
			try {
				double chap = Double.valueOf(chapterNum);
				if (chap > 25) {
					DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.WARNING, "提示", "最多只能有25个章节!");
					return;
				}
			} catch(Exception ee) {
				DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.WARNING, "提示", "总章数只能为数字！");
				return;
			}
			//通过课程id，判断课程是否保存细目表，detailCount=0,可以修改课程章节，如果detailCount》0，不能修改细目表存
			int detailCount = this.detailTableService.checkDetailTable(LtContext.COURSE_ID);
			if (detailCount > 0) {
				DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.WARNING, "提示", "该课程已经编辑细目表，不能修改课程章节！");
				return;
			}

			int chapterNumber = Integer.valueOf(chapterNum) ; //总章数
			Course course = new Course();
			course.setChapterNumber(chapterNumber);
			if (LtContext.LOGIN_SCHOOL == null) {
				DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.WARNING, "提示", "请登录！");
				return;
			}

			Course course1 = new Course();
			course1 = this.courseService.updateCourseById(LtContext.COURSE_ID,course.getChapterNumber()); //修改课程管理中的课程章节
			this.testPaperService.updateTestPaperChapterNumber(course.getChapterNumber(), LtContext.COURSE_ID); //修改试卷管理中的课程章节
			
			course.setId(course1.getId());
			course.setCode(course1.getCode());
			course.setCodeHubei(course1.getCodeHubei());
			course.setName(course1.getName());
			course.setStatusName(course.getStatus() == 0 ? "停用" : "启用");
			course.setOpenEdit(course1.getOpenEdit());
			course.setChapterNumber(course1.getChapterNumber());
			course.setTeachBook(course1.getTeachBook());
			course.setTeachOutline(course1.getTeachOutline());
			course.setSchoolName(LtContext.LOGIN_SCHOOL);
			course.setBatchNumber(course1.getBatchNumber());
			course.setVerificationTextbook(course1.getVerificationTextbook());
			course.setEditor(course1.getEditor());
			course.setEdition(course1.getEdition());
			course.setPress(course1.getPress());
			course.setVerificationTextbookName(course1.getVerificationTextbook() == 0 ? "未校验" : "已校验");

			DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.INFORMATION, "成功", "课程章节修改成功！");
			//关闭当前窗体
			((Node) (event.getSource())).getScene().getWindow().hide();
			
			MainPaneController mainController = (MainPaneController)StageManager.CONTROLLER.get("mainPaneController");
			mainController.COURSE_TABLE_DATALIST.clear();//先清理list数据，在添加
			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 doUpdateCancelFunc(ActionEvent event) {
		((Node) (event.getSource())).getScene().getWindow().hide();
	}

}
