package com.ecep.contract.controller.employee;

import org.controlsfx.control.ListSelectionView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ecep.contract.controller.AbstEntityController;
import com.ecep.contract.model.Employee;
import com.ecep.contract.model.EmployeeRole;
import com.ecep.contract.service.EmployeeService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.vm.EmployeeViewModel;

import javafx.scene.control.CheckBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Window;
import javafx.stage.WindowEvent;
import lombok.Getter;

@Lazy
@Scope("prototype")
@Component
@FxmlPath("/ui/employee/employee.fxml")
public class EmployeeWindowController extends AbstEntityController<Employee, EmployeeViewModel> {
    private static final Logger logger = LoggerFactory.getLogger(EmployeeWindowController.class);

    /**
     * 显示界面
     */
    public static void show(EmployeeViewModel viewModel, Window window) {
        show(EmployeeWindowController.class, viewModel, window);
    }


    public BorderPane root;
    public TabPane tabPane;
    public Tab baseInfoTab;

    public TextField nameField;
    public TextField departmentField;
    public TextField aliasField;
    public TextField accountField;
    public TextField emailField;
    public TextField phoneField;
    public DatePicker createdField;
    public TextField codeField;
    public DatePicker leaveDateField;
    public DatePicker entryDateField;
    public CheckBox isActiveField;
    /*

     */
    public Tab rolesTab;
    public ListSelectionView<EmployeeRole> rolesField;

    public Tab loginHistoryTab;
    public Tab authBindTab;
    /*

     */
    public Tab permissionsTab;
    public TableView<Tab> permissionsTable;


    public static void show(Employee employee, Window owner) {
        EmployeeViewModel model = EmployeeViewModel.from(employee);
        show(model, owner);
    }


    @Autowired
    @Getter
    EmployeeService employeeService;

    @Override
    public void onShown(WindowEvent windowEvent) {
        super.onShown(windowEvent);
        getTitle().bind(viewModel.getName().map(name -> "[" + viewModel.getId().get() + "] " + name + " 员工详情"));

    }

    @Override
    protected void registerTabSkins() {
        registerTabSkin(baseInfoTab, tab -> new EmployeeTabSkinBase(this));
        registerTabSkin(rolesTab, tab -> new EmployeeTabSkinRole(this));
        registerTabSkin(loginHistoryTab, tab -> new EmployeeTabSkinLoginHistory(this));
        registerTabSkin(authBindTab, tab -> new EmployeeTabSkinAuthBind(this));
    }

    @Override
    public EmployeeService getViewModelService() {
        return employeeService;
    }


}
