package nckd.jxccl.hrmp.hbpm.plugin.form.hr;

import kd.bos.bill.OperationStatus;
import kd.bos.form.FormShowParameter;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.list.ListShowParameter;
import kd.hr.hbp.common.util.HRStringUtils;
import nckd.jxccl.hrmp.hbpm.common.hr.PositionBillConstant;

import java.math.BigInteger;
import java.util.EventObject;

/**
 * 岗位申请单分录表单插件
 * @author: jtd
 * @date: 2025-11-04 9:35
 */
public class PositionBillEntryFormPlugin extends AbstractFormPlugin implements BeforeF7SelectListener {

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);

        BasedataEdit adminorg = getView().getControl(PositionBillConstant.NCKD_ADMINORG);
        adminorg.addBeforeF7SelectListener(this);

        BasedataEdit position = getView().getControl(PositionBillConstant.NCKD_POSITION);
        position.addBeforeF7SelectListener(this);

        BasedataEdit parent = getView().getControl(PositionBillConstant.NCKD_PARENT);
        parent.addBeforeF7SelectListener(this);
    }

    @Override
    public void beforeBindData(EventObject e) {
        super.beforeBindData(e);

        FormShowParameter formShowParameter = getView().getFormShowParameter();
        setOrg();
        if (formShowParameter.getCustomParam("sequence") != null && getModel().getDataEntity(true).getInt(PositionBillConstant.NCKD_SEQUENCE) == 0) {
            getModel().setValue(PositionBillConstant.NCKD_SEQUENCE, formShowParameter.getCustomParam("sequence"));
        }
    }

    /**
     * 设置组织体系管理组织
     */
    private void setOrg() {
        String entityId = getView().getEntityId();
        if (PositionBillConstant.NCKD_POSBILLENTRYADD.equals(entityId) || PositionBillConstant.NCKD_POSBILLENTRYCHANGE.equals(entityId)) {
            Object orgParam = getView().getFormShowParameter().getCustomParam(PositionBillConstant.ORG_KEY);
            String isInit = getPageCache().get("isInit");
            if (orgParam != null && HRStringUtils.isEmpty(isInit)) {
                getModel().beginInit();
                getModel().setValue(PositionBillConstant.NCKD_ORG, orgParam);
                getModel().endInit();
                getPageCache().put("isInit", BigInteger.ONE.toString());
            }
        }
    }

    public void afterBindData(EventObject e) {
        super.afterBindData(e);

        getModel().setDataChanged(false);
        OperationStatus status = getView().getFormShowParameter().getStatus();
        if (OperationStatus.ADDNEW.equals(status) || OperationStatus.EDIT.equals(status)) {
            getView().setEnable(Boolean.TRUE, PositionBillConstant.ORG_KEY);
        }
    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent beforeF7SelectEvent) {
        String fieldKey = beforeF7SelectEvent.getProperty().getName();
        // 替换组织F7模板
        if (PositionBillConstant.NCKD_ADMINORG.equals(fieldKey)) {
            ListShowParameter showParameter = (ListShowParameter) beforeF7SelectEvent.getFormShowParameter();
            showParameter.setFormId(PositionBillConstant.HAOS_ORGTREELISTF7);
            //showParameter.setCaption("行政组织");
        }

        // 替换岗位F7模板
        if (PositionBillConstant.NCKD_POSITION.equals(fieldKey)) {
            ListShowParameter showParameter = (ListShowParameter) beforeF7SelectEvent.getFormShowParameter();
            showParameter.setFormId(PositionBillConstant.NCKD_NOTIME_POSTREELISTF7);
            showParameter.setCaption("岗位");
        }

        // 替换上级岗位F7模板
        if (PositionBillConstant.NCKD_PARENT.equals(fieldKey)) {
            ListShowParameter showParameter = (ListShowParameter) beforeF7SelectEvent.getFormShowParameter();
            showParameter.setFormId(PositionBillConstant.NCKD_NOTIME_POSTREELISTF7);
            showParameter.setCaption("上级岗位");
        }
    }

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);


    }
}
