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

import com.yem.wm.bc.change.business.ChangeModelHelper;
import com.yem.wm.bc.change.business.ChangeResumeLogHelper;
import com.yem.wm.bc.common.CommonUtils;
import kd.bos.bill.BillOperationStatus;
import kd.bos.bill.BillShowParameter;
import kd.bos.bill.OperationStatus;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.ShowType;
import kd.bos.form.control.Button;
import kd.bos.form.control.Control;
import kd.bos.form.control.events.RowClickEventListener;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.FormOperate;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.list.BillList;
import kd.bos.list.ListShowParameter;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;

import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 夜子
 * @ClassName ChangeResumeFormPlugin
 * @description: 变更日志动态表单插件
 * @date 2023年08月10日
 * @version: 1.0
 */
public class ChangeResumeFormPlugin extends AbstractFormPlugin implements RowClickEventListener {

    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        FormShowParameter showParameter = this.getView().getFormShowParameter();
        Object srcbillId = showParameter.getCustomParam("srcbillid");
        Object xbillid = showParameter.getCustomParam("xbillid");
        this.getView().setEnable(false, "billinfo");
        if (!CommonUtils.isNull(srcbillId)) {
            QFilter qFilter3 = new QFilter("srcbillid", "=", srcbillId);
            QFilter qFilter2 = new QFilter("currentchangenumber", "!=", 0);
            DynamicObjectCollection num = QueryServiceHelper.query("yem_changeresume", "currentchangenumber", new QFilter[]{qFilter3, qFilter2}, "currentchangenumber");
            int currentchangenumber = num.size();
            this.getModel().setValue("currentchangenumber", currentchangenumber);
            this.getView().updateView("currentchangenumber");
            ListShowParameter showParameterMenu = new ListShowParameter();
            showParameterMenu.getOpenStyle().setShowType(ShowType.InContainer);
            showParameterMenu.getOpenStyle().setTargetKey("changerecord");
            showParameterMenu.setBillFormId("yem_changeresume");
            showParameterMenu.setStatus(OperationStatus.VIEW);
            showParameterMenu.setCustomParam("srcbillid", srcbillId);
            if (!CommonUtils.isNull(xbillid)) {
                showParameterMenu.setCustomParam("xbillid", xbillid);
            }
            this.getPageCache().put("childpageid", showParameterMenu.getPageId());

            this.getView().showForm(showParameterMenu);
        }

    }

    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);
        FormOperate operate = (FormOperate)args.getSource();
        if ("refresh".equals(operate.getOperateKey())) {
            this.getView().updateView();
        }

    }

    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        FormShowParameter showParameter = this.getView().getFormShowParameter();
        String srcbillno = (String)showParameter.getCustomParam("srcbillno");
        if (!CommonUtils.isNull(srcbillno)) {
            this.getModel().setValue("srcbillno", srcbillno);
        }

    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        String name = e.getProperty().getName();
        ChangeData changeData = e.getChangeSet()[0];
        Object newValue = changeData.getNewValue();
        if (name.equals("vnum")) {
            this.getView().getPageCache().put("vnum",String.valueOf(newValue));
            this.getView().getFormShowParameter().setCustomParam("vnum",String.valueOf(newValue));
        }
    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addClickListeners("highlight");
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
        String key = ((Control) evt.getSource()).getKey().toLowerCase();
        if (key.equals("highlight")) {
            IFormView view = this.getView();
            IDataModel model = this.getModel();

            FormShowParameter showParameter = view.getFormShowParameter();
            String vnum = (String) model.getValue("vnum");
            if (vnum == null || vnum.isEmpty()) {
                view.showTipNotification("请选择要比较的版本。");
                return;
            }
            long billID;
            String billentity;
             DynamicObject srcBill = null;
            DynamicObject xBill;
            String childPageId = this.getPageCache().get("childpageid");
            IFormView  childView = this.getView().getView(childPageId);
            BillList billList = (BillList)childView.getControl("billlistap");
            ListSelectedRow row = billList.getCurrentSelectedRowInfo();
            Long selectId = (Long)row.getPrimaryKeyValue();
            DynamicObject changeresume = BusinessDataServiceHelper.loadSingle(selectId, "yem_changeresume");
            if (changeresume != null) {
                billentity = (String)changeresume.get("xbillentity");
                billID = (Long)changeresume.get("xbillid");
                xBill = BusinessDataServiceHelper.loadSingle(billID, billentity);
            }else{
                view.showTipNotification(ResManager.loadKDString("版本信息异常。", "ChangeResumeFromPlugin_0", "yem-yc-changemodel"));
                return;
            }
            ListSelectedRowCollection AllRowCol= billList.getCurrentListAllRowCollection();
            for( ListSelectedRow rowNex : AllRowCol  ){
                Long selectIdNex = (Long)rowNex.getPrimaryKeyValue();
                DynamicObject changeresumeNex = BusinessDataServiceHelper.loadSingle(selectIdNex, "yem_changeresume");
                if (changeresumeNex != null) {
                    long billIDNex = (Long)changeresumeNex.get("xbillid");
                    String billentityNex = (String)changeresumeNex.get("xbillentity");
                    int changenumber = (Integer) changeresumeNex.get("currentchangenumber");
                    String version = changeresumeNex.getString("srcbillversion");
                    if (0 != changenumber) {
                        version  =  changeresumeNex.getString("srcbillversion")+ "." + changeresumeNex.get("srcbillsubversion");
                    }
                    if(vnum.equals(version)){
                        srcBill = BusinessDataServiceHelper.loadSingle(billIDNex, billentityNex);
                    }
                }
            }
            if(xBill!=null&&srcBill!=null){
                DynamicObject changeModel = ChangeModelHelper.getChangeModel4XBill(billentity);
                String crdheadjson  = ChangeResumeLogHelper.getBilljson(srcBill, xBill);
                String crdentryjson = ChangeResumeLogHelper.getBillEntryjson(srcBill, xBill, ChangeModelHelper.getXBillEntryAndOp(changeModel));
                Map<String, Object> customParams = new HashMap<>();
                customParams.put("crdheadjson",crdheadjson);
                customParams.put("crdentryjson",crdentryjson);

                this.showBillinfo(billID,billentity,customParams);
            }
        }
    }

    public void showBillinfo(long billID, String billentity,Map<String, Object> customParams ) {
        IFormView View = this.getView();
        if (!CommonUtils.isNull(billentity) && billID != 0L) {
            QFilter qf1 = new QFilter("xbillid", "=", billID);
            boolean changeReCord = QueryServiceHelper.exists("yem_changeresume", new QFilter[]{qf1});
            if (changeReCord) {
                View.setVisible(true, "billinfo");
                BillShowParameter showParameter = new BillShowParameter();
                customParams.put("InContainer","InContainer");
                showParameter.setCustomParams(customParams);
                showParameter.getOpenStyle().setShowType(ShowType.InContainer);
                showParameter.getOpenStyle().setTargetKey("billinfo"); //设定表单标识为billinfo
                showParameter.setFormId(billentity);
                showParameter.setPkId(billID);
                showParameter.setBillStatus(BillOperationStatus.VIEW);
                View.showForm(showParameter);
            } else {
                this.getView().showTipNotification(ResManager.loadKDString("数据已不存在，请刷新页面。", "ChangeResumeListPlugin_0", "yem-yc-changemodel", new Object[0]));
                View.setVisible(false, "billinfo");
            }
        } else {
            View.setVisible(false, "billinfo");
        }
    }

}