package com.ecep.contract.controller.project.quotation;

import com.ecep.contract.util.ProxyUtils;
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.Project;
import com.ecep.contract.model.ProjectQuotation;
import com.ecep.contract.service.ProjectQuotationService;
import com.ecep.contract.service.ProjectService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.util.UITools;
import com.ecep.contract.vm.ProjectQuotationViewModel;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Tab;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.WindowEvent;

@Lazy
@Scope("prototype")
@Component
@FxmlPath("/ui/project/project-quotation.fxml")
public class ProjectQuotationWindowController
        extends AbstEntityController<ProjectQuotation, ProjectQuotationViewModel> {
    @FXML
    public BorderPane root;
    @FXML
    public Tab baseInfoTab;

    @FXML
    public Button importExcelBtn;
    @FXML
    public Button exportExcelBtn;

    @FXML
    public TextField applicantField;
    @FXML
    public TextField applyTimeField;
    @FXML
    public TextField authorizerField;
    @FXML
    public TextField authorizationTimeField;
    @FXML
    public HBox levelGroup;
    @FXML
    public RadioButton good_radio_btn;
    @FXML
    public RadioButton bad_radio_btn;
    @FXML
    public RadioButton poor_radio_btn;
    @FXML
    public CheckBox standardPayWayField;
    @FXML
    public TextField noStandardPayWayTextField;
    @FXML
    public TextField amountField;
    @FXML
    public TextField evaluationFileField;
    @FXML
    public Button evaluationFileBtn;

    @FXML
    public TextField authorizationFileField;
    @FXML
    public Button openFileBtn;
    @FXML
    public Button changeFileBtn;
    @FXML
    public TextArea descriptionField;

    ProjectService getProjectService() {
        return getCachedBean(ProjectService.class);
    }

    @Override
    public ProjectQuotationService getViewModelService() {
        return getCachedBean(ProjectQuotationService.class);
    }

    @Override
    public void onShown(WindowEvent windowEvent) {
        super.onShown(windowEvent);
        Project project = viewModel.getProject().get();
        if (!ProxyUtils.isInitialized(project)) {
            project = getProjectService().findById(project.getId());
        }
        getTitle().set("[" + viewModel.getId().get() + "] " + project.getCode() + " 项目报价");

        exportExcelBtn.setOnAction(this::onExportExcelAction);
    }

    private void onExportExcelAction(ActionEvent event) {
        ProjectQuotationExportAsExcelFile tasker = new ProjectQuotationExportAsExcelFile();
        tasker.setQuotation(getEntity());
        UITools.showTaskDialogAndWait("导出Excel", tasker, null);
    }

    @Override
    protected void registerTabSkins() {
        registerTabSkin(baseInfoTab, tab -> new ProjectQuotationTabSkinBase(this));
    }

    public void onRefreshAction(ActionEvent event) {
        refreshByButton((Button) event.getSource());
    }
}
