package com.ecep.contract.controller.tab;

import java.time.LocalDate;
import java.util.Objects;

import org.controlsfx.glyphfont.Glyph;
import org.springframework.stereotype.Component;

import com.ecep.contract.controller.contract.AbstContractTableTabSkin;
import com.ecep.contract.controller.contract.ContractInvoiceWindowController;
import com.ecep.contract.controller.contract.ContractWindowController;
import com.ecep.contract.controller.table.EditableEntityTableTabSkin;
import com.ecep.contract.controller.table.cell.ContractInvoiceTableCell;
import com.ecep.contract.controller.table.cell.EmployeeTableCell;
import com.ecep.contract.controller.table.cell.NumberTableCell;
import com.ecep.contract.model.IdentityEntity;
import com.ecep.contract.service.ContractInvoiceService;
import com.ecep.contract.service.ContractService;
import com.ecep.contract.service.InvoiceService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.util.UITools;
import com.ecep.contract.vm.ContractInvoiceViewModel;
import com.ecep.contract.vo.ContractInvoiceVo;

import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleStringProperty;
import javafx.event.ActionEvent;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.Region;
import javafx.util.StringConverter;
import javafx.util.converter.CurrencyStringConverter;

@FxmlPath("/ui/contract/contract-tab-invoices.fxml")
public class ContractTabSkinInvoices
        extends AbstContractTableTabSkin<ContractInvoiceVo, ContractInvoiceViewModel>
        implements TabSkin, EditableEntityTableTabSkin<ContractInvoiceVo, ContractInvoiceViewModel> {

    private Tab tab;


    public TableColumn<ContractInvoiceViewModel, Number> idColumn;
    public TableColumn<ContractInvoiceViewModel, String> codeColumn;
    public TableColumn<ContractInvoiceViewModel, String> nameColumn;
    public TableColumn<ContractInvoiceViewModel, Integer> invoiceColumn;
    public TableColumn<ContractInvoiceViewModel, Number> amountColumn;
    public TableColumn<ContractInvoiceViewModel, String> remarkColumn;
    public TableColumn<ContractInvoiceViewModel, Integer> creatorColumn;
    public TableColumn<ContractInvoiceViewModel, LocalDate> createDateColumn;
    public TableColumn<ContractInvoiceViewModel, Integer> updaterColumn;
    public TableColumn<ContractInvoiceViewModel, LocalDate> updateDateColumn;

    public Label totalAmountLabel;

    public ContractTabSkinInvoices(ContractWindowController controller, Tab tab) {
        super(controller);
        this.tab = tab;
    }

    @Override
    public Tab getTab() {
        return tab;
    }

    @Override
    protected ContractInvoiceService getViewModelService() {
        return getBean(ContractInvoiceService.class);
    }

    @Override
    public void initializeTable() {
        super.initializeTable();
        getTableView().setEditable(true);

        // 初始化列
        idColumn.setCellValueFactory(new PropertyValueFactory<>("id"));
        idColumn.setPrefWidth(50);

        codeColumn.setCellValueFactory(new PropertyValueFactory<>("code"));
        codeColumn.setPrefWidth(120);
        codeColumn.setCellFactory(TextFieldTableCell.forTableColumn());

        nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
        nameColumn.setPrefWidth(200);
        nameColumn.setCellFactory(TextFieldTableCell.forTableColumn());

        // 发票列
        invoiceColumn.setCellValueFactory(new PropertyValueFactory<>("invoice"));
        invoiceColumn.setPrefWidth(100);
        invoiceColumn.setCellFactory(ContractInvoiceTableCell.forTableColumn(getCachedBean(InvoiceService.class)));

        // 金额列
        amountColumn.setCellValueFactory(new PropertyValueFactory<>("amount"));
        amountColumn.setPrefWidth(100);
        amountColumn.setCellFactory(NumberTableCell.forTableColumn(new CurrencyStringConverter(getLocale())));

        remarkColumn.setCellValueFactory(new PropertyValueFactory<>("remark"));
        remarkColumn.setPrefWidth(150);
        remarkColumn.setCellFactory(TextFieldTableCell.forTableColumn());

        // 创建人列
        creatorColumn.setCellValueFactory(new PropertyValueFactory<>("creator"));
        creatorColumn.setPrefWidth(80);
        creatorColumn.setCellFactory(EmployeeTableCell.forTableColumn(getEmployeeService()));

        createDateColumn.setCellValueFactory(new PropertyValueFactory<>("createDate"));
        createDateColumn.setPrefWidth(120);

        // 更新人列
        updaterColumn.setCellValueFactory(new PropertyValueFactory<>("updater"));
        updaterColumn.setPrefWidth(80);
        updaterColumn.setCellFactory(EmployeeTableCell.forTableColumn(getEmployeeService()));

        updateDateColumn.setCellValueFactory(new PropertyValueFactory<>("updateDate"));
        updateDateColumn.setPrefWidth(120);
    }

}