package com.hmsoft.bizpanel;

import com.alibaba.fastjson.JSON;
import com.hmsoft.app.Login;
import com.hmsoft.app.Main;
import com.hmsoft.common.AppConst;
import com.hmsoft.common.MsgTypeConst;
import com.hmsoft.common.util.DialogUtil;
import com.hmsoft.model.request.BizRequest;
import com.hmsoft.remote.rmi.exception.RemotingException;
import com.hmsoft.remote.rmi.model.Message;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.Tab;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class SysTab extends Tab {

	public SysTab(final Stage stage) {
		super();
		this.setText("系统配置");
		this.setId("systTabId");
		this.setClosable(false);
		//Grid容器
		GridPane gp = new GridPane();
		gp.setPadding(new Insets(20));
		gp.setHgap(5);
		gp.setVgap(5);
		
		Text passwdText = new Text("新密码：");
		final PasswordField passwd = new PasswordField();
		passwd.setMinWidth(60);
		final Button btnSave = new Button("修改");
		btnSave.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent event) {
				passwd.setText(passwd.getText().trim());
				if (passwd.getText().length() == 0) {
					DialogUtil.showAlertDialog(stage, AlertType.WARNING, "提示", "请输入新密码.");
					return;
				} else {
					final BizRequest req = new BizRequest();
					req.setReqType(AppConst.SCH_CHANGE_PASSWD);//修改密码请求
					req.setSchoolId(Main.LOGIN_SCHOOL_ID);
					req.setSchoolName(passwd.getText());
					final String json = JSON.toJSONString(req);
					final Message msg = new Message(MsgTypeConst.TYPE_BIZ_REQ_TO_MGE, json);
					try {
						//IFuture future = 
						Object obj = Login.CLIENT.requestTwoWay(msg);
						DialogUtil.showAlertDialog(stage, Alert.AlertType.INFORMATION, "成功", "修改成功!");
					} catch (RemotingException e) {
						e.printStackTrace();
					}
				}
			}
		});
		
		gp.add(passwdText, 0, 0);
		gp.add(passwd, 1, 0);
		gp.add(btnSave, 0, 1);
		//grid布局加到容器
		this.setContent(gp);
	}
}
