package com.ecep.contract.controller.tab;

import com.ecep.contract.controller.contract.ContractItemWindowController;
import com.ecep.contract.service.InventoryService;
import com.ecep.contract.util.UITools;
import com.ecep.contract.vm.ContractItemViewModel;
import com.ecep.contract.vo.ContractItemVo;

import javafx.scene.control.Tab;
import javafx.util.converter.LocalDateTimeStringConverter;

/**
 * 合同项目基础信息标签页
 */
public class ContractItemTabSkinBase
        extends AbstEntityBasedTabSkin<ContractItemWindowController, ContractItemVo, ContractItemViewModel> {

    public ContractItemTabSkinBase(ContractItemWindowController controller) {
        super(controller);
    }

    @Override
    public Tab getTab() {
        return controller.baseInfoTab;
    }

    @Override
    public void initializeTab() {
        // 数据绑定
        controller.idLabel.textProperty().bind(viewModel.getId().asString());
        controller.codeField.textProperty().bindBidirectional(viewModel.getCode());
        controller.titleField.textProperty().bindBidirectional(viewModel.getTitle());
        controller.specificationField.textProperty().bindBidirectional(viewModel.getSpecification());
        controller.unitField.textProperty().bindBidirectional(viewModel.getUnit());

        // 财务信息绑定
        controller.exclusiveTaxPriceField.textProperty().bindBidirectional(viewModel.getExclusiveTaxPrice().asObject(),
                new javafx.util.converter.DoubleStringConverter());
        controller.taxRateField.textProperty().bindBidirectional(viewModel.getTaxRate().asObject(),
                new javafx.util.converter.DoubleStringConverter());
        controller.taxPriceField.textProperty().bindBidirectional(viewModel.getTaxPrice().asObject(),
                new javafx.util.converter.DoubleStringConverter());
        controller.quantityField.textProperty().bindBidirectional(viewModel.getQuantity().asObject(),
                new javafx.util.converter.DoubleStringConverter());
        controller.taxAmountField.textProperty().bindBidirectional(viewModel.getTaxAmount().asObject(),
                new javafx.util.converter.DoubleStringConverter());
        controller.exclusiveTaxAmountField.textProperty().bindBidirectional(
                viewModel.getExclusiveTaxAmount().asObject(),
                new javafx.util.converter.DoubleStringConverter());

        // 日期绑定
        controller.startDateField.valueProperty().bindBidirectional(viewModel.getStartDate());
        controller.endDateField.valueProperty().bindBidirectional(viewModel.getEndDate());

        // 其他信息绑定
        controller.refIdField.textProperty().bindBidirectional(viewModel.getRefId().asObject(),
                new javafx.util.converter.IntegerStringConverter());
        controller.remarkField.textProperty().bindBidirectional(viewModel.getRemark());

        // 设置只读字段
        LocalDateTimeStringConverter localDateTimeStringConverter = controller.getCurrentEmployee()
                .getLocalDateTimeStringConverter();

        controller.createDateLabel.textProperty().bindBidirectional(viewModel.getCreateDate(),
                localDateTimeStringConverter);
        controller.updateDateLabel.textProperty().bindBidirectional(viewModel.getUpdateDate(),
                localDateTimeStringConverter);

        UITools.autoCompletion(controller.updaterLabel, viewModel.getCreator(), controller.getEmployeeService());
        UITools.autoCompletion(controller.updaterLabel, viewModel.getUpdater(), controller.getEmployeeService());

        // 设置存货显示信息
        UITools.autoCompletion(controller.inventoryField, viewModel.getInventory(),
                getCachedBean(InventoryService.class));
    }

    @Override
    protected ContractItemVo save(ContractItemVo entity) {
        // 保存前自动计算金额
        viewModel.calculateAmounts();
        return super.save(entity);
    }

}