package com.hmsoft.bizpanel;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.hmsoft.common.util.DialogUtil;
import com.hmsoft.common.util.POIUtil;
import com.hmsoft.model.Course;
import com.hmsoft.model.CoursePlan;
import com.hmsoft.model.School;
import com.hmsoft.model.StatisObj;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
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.FileChooser;
import javafx.stage.Stage;

/**
 * 统计
 * @author zq
 *
 */
public class StatisTab extends Tab {
	public static final TableView<StatisObj> STATIS_TABLE = new TableView<StatisObj>();
	public static final ObservableList<StatisObj> STATIS_DATALIST = FXCollections.observableArrayList();
	
	
	public StatisTab(final Stage stage) {
		super();
		this.setText("征集统计");
		this.setId("statisTabId");
		this.setClosable(false);
		//Grid容器
		GridPane gp = new GridPane();
		gp.setPadding(new Insets(20));
		gp.setHgap(5);
		gp.setVgap(5);
		//创建按钮
		this.createBtn(stage, gp);
		//创建表格
		this.createStatisTable(gp, stage);
		//grid布局加到容器
		this.setContent(gp);
	}

	@SuppressWarnings("unchecked")
	private void createStatisTable(final GridPane gp, final Stage stage) {
		//设置表格整体宽度
		STATIS_TABLE.setMinWidth(stage.getWidth() - 70);
		STATIS_TABLE.setMinHeight(stage.getHeight() - 170);
		//设置所有列
		TableColumn<StatisObj, String> schoolName = new TableColumn<StatisObj, String>("学校名称");
		schoolName.setCellValueFactory(new PropertyValueFactory<StatisObj, String>("schoolName"));
		schoolName.setMinWidth(150);
		TableColumn<StatisObj, Integer> count = new TableColumn<StatisObj, Integer>("课程数量");
		count.setCellValueFactory(new PropertyValueFactory<StatisObj, Integer>("count"));
		count.setMinWidth(100);
		TableColumn<StatisObj, ComboBox<Course>> comboBox = new TableColumn<StatisObj, ComboBox<Course>>("课程名称");
		comboBox.setCellValueFactory(new PropertyValueFactory<StatisObj, ComboBox<Course>>("comboBox"));
		comboBox.setMinWidth(260);
		
		STATIS_TABLE.setItems(STATIS_DATALIST);
		STATIS_TABLE.getColumns().addAll(schoolName, count, comboBox);
		
		final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll(STATIS_TABLE);
        
        gp.add(vbox, 0, 1, 2, 1);//第1行
	}

	private void createBtn(final Stage stage, final GridPane gp) {
		Button btnFrs = new Button();
		btnFrs.setText("刷新统计");
		Button btnExport = new Button();
		btnExport.setText("统计导出");
		btnFrs.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				Map<Long, List<Course>> selectMap = new HashMap<Long, List<Course>>();
				Map<Long, String> schoolMap = new HashMap<Long, String>();
				for (School sch : SchoolTab.DATALIST) {
					selectMap.put(sch.getSchoolId(), new ArrayList<Course>());
					schoolMap.put(sch.getSchoolId(), sch.getSchoolName());
				}
				for (CoursePlan cp : MtzjTab.DATALIST) {
					//指定的
					if (cp.getSchoolId() != null && cp.getSchoolId() > 0) {
						List<Course> list = selectMap.get(cp.getSchoolId());
						list.add(new Course(cp.getCourseCode(), cp.getCourseName()));
					} else {
						//可能多学校选择
						if (cp.getSchSelectIds() != null && cp.getSchSelectIds().length() > 0) {
							String[] arr = cp.getSchSelectIds().split("\\|");
							for (String idstr : arr) {
								List<Course> list = selectMap.get(Long.valueOf(idstr));
								list.add(new Course(cp.getCourseCode(), cp.getCourseName()));
							}
						}
					}
				}
				//sort and show
				List<StatisObj> statisList = new ArrayList<StatisObj>();
				Set<Long> set = selectMap.keySet();
				for (Long schoolId : set) {
					List<Course> courList = selectMap.get(schoolId);
					statisList.add(new StatisObj(schoolId, schoolMap.get(schoolId), courList.size(), courList));
				}
				Collections.sort(statisList);
				//
				STATIS_DATALIST.clear();
				STATIS_DATALIST.addAll(statisList);
			}
		});
		btnExport.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				if (DialogUtil.confirm("是否导出？", stage)) {
					FileChooser fileChooser = new FileChooser();
					fileChooser.setTitle("导出");
					fileChooser.setInitialFileName("征集统计.xls");
					fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("XLS Files", "*.xls"));
					File file = fileChooser.showSaveDialog(stage);
					if(file != null) {
						POIUtil.writeStatisToXlsx(STATIS_DATALIST, file.getAbsolutePath());
						DialogUtil.showAlertDialog(stage, Alert.AlertType.INFORMATION, "成功", "导出成功.");
					}
				}
			}
			
		});
		gp.add(btnFrs, 0, 0);
		gp.add(btnExport, 1, 0);
	}
}
