package com.ecep.contract.controller.tab;

import java.time.LocalDate;

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.customer.SalesOrderWindowController;
import com.ecep.contract.controller.table.cell.LocalDateFieldTableCell;
import com.ecep.contract.converter.EmployeeStringConverter;
import com.ecep.contract.service.SaleOrdersService;
import com.ecep.contract.vm.SalesOrderViewModel;
import com.ecep.contract.model.Employee;
import com.ecep.contract.model.SalesOrder;

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.control.cell.TextFieldTableCell;
import javafx.scene.input.KeyCode;
import lombok.Setter;

/**
 * 子合同
 */
@FxmlPath("/ui/contract/contract-tab-sale-orders.fxml")
public class ContractTabSkinSaleOrders
        extends AbstContractTableTabSkin<SalesOrder, SalesOrderViewModel>
        implements TabSkin {

    @Setter
    private SaleOrdersService saleOrdersService;
    @Setter
    private EmployeeStringConverter employeeStringConverter;

    public TableColumn<SalesOrderViewModel, Number> idColumn;
    public TableColumn<SalesOrderViewModel, String> codeColumn;
    public TableColumn<SalesOrderViewModel, Employee> employeeColumn;
    public TableColumn<SalesOrderViewModel, Employee> makerColumn;
    public TableColumn<SalesOrderViewModel, LocalDate> makerDateColumn;
    public TableColumn<SalesOrderViewModel, Employee> verifierColumn;
    public TableColumn<SalesOrderViewModel, LocalDate> verifierDateColumn;
    public TableColumn<SalesOrderViewModel, String> descriptionColumn;
    public MenuItem subContractTable_menu_refresh;
    public TextField contractSearchKeyField;
    public Button searchBtn;
    private Tab tab;

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

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

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

    @Override
    protected SaleOrdersService getViewModelService() {
        return getSaleOrdersService();
    }

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

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

        idColumn.setCellValueFactory(param -> param.getValue().getId());
        codeColumn.setCellValueFactory(param -> param.getValue().getCode());
        employeeColumn.setCellValueFactory(param -> param.getValue().getEmployee());
        employeeColumn.setCellFactory(TextFieldTableCell.forTableColumn(getEmployeeStringConverter()));
        makerColumn.setCellValueFactory(param -> param.getValue().getMaker());
        makerColumn.setCellFactory(TextFieldTableCell.forTableColumn(getEmployeeStringConverter()));
        makerDateColumn.setCellValueFactory(param -> param.getValue().getMakerDate());
        makerDateColumn.setCellFactory(LocalDateFieldTableCell.forTableColumn());

        verifierColumn.setCellValueFactory(param -> param.getValue().getVerifier());
        verifierColumn.setCellFactory(TextFieldTableCell.forTableColumn(getEmployeeStringConverter()));
        verifierDateColumn.setCellValueFactory(param -> param.getValue().getVerifierDate());
        verifierDateColumn.setCellFactory(LocalDateFieldTableCell.forTableColumn());

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

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

    private EmployeeStringConverter getEmployeeStringConverter() {
        if (employeeStringConverter == null) {
            employeeStringConverter = getBean(EmployeeStringConverter.class);
        }
        return employeeStringConverter;
    }

    SaleOrdersService getSaleOrdersService() {
        if (saleOrdersService == null) {
            saleOrdersService = getBean(SaleOrdersService.class);
        }
        return saleOrdersService;
    }
}
