package com.hmsoft.bizpanel;

import java.time.LocalDateTime;
import java.util.Optional;

import com.hmsoft.common.BatchConst;
import com.hmsoft.common.util.BatchDataUtil;
import com.hmsoft.common.util.DateUtil;
import com.hmsoft.common.util.DialogUtil;
import com.hmsoft.model.Batch;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

/**
 * 批次维护
 * @author zq
 *
 */
public class BatchTab extends Tab {

	private Button btnNew;
	private Button btnDelete;
	public static final TableView<Batch> TABLE = new TableView<Batch>();
	public static final ObservableList<Batch> DATALIST = FXCollections.observableArrayList();
	
	public BatchTab(final Stage stage) {
		super();
		this.setText("批次维护");
		this.setId("batchTabId");
		this.setClosable(false);
		
		//Grid容器
		GridPane gp = new GridPane();
		gp.setPadding(new Insets(20));
		gp.setHgap(5);
		gp.setVgap(5);
		//创建按钮
		this.createBtn(stage, gp);
		//创建表格
		this.createBatchTable(gp, stage);
		//grid布局加到容器
		this.setContent(gp);
	}
	
	private void createBtn(final Stage stage, GridPane gp) {
		//按钮
		btnNew = new Button();
		btnNew.setText("新建批次");
		
/*		Button btnActive = new Button();
		btnActive.setText("激活批次");
		
		Button btnPublish = new Button();
		btnPublish.setText("发布批次");*/
		
		btnDelete = new Button();
		btnDelete.setText("删除批次");
		
		btnNew.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent arg0) {
				btnNew.setDisable(true);
				showCreateWin();
			}
			
		});
		
/*		btnActive.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent arg0) {
				
			}
			
		});*/
		
		btnDelete.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent arg0) {
				deleteBatchFunc(stage);
			}
			
		});
		
		//加子对象
		gp.add(btnNew, 0, 0);
/*		gp.add(btnActive, 1, 0);
		gp.add(btnPublish, 2, 0);*/
		gp.add(btnDelete, 1, 0);
	}

	@SuppressWarnings("unchecked")
	private void createBatchTable(final GridPane gp, final Stage stage) {
		//设置表格整体宽度
		TABLE.setMinWidth(stage.getWidth() - 70);
		TABLE.setMinHeight(stage.getHeight() - 170);
		//设置所有列
		TableColumn<Batch, Integer> batchId = new TableColumn<Batch, Integer>("序号");
		batchId.setCellValueFactory(new PropertyValueFactory<Batch, Integer>("batchId"));
		batchId.setMinWidth(50);
		TableColumn<Batch, String> batchName = new TableColumn<Batch, String>("批次号");
		batchName.setCellValueFactory(new PropertyValueFactory<Batch, String>("batchNo"));
		batchName.setMinWidth(200);
		TableColumn<Batch, String> batchDate = new TableColumn<Batch, String>("创建时间");
		batchDate.setCellValueFactory(new PropertyValueFactory<Batch, String>("createTime"));
		batchDate.setMinWidth(180);
		TableColumn<Batch, String> publishTime = new TableColumn<Batch, String>("发布时间");
		publishTime.setCellValueFactory(new PropertyValueFactory<Batch, String>("publishTime"));
		publishTime.setMinWidth(180);
		TableColumn<Batch, String> batchDesc = new TableColumn<Batch, String>("批次描述");
		batchDesc.setCellValueFactory(new PropertyValueFactory<Batch, String>("batchDesc"));
		batchDesc.setMinWidth(600);
		TableColumn<Batch, String> statusDes = new TableColumn<Batch, String>("批次状态");
		statusDes.setCellValueFactory(new PropertyValueFactory<Batch, String>("statusDes"));
		statusDes.setMinWidth(100);
		TableColumn<Batch, String> status = new TableColumn<Batch, String>("批次状态");
		status.setCellValueFactory(new PropertyValueFactory<Batch, String>("status"));
		status.setVisible(false);
		//查询并显示到列表
		DATALIST.addAll(BatchDataUtil.loadBatchData());
		TABLE.setItems(DATALIST);
		TABLE.getColumns().addAll(batchId, batchName, batchDate, publishTime, statusDes, batchDesc);
		
		final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll(TABLE);
        
        gp.add(vbox, 0, 1, 2, 1);//第1行
	}
	
	/**
	 * 弹出新建窗口.
	 */
	private void showCreateWin() {
		final Stage addStage = new Stage();
		addStage.setTitle("添加批次");
		final GridPane grid = new GridPane();
		grid.setAlignment(Pos.CENTER);
		grid.setHgap(10);
		grid.setVgap(10);
		grid.setPadding(new Insets(50));
		
		//批次号标签
		Label batchNo = new Label("批次号: ");
		grid.add(batchNo, 0, 1);
		//批次号输入框
		final TextField batchNoField = new TextField();
		grid.add(batchNoField, 1, 1);
		
		Label batchDese = new Label("批次描述:");
		grid.add(batchDese, 0, 2);
		//批次描述
		final TextField batchDeseField = new TextField();
		batchDeseField.setOnKeyPressed(new EventHandler<KeyEvent>() {
			@Override
			public void handle(KeyEvent event) {
				if (event.getCode() == KeyCode.ENTER) {
					createBatchFunc(addStage, batchNoField, batchDeseField);
				}
			}
		});
		grid.add(batchDeseField, 1, 2);
		
		final HBox hbBtn = new HBox(10);
		grid.add(hbBtn, 1, 4);
		
		Button btnSave = new Button("保存");
		Button btncancel = new Button("取消");
		hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
		hbBtn.getChildren().add(btnSave);
		hbBtn.getChildren().add(btncancel);
		
		//新建窗口退出事件
		addStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
			@Override
			public void handle(WindowEvent event) {
				btnNew.setDisable(false);
			}
		});
		
		btnSave.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				createBatchFunc(addStage, batchNoField, batchDeseField);
			}
		});
		
		btncancel.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				btnNew.setDisable(false);
				addStage.close();
			}
		});
		
		Scene scene = new Scene(grid, 400, 220);
		addStage.setScene(scene);
		addStage.setResizable(false);
		addStage.alwaysOnTopProperty();
		addStage.show();
	}
	
	/**
	 * 删除批次.
	 */
	private void deleteBatchFunc(Stage stage) {
		Batch selected = TABLE.getSelectionModel().getSelectedItem();
		if (selected == null) {
			DialogUtil.showAlertDialog(stage, Alert.AlertType.WARNING, "提示", "请选择批次!");
			return;
		}
/*		if (selected.getStatus() != BatchConst.STATUS_NEW) {
			DialogUtil.showAlertDialog(stage, Alert.AlertType.WARNING, "提示", "只能删除新建状态批次!");
	        return;
		}*/
		
		if (DialogUtil.confirm("确定删除该批次？", stage)) {
			BatchDataUtil.deleteBatch(selected.getBatchId());
			DATALIST.remove(selected);//列表删除
			MtzjTab.BATCH_SELECT_BOX.getItems().remove(selected);//命题征集界面 下拉框删除
		}
	}
	
	private void createBatchFunc(final Stage addStage, final TextField batchNoField,
			final TextField batchDeseField) {
		try {
			for (Batch b : DATALIST) {
				if (b.getBatchNo().equals(batchNoField.getText())) {
					DialogUtil.showAlertDialog(addStage, Alert.AlertType.WARNING, "提示", "批次号不能重复!");
					return;
				}
			}
			final Batch batch = new Batch();
			batch.setBatchId(System.currentTimeMillis());
			batch.setBatchNo(batchNoField.getText());
			batch.setStatus(Integer.valueOf(BatchConst.STATUS_NEW));
			batch.setBatchDesc(batchDeseField.getText());
			batch.setCreateTime(DateUtil.format(LocalDateTime.now()));
			BatchDataUtil.insertBatch(batch);
			DATALIST.add(batch);//批次列表增加显示
			MtzjTab.BATCH_COMBO_DATA_LIST.add(batch);//命题征集界面 下拉框增加
		} catch(Exception e) {
			e.printStackTrace();
		}
		addStage.close();
		btnNew.setDisable(false);
	}
}
