package cn.hmsoft.ltc.controller;

import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.util.ResourceBundle;

import org.apache.commons.lang.StringUtils;

import cn.hmsoft.ltc.common.constants.LtContext;
import cn.hmsoft.ltc.common.util.DialogUtil;
import cn.hmsoft.ltc.common.util.RegexUtil;
import cn.hmsoft.ltc.model.Course;
import cn.hmsoft.ltc.model.QuestionType;
import cn.hmsoft.ltc.model.School;
import cn.hmsoft.ltc.model.Title;
import cn.hmsoft.ltc.model.Users;
import cn.hmsoft.ltc.service.iface.CourseService;
import cn.hmsoft.ltc.service.iface.TestPaperService;
import cn.hmsoft.ltc.service.iface.UserService;
import cn.hmsoft.ltc.service.impl.CourseServiceImpl;
import cn.hmsoft.ltc.service.impl.TestPaperServiceImpl;
import cn.hmsoft.ltc.service.impl.UserServiceImpl;
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.Event;
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.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

/**
 * 注册页面
 * @author zq
 *
 */
@FXMLController
public class AppRegisterController implements Initializable {
	@FXML 
	private GridPane registerGrid;
	@FXML
	private Label schoolsNameLab1;
	@FXML
	private Label numberLab;
	@FXML
	private Label phoneLab1;
	@FXML
	private Label phoneLab2;
	@FXML 
	private Label registNameLab;
	@FXML 
	private Label schoolsNameLab;
	@FXML
	private Label teacherNameLab;
	@FXML
	private Label phoneLab;
	@FXML
	private Label userNameLab;
	@FXML
	private Label passwdLab;
	@FXML
	private Label verifyPasswdLab;
	@FXML
	private Label courseNameLab;
	@FXML
	private ComboBox<School> schoolSelectBox;
	
	@FXML
	private ComboBox<Course> courseCombox;//注册界面 课程选择
	public final ObservableList<Course> COURSE_COMBO_DATA_LIST = FXCollections.observableArrayList();
	@FXML
	private TextField teacherTextFtield;
	@FXML
	private TextField phoneTextFtield;
	@FXML
	private Label userLb;
	@FXML
	private TextField certTextFtield;
	@FXML
	public ComboBox<Title> titleSelectBox;//注册表中教师职称下拉框
	@FXML
	private PasswordField passwdField;
	@FXML
	private PasswordField verifyPasswdField;
	@FXML
	private Button saveBtn;
	@FXML
	private Button cancelBtn;
	//@Autowired
	private UserService userService = new UserServiceImpl();
	public CourseService courseService = new CourseServiceImpl();  //课程
	
    public TestPaperService testPaperService = new TestPaperServiceImpl();  //试卷管理
    
	public final ObservableList<Title> TITLE_COMBO_DATA_LIST = FXCollections.observableArrayList();
	
	public final ObservableList<School> SCHOOL_COMBO_DATA_LIST = FXCollections.observableArrayList();
	
	public final ObservableList<Course> THESIS_COURSE_COMBO_DATA_LIST = FXCollections.observableArrayList();
	
	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		/*Title tx = new Title();
		tx.setId(-1);
		tx.setTitleName("请选择");*/
		
		Title t0 = new Title();
		t0.setId(0);
		t0.setTitleName("教授");
		
		Title t1 = new Title();
		t1.setId(1);
		t1.setTitleName("副教授");
		
		Title t2 = new Title();
		t2.setId(2);
		t2.setTitleName("讲师");
		
		Title t3 = new Title();
		t3.setId(2);
		t3.setTitleName("其他");
		//TITLE_COMBO_DATA_LIST.add(tx);
		TITLE_COMBO_DATA_LIST.add(t0);
		TITLE_COMBO_DATA_LIST.add(t1);
		TITLE_COMBO_DATA_LIST.add(t2);
		TITLE_COMBO_DATA_LIST.add(t3);
		this.titleSelectBox.setItems(TITLE_COMBO_DATA_LIST);
		
		
		School sch0 = new School();
		sch0.setId(0);
		sch0.setTitleName("湖北大学");
		
		School sch1 = new School();
		sch1.setId(1);
		sch1.setTitleName("湖北第二师范学院");
		
		School sch2 = new School();
		sch2.setId(2);
		sch2.setTitleName("湖北国土资源职业学院");
		
		School sch3 = new School();
		sch3.setId(3);
		sch3.setTitleName("湖北汽车工业学院");
		
		School sch4 = new School();
		sch4.setId(4);
		sch4.setTitleName("湖北生物科技职业学院");
		
		School sch5 = new School();
		sch5.setId(5);
		sch5.setTitleName("湖北文理学院");
		
		School sch6 = new School();
		sch6.setId(6);
		sch6.setTitleName("湖北医药学院");
		
		School sch7 = new School();
		sch7.setId(7);
		sch7.setTitleName("湖北中医药大学");
		
		School sch8 = new School();
		sch8.setId(8);
		sch8.setTitleName("华中科技大学");
		
		School sch9 = new School();
		sch9.setId(9);
		sch9.setTitleName("华中农业大学");
		
		School sch10 = new School();
		sch10.setId(10);
		sch10.setTitleName("华中师范大学");
		
		School sch11 = new School();
		sch11.setId(11);
		sch11.setTitleName("黄冈职业技术学院");
		
		School sch12 = new School();
		sch12.setId(12);
		sch12.setTitleName("江汉大学");
		
		School sch13 = new School();
		sch13.setId(13);
		sch13.setTitleName("三峡大学");
		
		School sch14 = new School();
		sch14.setId(14);
		sch14.setTitleName("三峡职业技术学院");
		
		School sch15 = new School();
		sch15.setId(15);
		sch15.setTitleName("武汉大学");
		
		School sch16 = new School();
		sch16.setId(16);
		sch16.setTitleName("武汉纺织大学");
		
		School sch17 = new School();
		sch17.setId(17);
		sch17.setTitleName("武汉工程大学");
		
		School sch18 = new School();
		sch18.setId(18);
		sch18.setTitleName("武汉科技大学");
		
		School sch19 = new School();
		sch19.setId(19);
		sch19.setTitleName("武汉理工大学");
		
		School sch20 = new School();
		sch20.setId(20);
		sch20.setTitleName("武汉轻工大学");
		
		School sch21 = new School();
		sch21.setId(21);
		sch21.setTitleName("武汉生物工程学院");
		
		School sch22 = new School();
		sch22.setId(22);
		sch22.setTitleName("武汉职业技术学院");
		
		School sch23 = new School();
		sch23.setId(23);
		sch23.setTitleName("长江大学");
		
		School sch24 = new School();
		sch24.setId(24);
		sch24.setTitleName("中国地质大学（武汉）");
		
		School sch25 = new School();
		sch25.setId(25);
		sch25.setTitleName("中南财经政法大学");
		
		School sch26 = new School();
		sch26.setId(26);
		sch26.setTitleName("中南民族大学");
		
		School sch27 = new School();
		sch27.setId(27);
		sch27.setTitleName("三峡电力职业学院");
		
		School sch28 = new School();
		sch28.setId(28);
		sch28.setTitleName("湖北经济学院");
		
		School sch29 = new School();
		sch29.setId(29);
		sch29.setTitleName("湖北工业大学");
		
		School sch30 = new School();
		sch30.setId(30);
		sch30.setTitleName("湖北师范大学");
		
		School sch31 = new School();
		sch31.setId(31);
		sch31.setTitleName("湖北美术学院");
		//TITLE_COMBO_DATA_LIST.add(tx);
		SCHOOL_COMBO_DATA_LIST.add(sch0);
		SCHOOL_COMBO_DATA_LIST.add(sch1);
		SCHOOL_COMBO_DATA_LIST.add(sch2);
		SCHOOL_COMBO_DATA_LIST.add(sch3);
		SCHOOL_COMBO_DATA_LIST.add(sch4);
		SCHOOL_COMBO_DATA_LIST.add(sch5);
		SCHOOL_COMBO_DATA_LIST.add(sch6);
		SCHOOL_COMBO_DATA_LIST.add(sch7);
		SCHOOL_COMBO_DATA_LIST.add(sch8);
		SCHOOL_COMBO_DATA_LIST.add(sch9);
		SCHOOL_COMBO_DATA_LIST.add(sch10);
		SCHOOL_COMBO_DATA_LIST.add(sch11);
		SCHOOL_COMBO_DATA_LIST.add(sch12);
		SCHOOL_COMBO_DATA_LIST.add(sch13);
		SCHOOL_COMBO_DATA_LIST.add(sch14);
		SCHOOL_COMBO_DATA_LIST.add(sch15);
		SCHOOL_COMBO_DATA_LIST.add(sch16);
		SCHOOL_COMBO_DATA_LIST.add(sch17);
		SCHOOL_COMBO_DATA_LIST.add(sch18);
		SCHOOL_COMBO_DATA_LIST.add(sch19);
		SCHOOL_COMBO_DATA_LIST.add(sch20);
		SCHOOL_COMBO_DATA_LIST.add(sch21);
		SCHOOL_COMBO_DATA_LIST.add(sch22);
		SCHOOL_COMBO_DATA_LIST.add(sch23);
		SCHOOL_COMBO_DATA_LIST.add(sch24);
		SCHOOL_COMBO_DATA_LIST.add(sch25);
		SCHOOL_COMBO_DATA_LIST.add(sch26);
		SCHOOL_COMBO_DATA_LIST.add(sch27);
		SCHOOL_COMBO_DATA_LIST.add(sch28);
		SCHOOL_COMBO_DATA_LIST.add(sch29);
		SCHOOL_COMBO_DATA_LIST.add(sch30);
		SCHOOL_COMBO_DATA_LIST.add(sch31);
		this.schoolSelectBox.setItems(SCHOOL_COMBO_DATA_LIST);
		
		try {
			//初始化题库页面信息
			this.selectCourseCombobox();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		//姓名输入后触发
		teacherTextFtield.setOnKeyReleased(new EventHandler<Event>() {
			@Override
			public void handle(Event event) {
				String teacherName = teacherTextFtield.getText().trim();
				if (StringUtils.isNotEmpty(teacherName)) {
					userLb.setText(teacherName); //把姓名给用户名
				} else {
					userLb.setText("");//显示空
				}
			}
		});

		
	}

	/**
	 * 用户注册
	 * @param event
	 * @throws SQLException 
	 */
	public void doRegistSaveFunc(ActionEvent event) throws SQLException {
		try {
			//校验注册文本框
			Users user = checkUsers();
			if (user == null) {
				return;
			}
			boolean flag = this.userService.checkName(user.getUserName());
			if(flag == true) {
				user.setUserRole(LtContext.PLAIN_USSER);
				this.userService.insertUser(user);
				Integer id =this.userService.selectUsersByUserName(user.getUserName());
				user.setUserId(id);
				LtContext.COURSE_ID=user.getCourseId();//把课程id保存在常量中
				//DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.INFORMATION, "成功", "用户注册成功，密码请妥善保存，如遗忘请联系学校命题管理人员！");
			} else {
				DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.INFORMATION, "提示", "用户名【"+user.getUserName()+"】已经存在，请重新输入新的用户名！");
				return;
			}

			//登录成功跳转到登入页面
			FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/LoginView.fxml"));
			Parent root = fxmlLoader.load();
			Stage stage = new Stage();
			stage.setTitle("登入");
			stage.setScene(new Scene(root,520,231));
			stage.show();
			//关闭当前窗体
			((Node) (event.getSource())).getScene().getWindow().hide();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 取消
	 * @param event
	 */
	public void doCancelFunc(ActionEvent event) {
		((Node) (event.getSource())).getScene().getWindow().hide();
	}
		
	/**
	 * 校验输入的相关信息
	 * @return
	 */
	private Users checkUsers() {
		String teacherName = teacherTextFtield.getText().trim();
		teacherTextFtield.setText(teacherName);
		userLb.setText(teacherName); //把姓名给用户名
		String phone = phoneTextFtield.getText().trim();
		phoneTextFtield.setText(phone);
		String userName = teacherTextFtield.getText().trim();
		String passWord = passwdField.getText().trim();
		passwdField.setText(passWord);
		String verifyPasswd = verifyPasswdField.getText().trim();
		verifyPasswdField.setText(verifyPasswd);
		if (this.schoolSelectBox.getSelectionModel().getSelectedItem() == null) {
			DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.INFORMATION, "提示", "学校名称不能为空!");
	        return null;
		}
		if (this.courseCombox.getSelectionModel().getSelectedItem() == null) {
			DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.INFORMATION, "提示", "命题课程不能为空!");
	        return null;
		}
		if (teacherName.length() == 0) {
			DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.INFORMATION, "提示", "老师姓名不能为空!");
			return null;
		}
		if (phone.length() == 0) {
			DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.INFORMATION, "提示", "手机号码不能为空!");
	        return null;
		} else if(phone.trim().length() != 11) {
			DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.INFORMATION, "提示", "手机号码只能是11位，请仔细检查！");
	        return null;
		}
		if (this.titleSelectBox.getSelectionModel().getSelectedItem() == null) {
			DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.INFORMATION, "提示", "请选择职称!");
	        return null;
		}
		if (this.certTextFtield.getText().length() == 0) {
			DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.INFORMATION, "提示", "身份证号不能为空!");
			return null;
		}else if(this.certTextFtield.getText().length() != 18) {
			DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.INFORMATION, "提示", "身份证号只能为18位!");
			return null;
		}
		if (userName.length() == 0 ) {
			DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.INFORMATION, "提示", "用户名不能为空!");
			return null;
		}
		if (passWord.length() == 0 || verifyPasswd.length() == 0) {
			DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.INFORMATION, "提示", "用户密码或者确认密码不能为空!");
			return null;
		}
		if(passWord.length() != 6) {
			DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.INFORMATION, "提示", "用户密码必输由6位数字组成!");
			return null;
		}
		if (!passWord.equals(verifyPasswd)) {
			DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.INFORMATION, "提示", "请检查 密码 和 确认密码 一致");
			//如果密码和确认密码不一致，重置为空。
			passwdField.setText("");
			verifyPasswdField.setText("");
			return null;
		}
		
		Users user = new Users();
		user.setSchoolName(this.schoolSelectBox.getSelectionModel().getSelectedItem().getTitleName());
		user.setCourseId(this.courseCombox.getSelectionModel().getSelectedItem().getId());
		/*user.setCourseCode(this.thesisCourseSelectBox.getSelectionModel().getSelectedItem().getCode());
		user.setCourseName(this.thesisCourseSelectBox.getSelectionModel().getSelectedItem().getName());*/
		user.setTeacherName(teacherName);
		user.setPhone(phone);
		user.setUserName(userName);
		user.setPasswd(passWord);
		user.setTitleName(this.titleSelectBox.getSelectionModel().getSelectedItem().getTitleName());
		user.setCertNo(this.certTextFtield.getText().trim());
		return user;
	}
	
	/**
	 * 查询课程下拉框信息
	 * @throws SQLException
	 */
	public void selectCourseCombobox() throws SQLException {
		courseCombox.setEditable(false);
		COURSE_COMBO_DATA_LIST.addAll(this.courseService.selectThesisCourseSelectBox());
		courseCombox.getItems().clear();
		courseCombox.setItems(COURSE_COMBO_DATA_LIST);
	}
	

}
