package view;

import javafx.collections.FXCollections;
import javafx.geometry.Insets;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.control.cell.PropertyValueFactory;
import model.DataManager;
import model.OperationLog;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class OperationLogView extends BorderPane {
    
    private TableView<OperationLog> tableView;
    private DatePicker startDatePicker;
    private DatePicker endDatePicker;
    private ComboBox<String> operationTypeComboBox;
    
    public OperationLogView() {
        initializeUI();
    }
    
    private void initializeUI() {
        // 顶部搜索区域
        VBox topArea = createTopArea();
        setTop(topArea);
        
        // 中间表格区域
        tableView = createTableView();
        setCenter(tableView);
        
        // 加载日志数据
        loadLogData();
    }
    
    private VBox createTopArea() {
        VBox topArea = new VBox(10);
        topArea.setPadding(new Insets(10));
        
        // 搜索条件区域
        GridPane searchArea = new GridPane();
        searchArea.setHgap(10);
        searchArea.setVgap(10);
        
        // 时间范围选择
        Label dateRangeLabel = new Label("时间范围：");
        startDatePicker = new DatePicker();
        Label toLabel = new Label("至");
        endDatePicker = new DatePicker();
        
        // 操作类型选择
        Label operationTypeLabel = new Label("操作类型：");
        operationTypeComboBox = new ComboBox<>();
        operationTypeComboBox.getItems().addAll("全部", "入库", "出库", "修改");
        operationTypeComboBox.setValue("全部");
        
        // 搜索按钮
        Button searchButton = new Button("搜索");
        searchButton.setOnAction(e -> handleSearch());
        
        // 添加到网格
        searchArea.addRow(0, dateRangeLabel, startDatePicker, toLabel, endDatePicker);
        searchArea.addRow(1, operationTypeLabel, operationTypeComboBox, searchButton);
        
        topArea.getChildren().add(searchArea);
        return topArea;
    }
    
    private TableView<OperationLog> createTableView() {
        TableView<OperationLog> table = new TableView<>();
        
        TableColumn<OperationLog, String> idCol = new TableColumn<>("日志ID");
        idCol.setCellValueFactory(new PropertyValueFactory<>("id"));
        
        TableColumn<OperationLog, String> typeCol = new TableColumn<>("操作类型");
        typeCol.setCellValueFactory(new PropertyValueFactory<>("operationType"));
        
        TableColumn<OperationLog, String> targetCol = new TableColumn<>("操作对象");
        targetCol.setCellValueFactory(new PropertyValueFactory<>("operationTarget"));
        
        TableColumn<OperationLog, String> operatorCol = new TableColumn<>("操作人");
        operatorCol.setCellValueFactory(new PropertyValueFactory<>("operator"));
        
        TableColumn<OperationLog, LocalDateTime> timeCol = new TableColumn<>("操作时间");
        timeCol.setCellValueFactory(new PropertyValueFactory<>("operationTime"));
        timeCol.setCellFactory(column -> new TableCell<OperationLog, LocalDateTime>() {
            private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            
            @Override
            protected void updateItem(LocalDateTime item, boolean empty) {
                super.updateItem(item, empty);
                if (empty || item == null) {
                    setText(null);
                } else {
                    setText(formatter.format(item));
                }
            }
        });
        
        TableColumn<OperationLog, String> detailsCol = new TableColumn<>("操作详情");
        detailsCol.setCellValueFactory(new PropertyValueFactory<>("details"));
        
        table.getColumns().addAll(idCol, typeCol, targetCol, operatorCol, timeCol, detailsCol);
        return table;
    }
    
    private void loadLogData() {
        // 从DataManager获取日志数据
        tableView.setItems(FXCollections.observableArrayList(
            DataManager.getInstance().getOperationLogs()
        ));
    }
    
    private void handleSearch() {
        // 根据搜索条件过滤日志数据
        LocalDateTime startDate = startDatePicker.getValue() != null ? 
            startDatePicker.getValue().atStartOfDay() : null;
        LocalDateTime endDate = endDatePicker.getValue() != null ? 
            endDatePicker.getValue().atTime(23, 59, 59) : null;
        String operationType = operationTypeComboBox.getValue();
        
        tableView.setItems(FXCollections.observableArrayList(
            DataManager.getInstance().getFilteredOperationLogs(startDate, endDate, operationType)
        ));
    }
} 