package com.ecep.contract.controller.contract;

import java.time.LocalDate;

import com.ecep.contract.ContractPayWay;
import com.ecep.contract.controller.contract.sale_order.SalesOrderWindowController;
import com.ecep.contract.controller.tab.TabSkin;
import com.ecep.contract.controller.table.cell.CompanyTableCell;
import com.ecep.contract.controller.table.cell.EmployeeTableCell;
import com.ecep.contract.controller.table.cell.LocalDateFieldTableCell;
import com.ecep.contract.converter.EmployeeStringConverter;
import com.ecep.contract.service.CompanyService;
import com.ecep.contract.service.SaleOrdersService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.vm.SalesOrderViewModel;
import com.ecep.contract.vo.SalesOrderVo;

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

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

    @Setter
    private SaleOrdersService saleOrdersService;
    private CompanyService companyService;

    @Setter
    private EmployeeStringConverter employeeStringConverter;

    public TableColumn<SalesOrderViewModel, Number> idColumn;
    public TableColumn<SalesOrderViewModel, String> codeColumn;
    /**
     * 业务员, Employee
     */
    public TableColumn<SalesOrderViewModel, Integer> employeeColumn;
    /**
     * 创建人, Employee
     */
    public TableColumn<SalesOrderViewModel, Integer> makerColumn;
    public TableColumn<SalesOrderViewModel, LocalDate> makerDateColumn;
    /**
     * 审核人, Employee
     */
    public TableColumn<SalesOrderViewModel, Integer> verifierColumn;
    public TableColumn<SalesOrderViewModel, LocalDate> verifierDateColumn;
    public TableColumn<SalesOrderViewModel, Number> refIdColumn;
    public TableColumn<SalesOrderViewModel, Number> taxRateColumn;
    public TableColumn<SalesOrderViewModel, String> customerAddressColumn;
    /**
     * 修改人, Employee
     */
    public TableColumn<SalesOrderViewModel, Integer> modifierColumn;
    public TableColumn<SalesOrderViewModel, LocalDate> modifierDateColumn;
    /**
     * 关闭人, Employee
     */
    public TableColumn<SalesOrderViewModel, Integer> closerColumn;
    public TableColumn<SalesOrderViewModel, LocalDate> closerDateColumn;
    public TableColumn<SalesOrderViewModel, String> descriptionColumn;
    public MenuItem subContractTable_menu_refresh;
    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() {
        super.initializeTab();
        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(EmployeeTableCell.forTableColumn(getEmployeeService()));
        makerColumn.setCellValueFactory(param -> param.getValue().getMaker());
        makerColumn.setCellFactory(EmployeeTableCell.forTableColumn(getEmployeeService()));
        makerDateColumn.setCellValueFactory(param -> param.getValue().getMakerDate());
        makerDateColumn.setCellFactory(LocalDateFieldTableCell.forTableColumn());
        verifierColumn.setCellValueFactory(param -> param.getValue().getVerifier());
        verifierColumn.setCellFactory(EmployeeTableCell.forTableColumn(getEmployeeService()));
        verifierDateColumn.setCellValueFactory(param -> param.getValue().getVerifierDate());
        verifierDateColumn.setCellFactory(LocalDateFieldTableCell.forTableColumn());
        
        // 设置新增字段的单元格值工厂和工厂类
        refIdColumn.setCellValueFactory(param -> param.getValue().getRefId());
        taxRateColumn.setCellValueFactory(param -> param.getValue().getTaxRate());
        customerAddressColumn.setCellValueFactory(param -> param.getValue().getCustomerAddress());
        modifierColumn.setCellValueFactory(param -> param.getValue().getModifier());
        modifierColumn.setCellFactory(EmployeeTableCell.forTableColumn(getEmployeeService()));
        modifierDateColumn.setCellValueFactory(param -> param.getValue().getModifierDate());
        modifierDateColumn.setCellFactory(LocalDateFieldTableCell.forTableColumn());
        closerColumn.setCellValueFactory(param -> param.getValue().getCloser());
        closerColumn.setCellFactory(EmployeeTableCell.forTableColumn(getEmployeeService()));
        closerDateColumn.setCellValueFactory(param -> param.getValue().getCloserDate());
        closerDateColumn.setCellFactory(LocalDateFieldTableCell.forTableColumn());

        descriptionColumn.setCellValueFactory(param -> param.getValue().getDescription());

    }

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

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

    CompanyService getCompanyService() {
        if (companyService == null) {
            companyService = getBean(CompanyService.class);
        }
        return companyService;
    }
}
