package com.ecep.contract.controller.tab;

import com.ecep.contract.controller.bank.account.BankAccountWindowController;
import com.ecep.contract.controller.company.AbstCompanyTableTabSkin;
import com.ecep.contract.controller.company.CompanyWindowController;
import com.ecep.contract.controller.table.EditableEntityTableTabSkin;
import com.ecep.contract.controller.table.cell.BankTableCell;
import com.ecep.contract.model.Bank;
import com.ecep.contract.model.CompanyBankAccount;
import com.ecep.contract.service.BankService;
import com.ecep.contract.service.CompanyBankAccountService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.vm.CompanyBankAccountViewModel;

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;
import lombok.Setter;

/**
 * 子合同
 */
@FxmlPath("/ui/company/company-tab-bank-account.fxml")
public class CompanyTabSkinBankAccount
        extends AbstCompanyTableTabSkin<CompanyBankAccount, CompanyBankAccountViewModel>
        implements TabSkin, EditableEntityTableTabSkin<CompanyBankAccount, CompanyBankAccountViewModel> {
    /**
     * 银行账户
     */
    public TableColumn<CompanyBankAccountViewModel, Number> bankAccountTable_idColumn;
    public TableColumn<CompanyBankAccountViewModel, Bank> bankAccountTable_bankColumn;
    public TableColumn<CompanyBankAccountViewModel, String> bankAccountTable_openingBankColumn;
    public TableColumn<CompanyBankAccountViewModel, String> bankAccountTable_accountColumn;
    public TextField bankAccountSearchKeyField;
    public Button bankAccountSearchBtn;
    public MenuItem bankAccountTable_menu_refresh;
    public MenuItem bankAccountTable_menu_add;
    public MenuItem bankAccountTable_menu_del;

    @Setter
    private CompanyBankAccountService bankAccountService;
    @Setter
    private BankService bankService;

    public CompanyTabSkinBankAccount(CompanyWindowController controller) {
        super(controller);
    }
    @Override
    public Tab getTab() {
        return controller.bankAccountTab;
    }

    @Override
    public void initializeTab() {
        bankAccountSearchKeyField.setOnKeyReleased(event -> {
            if (event.getCode() == KeyCode.ENTER) {
                bankAccountSearchBtn.fire();
            }
        });
        bankAccountSearchBtn.setOnAction(this::onTableRefreshAction);

        bankAccountTable_idColumn.setCellValueFactory(param -> param.getValue().getId());
        bankAccountTable_bankColumn.setCellValueFactory(param -> param.getValue().getBank());
        bankAccountTable_bankColumn.setCellFactory(param -> new BankTableCell<>(getBankService()));

        bankAccountTable_openingBankColumn.setCellValueFactory(param -> param.getValue().getOpeningBank());
        bankAccountTable_accountColumn.setCellValueFactory(param -> param.getValue().getAccount());


        bankAccountTable_menu_refresh.setOnAction(this::onTableRefreshAction);
        bankAccountTable_menu_add.setOnAction(this::onTableAddAction);
        bankAccountTable_menu_del.setOnAction(this::onTableDeleteAction);

        super.initializeTab();
    }

    @Override
    protected void onTableRowDoubleClickedAction(CompanyBankAccountViewModel item) {
        BankAccountWindowController.show(item, controller.root.getScene().getWindow());
    }


    public CompanyBankAccountService getViewModelService() {
        if (bankAccountService == null) {
            bankAccountService = getBean(CompanyBankAccountService.class);
        }
        return bankAccountService;
    }

    public BankService getBankService() {
        if (bankService == null) {
            bankService = getBean(BankService.class);
        }
        return bankService;
    }

}
