package nckd.jxccl.hr.psms.plugin.form.adjust;

import kd.bos.form.ShowType;
import kd.bos.form.container.Tab;
import kd.bos.form.control.events.TabSelectEvent;
import kd.bos.form.control.events.TabSelectListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.list.ListShowParameter;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.report.ReportShowParameter;
import nckd.jxccl.hr.psms.common.PositionStructureConstant;

import java.util.EventObject;

/**
* 职位体系-动态调整
* @author W.Y.C
* @date 2025/10/11 18:28
* @version 1.0
*/
public class DynamicAdjustmentFormPlugin extends AbstractFormPlugin implements TabSelectListener {

    private final static Log logger = LogFactory.getLog(DynamicAdjustmentFormPlugin.class);

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        // 获取页签控件并添加监听
        Tab tab = (Tab)this.getControl(PositionStructureConstant.NCKD_TABAP);
        tab.addTabSelectListener(this);
    }

    @Override
    public void afterBindData(EventObject e) {
        //页面加载完成默认打开已生成动态调整列表
        switchTab();
    }
    @Override
    public void tabSelected(TabSelectEvent tabSelectEvent) {
        //切换tab页签
        switchTab();

    }

    /**
     * 切换页签（根据当前页签焦点加载对应页面）
     * @param
     * @return: void
     * @author W.Y.C
     * @date: 2025/09/10 17:16
     */
    private void switchTab() {
        Tab tab = (Tab)this.getView().getControl(PositionStructureConstant.NCKD_TABAP);
        String currentTab = tab.getCurrentTab();
        if (PositionStructureConstant.NCKD_ADJUSTED.equalsIgnoreCase(currentTab)) {
            //打开“已生成动态调整”列表
            openTargetPage(PositionStructureConstant.ADJUST_QUERY, PositionStructureConstant.NCKD_ADJUSTED);
        } else if (PositionStructureConstant.NCKD_UNADJUSTED.equalsIgnoreCase(currentTab)) {
            //打开“未生成动态调整”列表
            // openTargetPage(PositionStructureConstant.NCKD_UNADJUSTREPORT,PositionStructureConstant.NCKD_UNADJUSTED);
            ReportShowParameter reportShowParameter = new ReportShowParameter();
            reportShowParameter.setFormId(PositionStructureConstant.NCKD_UNADJUSTREPORT);
            reportShowParameter.getOpenStyle().setShowType(ShowType.InContainer);
            reportShowParameter.getOpenStyle().setTargetKey(PositionStructureConstant.NCKD_UNADJUSTED);
            reportShowParameter.setCaption("未生成动态调整");
            this.getView().showForm(reportShowParameter);
        }
    }

    /**
     * 在页签中打开表单
     * @param formId 需要打开的表单标识
     * @param targetKey 目标容器标识
     * @return: void
     * @author W.Y.C
     * @date: 2025/09/10 17:15
     */
    private void openTargetPage(String formId, String targetKey) {
        ListShowParameter parameter = new ListShowParameter();
        parameter.setBillFormId(formId);
        // 在容器内打开
        parameter.getOpenStyle().setShowType(ShowType.InContainer);
        // 指定页签容器标识
        parameter.getOpenStyle().setTargetKey(targetKey);
        this.getView().showForm(parameter);
    }
}
