package com.yem.wm.bc.change.list;

import com.yem.wm.bc.change.business.ChangeModelHelper;
import com.yem.wm.bc.change.common.consts.ChangeResumeFormConst;
import com.yem.wm.bc.common.enums.StatusEnum;
import kd.bos.bill.BillShowParameter;
import kd.bos.bill.OperationStatus;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.RefObject;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.form.*;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.events.MessageBoxClosedEvent;
import kd.bos.form.operate.FormOperate;
import kd.bos.list.IListView;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.util.StringUtils;
import java.util.List;

/**
 * @author 夜子
 * @ClassName SrcBillChangeListPlugi
 * @description: TODO
 * @date 2023年08月03日
 * @version: 1.0
 */
public class SrcBillChangeListPlugin extends AbstractListPlugin {
    private static final String OPPARAM_AFTERCONFIRM = "afterconfirm";
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);
        if (args.getListSelectedData() != null && args.getListSelectedData().size() != 0) {
            FormOperate operate = (FormOperate)args.getSource();
            if (operate.getOperateKey().equals("bizchange")) {
                if (args.getListSelectedData().getPrimaryKeyValues().length > 1) {
                    this.getView().showTipNotification(ResManager.loadKDString("仅支持单张单据变更，请选择1张单据。", "SrcBillChangeListPlugin_1", "yem-yc-changemodel"));
                    args.setCancel(true);
                    return;
                }

                RefObject<String> afterConfirm = new RefObject<>();
                if (!operate.getOption().tryGetVariableValue("afterconfirm", afterConfirm)) {
                    this.getPageCache().put(ChangeResumeFormConst.ENABLE_CHANGERESUME, "true");
                    ConfirmCallBackListener confirmCallBacks = new ConfirmCallBackListener("bizchange", this);
                    String confirmTip = ResManager.loadKDString("是否确认要对当前单据进行变更?", "SrcBillChangeListPlugin_0", "yem-yc-changemodel");
                    this.getView().showConfirm(confirmTip, MessageBoxOptions.YesNo, ConfirmTypes.Default, confirmCallBacks);
                    args.setCancel(true);
                }
            }

        }
    }

    public void confirmCallBack(MessageBoxClosedEvent evt) {
        super.confirmCallBack(evt);
        if (evt.getCallBackId().equals("bizchange") && evt.getResult() == MessageBoxResult.Yes) {
            OperateOption operateOption = OperateOption.create();
            operateOption.setVariableValue("afterconfirm", "true");
            operateOption.setVariableValue(ChangeResumeFormConst.ENABLE_CHANGERESUME, this.getPageCache().get(ChangeResumeFormConst.ENABLE_CHANGERESUME));
            this.getView().invokeOperation("bizchange", operateOption);
        }

    }

    public void afterDoOperation(AfterDoOperationEventArgs args) {
        super.afterDoOperation(args);
        IListView listview = (IListView)this.getView();
        FormOperate operate = (FormOperate)args.getSource();
        if (args.getOperationResult() != null && args.getOperationResult().isSuccess()) {
            List<Object> billPks;
            String xBillEntityID;
            if (args.getOperateKey().equals("bizchange")) {
                OperationResult opRes = args.getOperationResult();
                billPks = opRes.getSuccessPkIds();
                String srcBillEntity = listview.getListModel().getDataEntityType().getName();
                xBillEntityID = operate.getOption().getVariableValue("xBillEntityID", "");
                if (!billPks.isEmpty()) {
                    if (StringUtils.isEmpty(xBillEntityID)) {
                        DynamicObject changeModel = ChangeModelHelper.getChangeModel4SrcBill(srcBillEntity);
                        if (changeModel != null && changeModel.getDynamicObject("xbill") != null) {
                            xBillEntityID = changeModel.getDynamicObject("xbill").getString("id");
                        }
                    }

                    for (Object billPk : billPks) {
                        QFilter q1 = new QFilter("sourcebillid", "=", billPk);
                        QFilter q2 = new QFilter("billstatus", "=", StatusEnum.SAVE.getValue());
                        QFilter q3 = new QFilter("changestatus", "!=", "D");
                        DynamicObject xbill = QueryServiceHelper.queryOne(xBillEntityID, "id", new QFilter[]{q1, q2, q3});
                        BillShowParameter fsp = new BillShowParameter();
                        fsp.setFormId(xBillEntityID);
                        fsp.getOpenStyle().setShowType(ShowType.MainNewTabPage);
                        fsp.setPkId(xbill.get("id"));
                        fsp.setCustomParam(ChangeResumeFormConst.ENABLE_CHANGERESUME, this.getPageCache().get(ChangeResumeFormConst.ENABLE_CHANGERESUME));
                        this.getView().showForm(fsp);
                    }
                }
            } else {

                if (args.getOperateKey().equals("bizchangeresume")) {
                    List<Object> successbillids = args.getOperationResult().getSuccessPkIds();
                    if (successbillids.size() > 1) {
                        this.getView().showTipNotification(ResManager.loadKDString("仅支持单张单据查看变更履历，请选择1张单据。", "SrcBillChangeListPlugin_3", "yem-yc-changemodel"));
                    } else if (successbillids.size() == 1) {
                        long srcbillid = (Long)successbillids.get(0);
                        QFilter qFilter1 = new QFilter("srcbillid", "=", srcbillid);
                        QFilter qFilter2 = new QFilter("currentchangenumber", "!=", 0);
                        DynamicObject  changerecord = BusinessDataServiceHelper.loadSingleFromCache("yem_changeresume", new QFilter[]{qFilter1, qFilter2});
                        if (changerecord != null) {
                            FormShowParameter changeResume = new FormShowParameter();
                            changeResume.setFormId("yem_changeresumeform");
                            changeResume.getOpenStyle().setShowType(ShowType.MainNewTabPage);
                            changeResume.setStatus(OperationStatus.EDIT);
                            changeResume.setCustomParam("srcbillid", srcbillid);
                            changeResume.setCustomParam("srcbillno", changerecord.get("srcbillno"));
                            this.getView().showForm(changeResume);
                        } else {
                            this.getView().showTipNotification(ResManager.loadKDString("没有实际录入变更数据，暂未记录变更履历信息。", "XBillChangeListPlugin_2", "yem-yc-changemodel"));
                        }
                    }
                }
            }
        }

    }
}
