package com.ecep.contract.controller.vendor;

import java.time.LocalDate;

import com.ecep.contract.controller.tab.TabSkin;
import com.ecep.contract.controller.table.cell.EmployeeTableCell;
import com.ecep.contract.controller.table.cell.VendorCatalogTableCell;
import com.ecep.contract.service.VendorEntityService;
import com.ecep.contract.service.VendorCatalogService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.vm.CompanyVendorEntityViewModel;
import com.ecep.contract.vo.VendorEntityVo;

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

@FxmlPath("/ui/company/vendor/vendor-tab-entity.fxml")
public class VendorTabSkinEntity
        extends AbstVendorTableTabSkin<VendorEntityVo, CompanyVendorEntityViewModel>
        implements TabSkin {

    // 关联项 tab
    public TableColumn<CompanyVendorEntityViewModel, Number> entityTable_idColumn;
    public TableColumn<CompanyVendorEntityViewModel, Integer> entityTable_catalogColumn;
    public TableColumn<CompanyVendorEntityViewModel, String> entityTable_nameColumn;
    public TableColumn<CompanyVendorEntityViewModel, String> entityTable_abbNameColumn;
    public TableColumn<CompanyVendorEntityViewModel, String> entityTable_codeColumn;
    public TableColumn<CompanyVendorEntityViewModel, Integer> entityTable_creatorColumn;
    public TableColumn<CompanyVendorEntityViewModel, LocalDate> entityTable_modifyDateColumn;
    public TableColumn<CompanyVendorEntityViewModel, Integer> entityTable_modifierColumn;
    public TableColumn<CompanyVendorEntityViewModel, LocalDate> entityTable_developDateColumn;
    public TableColumn<CompanyVendorEntityViewModel, LocalDate> entityTable_updatedDateColumn;

    public MenuItem entityTable_menu_refresh;
    public MenuItem entityTable_menu_del;

    public VendorTabSkinEntity(VendorWindowController controller) {
        super(controller);
    }

    VendorEntityService getVendorEntityService() {
        return getCachedBean(VendorEntityService.class);
    }

    @Override
    protected VendorEntityService getViewModelService() {
        return getVendorEntityService();
    }

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

    @Override
    public void initializeTab() {
        super.initializeTab();

        entityTable_idColumn.setCellValueFactory(param -> param.getValue().getId());
        entityTable_nameColumn.setCellValueFactory(param -> param.getValue().getName());
        entityTable_abbNameColumn.setCellValueFactory(param -> param.getValue().getAbbName());
        entityTable_codeColumn.setCellValueFactory(param -> param.getValue().getCode());
        initializeEntityTabCatalogColumn(entityTable_catalogColumn);

        entityTable_developDateColumn.setCellValueFactory(param -> param.getValue().getDevelopDate());
        entityTable_modifyDateColumn.setCellValueFactory(param -> param.getValue().getModifyDate());
        entityTable_creatorColumn.setCellValueFactory(param -> param.getValue().getCreatorId());
        entityTable_creatorColumn.setCellFactory(param -> new EmployeeTableCell<>(getEmployeeService()));
        entityTable_modifierColumn.setCellValueFactory(param -> param.getValue().getModifierId());
        entityTable_modifierColumn.setCellFactory(param -> new EmployeeTableCell<>(getEmployeeService()));
        entityTable_updatedDateColumn.setCellValueFactory(param -> param.getValue().getUpdatedDate());

        entityTable_menu_refresh.setOnAction(this::onTableRefreshAction);
        entityTable_menu_del.setOnAction(this::onTableDeleteAction);
    }

    private void initializeEntityTabCatalogColumn(TableColumn<CompanyVendorEntityViewModel, Integer> column) {
        column.setCellValueFactory(param -> param.getValue().getCatalogId());
        column.setCellFactory(VendorCatalogTableCell.forTableColumn(getBean(VendorCatalogService.class)));

    }
}
