package cn.hmsoft.ltc.controller;

import java.net.URL;
import java.sql.SQLException;
import java.util.ResourceBundle;
import cn.hmsoft.ltc.common.util.DialogUtil;
import cn.hmsoft.ltc.common.util.StageManager;
import cn.hmsoft.ltc.model.QuestionTypeSelect;
import cn.hmsoft.ltc.model.QuestionType;
import cn.hmsoft.ltc.service.iface.QuestionTypeService;
import cn.hmsoft.ltc.service.impl.QuestionTypeServiceImpl;
import de.felixroske.jfxsupport.FXMLController;
import de.felixroske.jfxsupport.GUIState;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;

/**
 * 新增题型信息
 * @author Administrator
 *
 */
@FXMLController
public class QuestionTypesController implements Initializable {
	@FXML 
	private GridPane questionGridPane;
	@FXML
	private Label addQuestionLab;
	@FXML 
	private Label codeLabel;
	@FXML 
	private Label nameLabel;
	@FXML 
	private Label typeLabel;
	@FXML 
	private Label defauleScoreLabel;
	@FXML 
	private Label statusLabel;
	@FXML 
	private Label remarkLabel;
	@FXML 
	private TextField codeTextField;
	@FXML 
	private TextField nameTextField;
	@FXML 
	private TextField defauleScoreTextField;
	@FXML 
	private TextField guideTextField;
    @FXML
    private Button saveTypeBtn;
    @FXML
    private Button cancelBtn;
	
	//题目类型下拉框.
	@FXML
	public ComboBox<QuestionTypeSelect> typeSelectBox;//细目表中课程下拉框
    
	private QuestionTypeService questionTypeService = new QuestionTypeServiceImpl();
	public static final ObservableList<QuestionType> DATALIST_QUESTION_TYPE = FXCollections.observableArrayList();

	public static QuestionType questionTypeObj;

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		//初始化设置下拉框值
		ObservableList<QuestionTypeSelect> typeSelect = FXCollections.observableArrayList();
		QuestionTypeSelect type1 = new QuestionTypeSelect(0, "主观题");
		QuestionTypeSelect type2 = new QuestionTypeSelect(1, "单选题");
		QuestionTypeSelect type3 = new QuestionTypeSelect(2, "多选题");
		
		typeSelect.add(type1);
		typeSelect.add(type2);
		typeSelect.add(type3);
		typeSelectBox.setItems(typeSelect);
		typeSelectBox.setPromptText("请选择");
	}

	/**
	 * 题型保存
	 * @param event
	 * @throws SQLException 
	 */
	public void doTypeSaveFun(ActionEvent event) throws SQLException {
		try {
			// 校验数据
			QuestionType questionType = checkQuestion();
			//给对应的名称值
			if (questionType.getCorrectOptionsNum()==0) {
				questionType.setCorrectOptionsName("主观题");
			} else if (questionType.getCorrectOptionsNum() == 1) {
				questionType.setCorrectOptionsName("单选题");
			} else if (questionType.getCorrectOptionsNum() ==2) {
				questionType.setCorrectOptionsName("多选题");
			}
			questionType.setStatusCode(1);//默认是启用
			questionType.setEditeStatus(1);//默认添加的是启用编辑的,初始化内置的为0
			questionType.setStatusName(questionType.getStatusCode()==0 ? "停用" : "启用");
			this.questionTypeService.insertQuestionType(questionType);
			DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.INFORMATION, "成功", "题型保存成功！");
			//关闭当前窗体
			((Node) (event.getSource())).getScene().getWindow().hide();
			MainPaneController mainController = (MainPaneController)StageManager.CONTROLLER.get("mainPaneController");
			mainController.DATALIST_QUESTION_TYPE.add(questionType);
			mainController.questionTypeTableView.refresh();
			//mainController.freshQuestionAdd(questionType);//刷新 网页题型下拉框
		} catch (Exception ee) {
			ee.printStackTrace();
		}
	}
	
	
	/**
	 * 取消
	 * @param event
	 */
	public void doCancelFunc(ActionEvent event) {
		((Node) (event.getSource())).getScene().getWindow().hide();
	}
	
	/**
	 * 校验题型管理字符信息
	 * @return
	 */
	public QuestionType checkQuestion() {
		String code = codeTextField.getText().trim(); //题型编码
		codeTextField.setText(code);
		String name = nameTextField.getText().trim();   //题型名称
		nameTextField.setText(name);
		if(name.trim().length() == 0) {
			DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.WARNING, "提示", "题型名称不能为空！");
			return null;
		}
		Integer correctOptionsNum = 0;			//正确选项数量  0:主观题,1.单选题,2.多选题
		String testType = this.typeSelectBox.getSelectionModel().getSelectedItem().getName();
		if ("主观题".equals(testType)) {
			correctOptionsNum = 0;
		} else if("单选题".equals(testType)) {
			correctOptionsNum = 1;
		} else if("多选题".equals(testType)) {
		 	correctOptionsNum = 2;
		} else {
			DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.WARNING, "提示", "题型不能为空！");
			return null;
		}
		
		String scoreStr = defauleScoreTextField.getText().trim();
		defauleScoreTextField.setText(scoreStr);
		if (scoreStr.length() == 0) {
			DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.WARNING, "提示", "默认分值不能为空！");
			return null;
		}
		try {
			Double.valueOf(scoreStr);
		} catch(Exception ee){
			DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.WARNING, "提示", "默认分值只能为数字！");
			return null;
		}
		Integer defaultScore = Integer.valueOf(scoreStr); //默认分值
		String guideText = this.guideTextField.getText().trim();//备注
		guideTextField.setText(guideText);
		QuestionType question = new QuestionType();
		question.setCode(code);
		question.setName(name);
		question.setCorrectOptionsNum(correctOptionsNum);
		question.setStatusCode(1);
		question.setDefaultScore(defaultScore);
		question.setGuideText(guideText);
		return question;
	}

}
