package com.ecep.contract.controller.tab;

import java.time.LocalDate;

import com.ecep.contract.controller.company.AbstCompanyTableTabSkin;
import com.ecep.contract.controller.company.CompanyContactWindowController;
import com.ecep.contract.controller.company.CompanyWindowController;
import com.ecep.contract.controller.table.EditableEntityTableTabSkin;
import com.ecep.contract.model.CompanyContact;
import com.ecep.contract.service.CompanyContactService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.vm.CompanyContactViewModel;

import javafx.event.ActionEvent;
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-contact.fxml")
public class CompanyTabSkinContact
        extends AbstCompanyTableTabSkin<CompanyContact, CompanyContactViewModel>
        implements TabSkin, EditableEntityTableTabSkin<CompanyContact, CompanyContactViewModel> {

    /**
     * 以下是联系人列定义
     */
    public TableColumn<CompanyContactViewModel, Number> contactTable_idColumn;
    public TableColumn<CompanyContactViewModel, String> contactTable_nameColumn;
    public TableColumn<CompanyContactViewModel, String> contactTable_positionColumn;
    public TableColumn<CompanyContactViewModel, String> contactTable_phoneColumn;
    public TableColumn<CompanyContactViewModel, String> contactTable_emailColumn;
    public TableColumn<CompanyContactViewModel, String> contactTable_addressColumn;
    public TableColumn<CompanyContactViewModel, String> contactTable_u8CodeColumn;
    public TableColumn<CompanyContactViewModel, LocalDate> contactTable_createdColumn;

    public TextField contactSearchKeyField;
    public Button contactSearchBtn;
    public MenuItem contactTable_menu_refresh;
    public MenuItem contactTable_menu_add;
    public MenuItem contactTable_menu_del;
    @Setter
    private CompanyContactService contactService;

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

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


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

        contactTable_idColumn.setCellValueFactory(param -> param.getValue().getId());
        contactTable_nameColumn.setCellValueFactory(param -> param.getValue().getName());
        contactTable_positionColumn.setCellValueFactory(param -> param.getValue().getPosition());
        contactTable_phoneColumn.setCellValueFactory(param -> param.getValue().getPhone());
        contactTable_emailColumn.setCellValueFactory(param -> param.getValue().getEmail());
        contactTable_addressColumn.setCellValueFactory(param -> param.getValue().getAddress());
        contactTable_u8CodeColumn.setCellValueFactory(param -> param.getValue().getU8Code());
        contactTable_createdColumn.setCellValueFactory(param -> param.getValue().getCreated());

        contactTable_menu_refresh.setOnAction(this::onTableRefreshAction);
        contactTable_menu_add.setOnAction(this::onTableAddAction);
        contactTable_menu_del.setOnAction(this::onTableDeleteAction);


        super.initializeTab();
    }

    @Override
    protected void onTableAddAction(ActionEvent event) {
        CompanyContact contact = new CompanyContact();
        contact.setCompany(getParent());
        contact.setCreated(LocalDate.now());
        getViewModelService().save(contact);
        loadTableDataSet();
    }

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

    public CompanyContactService getViewModelService() {
        if (contactService == null) {
            contactService = getBean(CompanyContactService.class);
        }
        return contactService;
    }
}
