package com.ecep.contract.controller.tab;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

import com.ecep.contract.ContractPayWay;
import com.ecep.contract.controller.contract.AbstContractTableTabSkin;
import com.ecep.contract.controller.contract.ContractWindowController;
import com.ecep.contract.model.Contract;
import com.ecep.contract.service.ContractService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.vm.ContractViewModel;

import javafx.application.Platform;
import javafx.scene.control.Button;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;

/**
 * 子合同
 */
@FxmlPath("/ui/contract/contract-tab-sub-contract.fxml")
public class ContractTabSkinSubContract
        extends AbstContractTableTabSkin<Contract, ContractViewModel>
        implements TabSkin {

    public TableColumn<ContractViewModel, Number> subContractTable_idColumn;
    public TableColumn<ContractViewModel, String> subContractTable_nameColumn;
    public TableColumn<ContractViewModel, String> subContractTable_codeColumn;
    public TableColumn<ContractViewModel, LocalDate> subContractTable_setupDateColumn;
    public TableColumn<ContractViewModel, LocalDate> subContractTable_inureDateColumn;
    public TableColumn<ContractViewModel, LocalDate> subContractTable_orderDateColumn;
    public TableColumn<ContractViewModel, LocalDate> subContractTable_varyDateColumn;
    public MenuItem subContractTable_menu_refresh;
    public TextField contractSearchKeyField;
    public Button contractSearchBtn;

    public ContractTabSkinSubContract(ContractWindowController controller) {
        super(controller);
    }

    @Override
    public void initializeUIComponents() {
        super.initializeUIComponents();
        // 合同是收时，有关联子合同
        getTab().disableProperty().bind(viewModel.getPayWay().isEqualTo(ContractPayWay.RECEIVE).not());
    }

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

    @Override
    protected ContractService getViewModelService() {
        return getContractService();
    }

    @Override
    public Map<String, Object> getSpecification(Contract parent) {
        Map<String, Object> params = getSpecification();
        if (params == null) {
            params = new HashMap<>();
        }
        params.put("parentCode", parent.getCode());
        return params;
    }

    @Override
    public void initializeTab() {
        super.initializeTab();
        contractSearchKeyField.setOnKeyReleased(event -> {
            if (event.getCode() == KeyCode.ENTER) {
                contractSearchBtn.fire();
            }
        });

        contractSearchBtn.setOnAction(this::onTableRefreshAction);
        subContractTable_menu_refresh.setOnAction(this::onTableRefreshAction);

        subContractTable_idColumn.setCellValueFactory(param -> param.getValue().getId());
        subContractTable_nameColumn.setCellValueFactory(param -> param.getValue().getName());
        subContractTable_codeColumn.setCellValueFactory(param -> param.getValue().getCode());

        subContractTable_setupDateColumn.setCellValueFactory(param -> param.getValue().getSetupDate());
        subContractTable_inureDateColumn.setCellValueFactory(param -> param.getValue().getInureDate());
        subContractTable_orderDateColumn.setCellValueFactory(param -> param.getValue().getOrderDate());
        subContractTable_varyDateColumn.setCellValueFactory(param -> param.getValue().getVaryDate());

        Platform.runLater(() -> {
            getTableView().getSortOrder().add(subContractTable_codeColumn);
        });
    }

    @Override
    protected void onTableRowDoubleClickedAction(ContractViewModel item) {
        ContractWindowController.show(item, getTableView().getScene().getWindow());
    }

}
