package com.ecep.contract.controller.tab;

import java.time.LocalDate;

import org.springframework.beans.BeansException;

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.converter.EmployeeStringConverter;
import com.ecep.contract.model.Invoice;
import com.ecep.contract.service.InvoiceService;
import com.ecep.contract.service.PurchaseBillVoucherService;
import com.ecep.contract.service.YongYouU8Service;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.vm.InvoiceViewModel;

import javafx.scene.control.Button;
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-invoice.fxml")
public class CompanyTabSkinInvoice
        extends AbstCompanyTableTabSkin<Invoice, InvoiceViewModel>
        implements TabSkin, EditableEntityTableTabSkin<Invoice, InvoiceViewModel> {

    public TableColumn<InvoiceViewModel, Number> idColumn;
    public TableColumn<InvoiceViewModel, String> codeColumn;
    public TableColumn<InvoiceViewModel, LocalDate> dateColumn;
    public TableColumn<InvoiceViewModel, String> descriptionColumn;

    public TextField searchKeyField;
    public Button searchBtn;

    @Setter
    private PurchaseBillVoucherService purchaseBillVoucherService;
    @Setter
    private InvoiceService invoiceService;
    @Setter
    private YongYouU8Service u8Service;
    @Setter
    private EmployeeStringConverter employeeStringConverter;

    public CompanyTabSkinInvoice(CompanyWindowController controller) {
        super(controller);
    }

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

    @Override
    protected InvoiceService getViewModelService() {
        return getInvoiceService();
    }

    @Override
    public void initializeTab() {

        if (u8Service == null) {
            try {
                u8Service = getBean(YongYouU8Service.class);
            } catch (BeansException ignored) {
            }
        }

        searchKeyField.setOnKeyReleased(event -> {
            if (event.getCode() == KeyCode.ENTER) {
                searchBtn.fire();
            }
        });

        searchBtn.setOnAction(this::onTableRefreshAction);
        searchKeyField.setOnKeyReleased(event -> {
            if (event.getCode() == KeyCode.ENTER) {
                searchBtn.fire();
            }
        });

        idColumn.setCellValueFactory(param -> param.getValue().getId());
        codeColumn.setCellValueFactory(param -> param.getValue().getCode());
        dateColumn.setCellValueFactory(param -> param.getValue().getInvoiceDate());
        descriptionColumn.setCellValueFactory(param -> param.getValue().getDescription());
        super.initializeTab();
    }

    @Override
    protected void onTableRowDoubleClickedAction(InvoiceViewModel item) {
        // ContractWindowController.show(item, controller.root.getScene().getWindow());
    }

    private PurchaseBillVoucherService getPurchaseBillVoucherService() {
        if (purchaseBillVoucherService == null) {
            purchaseBillVoucherService = getBean(PurchaseBillVoucherService.class);
        }
        return purchaseBillVoucherService;
    }

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

    private InvoiceService getInvoiceService() {
        if (invoiceService == null) {
            invoiceService = getBean(InvoiceService.class);
        }
        return invoiceService;
    }
}
