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

import kd.bos.bill.OperationStatus;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.form.ConfirmCallBackListener;
import kd.bos.form.MessageBoxOptions;
import kd.bos.form.MessageBoxResult;
import kd.bos.form.control.EntryGrid;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.form.events.MessageBoxClosedEvent;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.form.plugin.IFormPlugin;
import kd.bos.list.ListShowParameter;
import kd.bos.orm.ORM;
import nckd.jxccl.hrmp.hbpm.business.hr.PositionBillService;
import nckd.jxccl.hrmp.hbpm.common.hr.PositionBillConstant;

import java.util.EventObject;

/**
 * 岗位申请单表单插件
 * @author: jtd
 * @date: 2025-10-25 13:38
 */
public class PositionBillFormPlugin extends AbstractFormPlugin {

    private static final String NCKD_ENTRYENTITY_ADD = "nckd_entrtyentity_add";
    private static final String NCKD_ENTRYENTITY_INFO = "nckd_entrtyentity_info";
    private static final String NCKD_CHANGETYPE = "nckd_chagetype";

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

        // 生成临时单据ID
        genBillIdIfNotExist();
        this.getModel().setDataChanged(false);
    }

    /**
     * 生成临时单据ID
     */
    private void genBillIdIfNotExist() {
        long billId = this.getModel().getDataEntity().getLong("id");
        if (billId == 0L) {
            billId = ORM.create().genLongId(this.getView().getEntityId());
            this.getModel().setValue("id", billId);
        }
    }

    @Override
    public void itemClick(ItemClickEvent evt) {
        super.itemClick(evt);

        if ("tb_del".equals(evt.getItemKey())) {
            EntryGrid posdutyEntryGrid = getView().getControl(PositionBillConstant.NCKD_POSDUTYENTRYENTITY);
            int length = (posdutyEntryGrid.getSelectRows()).length;
            if (length > 0) {
                ConfirmCallBackListener deleteEntryCallBackListener = new ConfirmCallBackListener("del_row", this);
                getView().showConfirm(String.format("你已选中%s条记录，删除后将不可恢复，是否继续？", length), MessageBoxOptions.OKCancel, deleteEntryCallBackListener);
            } else {
                getView().showTipNotification("请选中需要操作的行。");
            }
        }
    }



    @Override
    public void confirmCallBack(MessageBoxClosedEvent event) {
        super.confirmCallBack(event);

        if ("del_row".equals(event.getCallBackId()) && event.getResult().getValue() == MessageBoxResult.Yes.getValue()) {
            EntryGrid strategyEntryGrid = (EntryGrid)getView().getControl("cooprelentryentity");
            int[] rows = strategyEntryGrid.getSelectRows();
            getModel().deleteEntryRows("cooprelentryentity", rows);
        }
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs afterDoOperationEventArgs) {
        super.afterDoOperation(afterDoOperationEventArgs);

        OperationResult afterOperationResult = afterDoOperationEventArgs.getOperationResult();
        if (!(afterOperationResult == null || afterOperationResult.isSuccess())) {
            return;
        }

        String opKey = afterDoOperationEventArgs.getOperateKey();
        switch (opKey) {
            case PositionBillConstant.NEWENTRY_ADD:
                PositionBillService.openViewForm(this.getView(), opKey, this.getPluginName(), null);
                break;
            case "newentry_change":
                PositionBillService.openViewForm(this.getView(), opKey, this.getPluginName(), null);
                break;
            case "deleteentry_add":
                PositionBillService.openViewForm(this.getView(), "add", this.getPluginName(), null);
                break;
            case "deleteentry_change":
                PositionBillService.openViewForm(this.getView(), "add", this.getPluginName(), null);
                break;
            case "edit_add":
                PositionBillService.openViewForm(this.getView(), "add", this.getPluginName(), null);
                break;
            case "edit_change":
                PositionBillService.openViewForm(this.getView(), "add", this.getPluginName(), null);
                break;
        }
    }

    @Override
    public void closedCallBack(ClosedCallBackEvent closedCallBackEvent) {
        super.closedCallBack(closedCallBackEvent);

        switch (closedCallBackEvent.getActionId()) {
            case "add":
                PositionBillService.openViewForm(this.getView(), "add", this.getPluginName(), null);
                break;
            case "change":
                PositionBillService.openViewForm(this.getView(), "change", this.getPluginName(), null);
                break;
            case "delete":
                PositionBillService.openViewForm(this.getView(), "delete", this.getPluginName(), null);
                break;
            case "edit":
        }

    }

}
