package com.ecep.contract.controller.tab;

import java.time.LocalDateTime;

import com.ecep.contract.ContractPayWay;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.controller.contract.AbstContractTableTabSkin;
import com.ecep.contract.controller.contract.ContractWindowController;
import com.ecep.contract.controller.vendor.purchase.order.PurchaseOrderWindowController;
import com.ecep.contract.converter.EmployeeStringConverter;
import com.ecep.contract.service.CompanyService;
import com.ecep.contract.service.PurchaseOrdersService;
import com.ecep.contract.vm.PurchaseOrderViewModel;
import com.ecep.contract.vo.PurchaseOrderVo;
import com.ecep.contract.controller.table.cell.CompanyTableCell;
import com.ecep.contract.controller.table.cell.LocalDateTimeTableCell;

import javafx.scene.control.MenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TableColumn;

/**
 * 子合同
 */
@FxmlPath("/ui/contract/contract-tab-purchase-orders.fxml")
public class ContractTabSkinPurchaseOrders
        extends AbstContractTableTabSkin<PurchaseOrderVo, PurchaseOrderViewModel>
        implements TabSkin {

    public TableColumn<PurchaseOrderViewModel, Number> idColumn;
    public TableColumn<PurchaseOrderViewModel, String> codeColumn;
    public TableColumn<PurchaseOrderViewModel, Integer> table_makerColumn;
    public TableColumn<PurchaseOrderViewModel, LocalDateTime> table_makerDateColumn;
    public TableColumn<PurchaseOrderViewModel, Integer> table_verifierColumn;
    public TableColumn<PurchaseOrderViewModel, LocalDateTime> table_verifierDateColumn;
    public TableColumn<PurchaseOrderViewModel, Integer> table_closerColumn;
    public TableColumn<PurchaseOrderViewModel, LocalDateTime> table_closerDateColumn;
    public TableColumn<PurchaseOrderViewModel, String> table_descriptionColumn;

    public MenuItem table_menu_refresh;

    private Tab tab;

    public ContractTabSkinPurchaseOrders(ContractWindowController controller, Tab tab) {
        super(controller);
        this.tab = tab;
    }

    @Override
    protected PurchaseOrdersService getViewModelService() {
        return getPurchaseOrdersService();
    }

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

    @Override
    public Tab getTab() {
        return tab;
    }

    @Override
    public void initializeTab() {
        table_menu_refresh.setOnAction(this::onTableRefreshAction);

        idColumn.setCellValueFactory(param -> param.getValue().getId());
        codeColumn.setCellValueFactory(param -> param.getValue().getCode());

        EmployeeStringConverter converter = getEmployeeStringConverter();
        table_makerColumn.setCellValueFactory(param -> param.getValue().getMaker());
        table_makerColumn.setCellFactory(CompanyTableCell.forTableColumn(getCompanyService()));
        table_makerDateColumn.setCellValueFactory(param -> param.getValue().getMakerDate());
        table_makerDateColumn.setCellFactory(param -> new LocalDateTimeTableCell<>());
        table_verifierColumn.setCellValueFactory(param -> param.getValue().getMaker());
        table_verifierColumn.setCellFactory(CompanyTableCell.forTableColumn(getCompanyService()));
        table_verifierDateColumn.setCellValueFactory(param -> param.getValue().getVerifierDate());
        table_verifierDateColumn.setCellFactory(param -> new LocalDateTimeTableCell<>());
        table_closerColumn.setCellValueFactory(param -> param.getValue().getMaker());
        table_closerColumn.setCellFactory(CompanyTableCell.forTableColumn(getCompanyService()));
        table_closerDateColumn.setCellValueFactory(param -> param.getValue().getCloserDate());
        table_closerDateColumn.setCellFactory(param -> new LocalDateTimeTableCell<>());

        table_descriptionColumn.setCellValueFactory(param -> param.getValue().getDescription());
        super.initializeTab();
    }

    @Override
    protected void onTableRowDoubleClickedAction(PurchaseOrderViewModel item) {
        showInOwner(PurchaseOrderWindowController.class, item);
    }

    PurchaseOrdersService getPurchaseOrdersService() {
        return getBean(PurchaseOrdersService.class);
    }

    EmployeeStringConverter getEmployeeStringConverter() {
        return getBean(EmployeeStringConverter.class);
    }

    CompanyService getCompanyService() {
        return controller.getCompanyService();
    }
}
