package com.hmsoft.bizpanel;

import com.hmsoft.common.util.CoursePlanDataUtil;
import com.hmsoft.model.CoursePlan;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Tab;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * 
 * @author zq
 *
 */
public class HisPubTab extends Tab {
	
	public static final TableView<CoursePlan> HIS_TABLE = new TableView<CoursePlan>();
	public static final ObservableList<CoursePlan> HIS_DATALIST = FXCollections.observableArrayList();
	//历史发布批次下拉框
	public static final ComboBox<String> BATCH_COMBO_BOX = new ComboBox<String>();
	public static final ObservableList<String> BATCH_COMBO_DATA_LIST = FXCollections.observableArrayList();
	
	public HisPubTab(final Stage stage) {
		super();
		this.setText("最终发布");
		this.setId("finalCollectId");
		this.setClosable(false);
		//Grid
		GridPane gp = new GridPane();
		gp.setPadding(new Insets(20));
		gp.setHgap(5);
		gp.setVgap(5);
		//
		this.createComboBox(gp);
		//创建按钮
		this.createBtn(gp, stage);
		//创建表格
		this.createHisTable(gp, stage);
		//grid内容
		this.setContent(gp);
		
		
	}

	//批次下拉框
	private void createComboBox(final GridPane gp) {
		//批次号
		BATCH_COMBO_BOX.setPromptText("批次号");
		BATCH_COMBO_BOX.setEditable(false);
		BATCH_COMBO_DATA_LIST.addAll(CoursePlanDataUtil.getPublishedBatchNoList());
		BATCH_COMBO_BOX.setItems(BATCH_COMBO_DATA_LIST);
		BATCH_COMBO_BOX.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				String selectedNo = BATCH_COMBO_BOX.getSelectionModel().getSelectedItem();
				//加载该批次之前已经导入的课程计划
				HIS_DATALIST.clear();
				HIS_DATALIST.addAll(CoursePlanDataUtil.loadCoursePlanData(selectedNo));
			}
		});
		//批次下拉框
		gp.add(BATCH_COMBO_BOX, 0, 0);
	}
	
	private void createBtn(GridPane gp, Stage stage) {
		
	}
	
	private void createHisTable(GridPane gp, Stage stage) {
		//表格宽度
		HIS_TABLE.setMinWidth(stage.getWidth() - 70);
		HIS_TABLE.setMinHeight(stage.getHeight() - 170);
		HIS_TABLE.setEditable(true);
		//表格列
		final TableColumn<CoursePlan, Integer> coursePlanId = new TableColumn<CoursePlan, Integer>("ID");
		coursePlanId.setCellValueFactory(new PropertyValueFactory<CoursePlan, Integer>("coursePlanId"));
		coursePlanId.setMinWidth(10);
		final TableColumn<CoursePlan, String> courseCode = new TableColumn<CoursePlan, String>("课程代码");
		courseCode.setCellValueFactory(new PropertyValueFactory<CoursePlan, String>("courseCode"));
		courseCode.setMinWidth(30);
		final TableColumn<CoursePlan, String> courseName = new TableColumn<CoursePlan, String>("课程名称");
		courseName.setCellValueFactory(new PropertyValueFactory<CoursePlan, String>("courseName"));
		courseName.setMinWidth(50);
		final TableColumn<CoursePlan, String> outlineName = new TableColumn<CoursePlan, String>("课程大纲");
		outlineName.setCellValueFactory(new PropertyValueFactory<CoursePlan, String>("outlineName"));
		outlineName.setMinWidth(60);
		final TableColumn<CoursePlan, String> bookName = new TableColumn<CoursePlan, String>("教材名称");
		bookName.setCellValueFactory(new PropertyValueFactory<CoursePlan, String>("bookName"));
		bookName.setMinWidth(60);
		final TableColumn<CoursePlan, String> bianZhu = new TableColumn<CoursePlan, String>("编著");
		bianZhu.setCellValueFactory(new PropertyValueFactory<CoursePlan, String>("bianZhu"));
		bianZhu.setMinWidth(30);
		final TableColumn<CoursePlan, String> press = new TableColumn<CoursePlan, String>("出版社");
		press.setCellValueFactory(new PropertyValueFactory<CoursePlan, String>("press"));
		press.setMinWidth(50);
		final TableColumn<CoursePlan, String> publications = new TableColumn<CoursePlan, String>("版次");
		publications.setCellValueFactory(new PropertyValueFactory<CoursePlan, String>("publications"));
		publications.setMinWidth(30);
		final TableColumn<CoursePlan, String> cengci = new TableColumn<CoursePlan, String>("层次");
		cengci.setCellValueFactory(new PropertyValueFactory<CoursePlan, String>("cengci"));
		cengci.setMinWidth(30);
//		final TableColumn<CoursePlan, String> zjStatus = new TableColumn<CoursePlan, String>("征集状态");
//		zjStatus.setCellValueFactory(new PropertyValueFactory<CoursePlan, String>("zjStatusDesc"));
//		zjStatus.setMinWidth(30);
		//学校选择的课程IDs

		HIS_TABLE.setItems(HIS_DATALIST);
		HIS_TABLE.getColumns().addAll(coursePlanId, courseCode, courseName, outlineName, bookName, 
				bianZhu, press, publications, cengci);
		
		final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll(HIS_TABLE);
        
        gp.add(vbox, 0, 1, 3, 1);
	}

}
