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

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.ProjectCost;
import com.ecep.contract.service.ProjectCostService;
import com.ecep.contract.service.ProjectService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.util.UITools;
import com.ecep.contract.vm.ProjectCostViewModel;

import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Tab;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Window;
import javafx.stage.WindowEvent;

@Lazy
@Scope("prototype")
@Component
@FxmlPath("/ui/project/project-cost.fxml")
public class ProjectCostWindowController
        extends AbstEntityController<ProjectCost, ProjectCostViewModel> {
    public BorderPane root;
    public Tab baseInfoTab;
    public Tab itemTab;
    public Tab approvalTab;
    public Tab otherTab;

    public CheckBox standardPayWayField;
    public CheckBox standardContractTextField;
    public TextField noStandardPayWayTextField;
    public TextField noStandardContractTextField;

    public TextField stampTaxField;
    public TextField stampTaxFeeField;
    public TextField taxAndSurchargesField;
    public TextField taxAndSurchargesFeeField;
    public TextField grossProfitMarginField;

    public TextField inTaxAmountField;
    public TextField inExclusiveTaxAmountField;
    public TextField outTaxAmountField;
    public TextField outExclusiveTaxAmountField;

    public TextField applicantField;
    public TextField authorizerField;
    public TextField applyTimeField;
    public TextField authorizationTimeField;
    public TextArea descriptionField;
    public TextField authorizationFileField;
    public Button openFileBtn;
    public Button changeFileBtn;
    public Button exportExcelBtn;
    public Button importExcelBtn;

    public static void show(ProjectCost cost, Window window) {
        ProjectCostViewModel model = new ProjectCostViewModel();
        model.update(cost);
        show(ProjectCostWindowController.class, model, window);
    }

    @Override
    public void onShown(WindowEvent windowEvent) {
        super.onShown(windowEvent);
        root.getScene().getStylesheets().add("/ui/project/comm.css");
        Project project = viewModel.getProject().get();
        if (!ProxyUtils.isInitialized(project)) {
            project = getProjectService().findById(project.getId());
        }
        getTitle().set("[" + viewModel.getId().get() + "] " + project.getCode() + " 项目成本 Ver:"
                + viewModel.getVersion().getValue() + " ");

        exportExcelBtn.setOnAction(this::onExportExcelAction);
    }

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

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

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

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

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

}
