package com.ecep.contract.controller.contract;

import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ecep.contract.controller.AbstEntityController;
import com.ecep.contract.controller.inventory.InventoryWindowController;
import com.ecep.contract.controller.tab.ContractItemTabSkinBase;
import com.ecep.contract.service.ContractItemService;
import com.ecep.contract.service.InventoryService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.util.UITools;
import com.ecep.contract.vm.ContractItemViewModel;
import com.ecep.contract.vm.InventoryViewModel;
import com.ecep.contract.vo.ContractItemVo;
import com.ecep.contract.vo.InventoryVo;

import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Window;
import javafx.stage.WindowEvent;

@Lazy
@Scope("prototype")
@Component
@FxmlPath("/ui/contract/contract-item.fxml")
public class ContractItemWindowController
        extends AbstEntityController<ContractItemVo, ContractItemViewModel> {

    public static void show(ContractItemVo contractItem, Window owner) {
        show(ContractItemViewModel.from(contractItem), owner);
    }

    /**
     * 显示界面
     */
    public static void show(ContractItemViewModel viewModel, Window window) {
        show(ContractItemWindowController.class, viewModel, window);
    }

    public BorderPane root;
    public Tab baseInfoTab;

    // 基本信息字段
    public Label idLabel;
    public TextField codeField;
    public TextField titleField;
    public TextField specificationField;
    public TextField unitField;
    public TextField inventoryField;
    public Button openInventoryBtn;

    // 财务信息字段
    public TextField exclusiveTaxPriceField;
    public TextField taxRateField;
    public TextField taxPriceField;
    public TextField quantityField;
    public TextField taxAmountField;
    public TextField exclusiveTaxAmountField;

    // 日期信息字段
    public DatePicker startDateField;
    public DatePicker endDateField;
    public TextField createDateLabel;
    public TextField updateDateLabel;
    public TextField creatorLabel;
    public TextField updaterLabel;

    // 其他信息
    public TextField refIdField;
    public TextArea remarkField;

    @Override
    public ContractItemService getViewModelService() {
        return getCachedBean(ContractItemService.class);
    }

    public InventoryService getInventoryService() {
        return getCachedBean(InventoryService.class);
    }

    public void onShown(WindowEvent windowEvent) {
        super.onShown(windowEvent);
        getTitle().bind(viewModel.getTitle()
                .map(title -> "[" + viewModel.getId() + "] " + title + " 合同内容详情"));
        root.getScene().getStylesheets().add("/ui/contract/contract.css");

    }

    @Override
    protected void registerTabSkins() {
        super.registerTabSkins();
        TabPane tabPane = baseInfoTab.getTabPane();
        ObservableList<Tab> tabs = tabPane.getTabs();
        registerTabSkin(baseInfoTab, tab -> new ContractItemTabSkinBase(this));
    }

    /**
     * 打开关联的存货信息
     */
    public void onOpenInventoryAction(ActionEvent event) {
        try {
            if (viewModel.getInventory() == null) {
                UITools.showAlertAndWait("没有关联的存货信息");
                return;
            }
            Integer inventoryId = viewModel.getInventory().get();
            InventoryVo inventory = getInventoryService().findById(inventoryId);
            if (inventory != null) {
                InventoryWindowController.show(InventoryViewModel.from(inventory), root.getScene().getWindow());
            } else {
                UITools.showAlertAndWait("未找到关联的存货信息");
            }
        } catch (Exception e) {
            UITools.showAlertAndWait("打开存货信息失败: " + e.getMessage());
        }
    }

    /**
     * 计算税额和金额
     */
    public void onCalculateAmountsAction(ActionEvent event) {
        try {
            viewModel.calculateAmounts();
            UITools.showAlertAndWait("金额计算完成");
        } catch (Exception e) {
            UITools.showAlertAndWait("计算失败: " + e.getMessage());
        }
    }

}