import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import view.*;

public class Main extends Application {

    private BorderPane contentArea;

    @Override
    public void start(Stage primaryStage) {
        try {
            // 创建主布局
            BorderPane mainLayout = new BorderPane();

            // 创建菜单栏
            MenuBar menuBar = createMenuBar();
            mainLayout.setTop(menuBar);

            // 创建左侧导航栏
            VBox leftPanel = createLeftPanel();
            mainLayout.setLeft(leftPanel);

            // 创建内容区域
            contentArea = new BorderPane();
            contentArea.setStyle("-fx-background-color: #f4f4f4;");
            contentArea.setPadding(new Insets(10));

            // 设置默认欢迎页面
            showWelcomePage();

            mainLayout.setCenter(contentArea);

            // 创建状态栏
            HBox statusBar = createStatusBar();
            mainLayout.setBottom(statusBar);

            // 设置场景
            Scene scene = new Scene(mainLayout, 1200, 800);
            primaryStage.setScene(scene);
            primaryStage.setTitle("仓库管理系统");
            primaryStage.show();

            // 创建 goods 表
            DML dml = new DML();
            dml.createGoodsTable();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private VBox createLeftPanel() {
        VBox leftPanel = new VBox(10);
        leftPanel.setPadding(new Insets(10));
        leftPanel.setStyle("-fx-background-color: #e8e8e8;");
        leftPanel.setPrefWidth(200);

        // 创建搜索框
        TextField searchField = new TextField();
        searchField.setPromptText("搜索...");

        // 创建导航树
        TreeView<String> navigationTree = createNavigationTree();

        // 添加到左侧面板
        leftPanel.getChildren().addAll(searchField, navigationTree);
        VBox.setVgrow(navigationTree, Priority.ALWAYS);

        return leftPanel;
    }

    private void showWelcomePage() {
        VBox welcomeBox = new VBox(20);
        welcomeBox.setAlignment(javafx.geometry.Pos.CENTER);

        Label welcomeLabel = new Label("欢迎使用仓库管理系统");
        welcomeLabel.setStyle("-fx-font-size: 24px; -fx-font-weight: bold;");

        Label infoLabel = new Label("请从左侧菜单选择要使用的功能");
        infoLabel.setStyle("-fx-font-size: 14px;");

        welcomeBox.getChildren().addAll(welcomeLabel, infoLabel);
        contentArea.setCenter(welcomeBox);
    }

    private HBox createStatusBar() {
        HBox statusBar = new HBox(10);
        statusBar.setPadding(new Insets(5));
        statusBar.setStyle("-fx-background-color: #e8e8e8;");

        Label statusLabel = new Label("就绪");
        Label userLabel = new Label("当前用户：管理员");
        Label timeLabel = new Label(getCurrentTime());

        Region spacer = new Region();
        HBox.setHgrow(spacer, Priority.ALWAYS);

        statusBar.getChildren().addAll(statusLabel, spacer, userLabel, timeLabel);
        // 动态更新时间
        Thread timeThread = new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(1000);
                    Platform.runLater(() -> timeLabel.setText(getCurrentTime()));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        timeThread.setDaemon(true);
        timeThread.start();
        return statusBar;
    }
    private String getCurrentTime() {//实时时间。
        return java.time.LocalDateTime.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
    private MenuBar createMenuBar() {
        MenuBar menuBar = new MenuBar();

        // 系统菜单
        Menu systemMenu = new Menu("系统");
        MenuItem settingsItem = new MenuItem("系统设置");
        MenuItem exitItem = new MenuItem("退出");
        systemMenu.getItems().addAll(settingsItem, exitItem);

        // 库存管理菜单
        Menu inventoryMenu = new Menu("库存管理");
        MenuItem inboundItem = new MenuItem("入库管理");
        MenuItem outboundItem = new MenuItem("出库管理");
        MenuItem queryItem = new MenuItem("库存查询");
        inventoryMenu.getItems().addAll(inboundItem, outboundItem, queryItem);

        // 货物管理菜单
        Menu goodsMenu = new Menu("货物管理");
        MenuItem goodsInfoItem = new MenuItem("货物信息");
        MenuItem categoryItem = new MenuItem("分类管理");
        goodsMenu.getItems().addAll(goodsInfoItem, categoryItem);

        // 报表菜单
        Menu reportMenu = new Menu("报表统计");
        MenuItem inventoryReportItem = new MenuItem("库存报表");
        MenuItem logItem = new MenuItem("操作日志");
        reportMenu.getItems().addAll(inventoryReportItem, logItem);

        // 为入库管理菜单项添加事件处理
        inboundItem.setOnAction(e -> showInboundView());

        // 为出库管理菜单项添加事件处理。
        outboundItem.setOnAction(e -> showOutboundView());

        // 为货物信息菜单项添加事件处理
        goodsInfoItem.setOnAction(e -> showGoodsManagementView());

        //为库存报表菜单项添加事件处理
        inventoryReportItem.setOnAction(e -> showInventoryReportView());

        menuBar.getMenus().addAll(systemMenu, inventoryMenu, goodsMenu, reportMenu);
        return menuBar;
    }

    private TreeView<String> createNavigationTree() {
        TreeItem<String> root = new TreeItem<>("功能导航");
        root.setExpanded(true);

        // 库存管理
        TreeItem<String> inventory = new TreeItem<>("库存管理");
        inventory.getChildren().addAll(
                new TreeItem<>("入库管理"),
                new TreeItem<>("出库管理"),
                new TreeItem<>("库存查询")
        );

        // 货物管理
        TreeItem<String> goods = new TreeItem<>("货物管理");
        goods.getChildren().addAll(
                new TreeItem<>("货物信息"),
                new TreeItem<>("分类管理")
        );

        // 报表统计
        TreeItem<String> report = new TreeItem<>("报表统计");
        report.getChildren().addAll(
                new TreeItem<>("库存报表"),
                new TreeItem<>("操作日志")
        );

        root.getChildren().addAll(inventory, goods, report);

        TreeView<String> tree = new TreeView<>(root);

        // 为导航树添加事件处理
        tree.setOnMouseClicked(event -> {
            TreeItem<String> item = tree.getSelectionModel().getSelectedItem();
            if (item != null) {
                switch (item.getValue()){
                    case "库存查询":
                        showInventoryView();
                        break;
                    case "入库管理":
                        showInboundView();
                        break;
                    case "出库管理":
                        showOutboundView();
                        break;
                    case "货物信息":
                        showGoodsManagementView();
                        break;
                    case "库存报表":
                        showInventoryReportView();
                        break;
                    case "分类管理":
                        showCategoryManagementView();
                        break;
                    case "操作日志":
                        showOperationLogView();
                        break;
                    default:
                        // 其他菜单项的处理逻辑
                        break;
                }
            }
        });

        return tree;
    }

    private void showInboundView() {
        InboundView inboundView = new InboundView();
        contentArea.setCenter(inboundView);
    }
    private void showOutboundView() {
        OutboundView outboundView = new OutboundView();
        contentArea.setCenter(outboundView);
    }
    private void showInventoryView() {
        InventoryView inventoryView = new InventoryView();
        contentArea.setCenter(inventoryView);
    }
    // 新增显示库存报表视图的方法、//
    private void showInventoryReportView() {
        InventoryReportView inventoryReportView = new InventoryReportView();
        contentArea.setCenter(inventoryReportView);
    }
    private void showGoodsManagementView() {
        GoodsManagementView goodsManagementView = new GoodsManagementView();
        contentArea.setCenter(goodsManagementView);
    }
    private void showCategoryManagementView() {
        CategoryManagementView categoryManagementView = new CategoryManagementView();
        contentArea.setCenter(categoryManagementView);
    }
    private void showOperationLogView() {
        OperationLogView operationLogView = new OperationLogView();
        contentArea.setCenter(operationLogView);
    }
    public static void main(String[] args) {
        launch(args);
    }
}
