package cn.hmsoft.ltc.bizpane;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.concurrent.Worker.State;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Tab;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

/**
 * 
 * @author zq
 *
 */
public class DetailTab extends Tab {

	private Button btnAdd;
	private Button btnDelete;
	
	public DetailTab(final Stage stage) {
		super();
		this.setText("细目表");
		this.setId("DetailTab");
		this.setClosable(false);
		
		//Grid容器
		GridPane gp = new GridPane();
		gp.setPadding(new Insets(20));
		gp.setHgap(5);
		gp.setVgap(5);
		
		//创建按钮
		this.createBtn(stage, gp);
		//创建表格
		this.createWebShow(gp, stage);
		//grid布局加到容器
		this.setContent(gp);
	}
	
	private void createBtn(final Stage stage, final GridPane gp) {
		//按钮
		btnAdd = new Button();
		btnAdd.setText("添加学校");
		
		btnDelete = new Button();
		btnDelete.setText("删除学校");
		
		Button btnPassReset = new Button();
		btnPassReset.setText("密码重置");
	
		btnAdd.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent event) {
				btnAdd.setDisable(true);
			}
		});
		
		btnDelete.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent event) {
			}
		});
		
		btnPassReset.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent event) {
			}
		});
		
		gp.add(btnAdd, 0, 0);
		gp.add(btnDelete, 1, 0);
		gp.add(btnPassReset, 2, 0);
	}
	
	@SuppressWarnings("unchecked")
	private void createWebShow(final GridPane gp, final Stage stage) {
	    final WebView browser = new WebView();
	    browser.setMinWidth(stage.getWidth() - 70);
	    browser.setMaxHeight(stage.getHeight() - 170);
	    final WebEngine webEngine = browser.getEngine();
	
	    ScrollPane scrollPane = new ScrollPane();
	    scrollPane.setContent(browser);
	
	    webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
	          @Override
	          public void changed(ObservableValue ov, State oldState, State newState) {
	
	            if (newState == Worker.State.SUCCEEDED) {
	            	stage.setTitle(webEngine.getLocation());
	            }
	
	          }
	        });
	    webEngine.load("http://localhost:8080/detail");//
		
		//添加显示表格
		final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().add(scrollPane);
        
        gp.add(vbox, 0, 1, 3, 1);
	}

}
