package com.yem.wm.im.clientdemand.form;


import com.yem.wm.es.Util.ZJUtils;
import com.yem.wm.im.clientdemand.utils.ClientUtils;
import com.yem.wm.utils.YEM;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.IFormView;
import kd.bos.form.control.Control;
import kd.bos.form.control.EntryGrid;
import kd.bos.form.control.events.RowClickEvent;
import kd.bos.form.control.events.RowClickEventListener;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.HyperLinkClickEvent;
import kd.bos.form.events.HyperLinkClickListener;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.sdk.plugin.Plugin;

import java.util.EventObject;

/**
 * 单据界面
 */
public class ClientDemandProFormPlugin extends AbstractBillPlugIn implements Plugin, BeforeF7SelectListener, RowClickEventListener, HyperLinkClickListener {
    @Override
    public void registerListener(EventObject e) {
        BasedataEdit resourceid = this.getView().getControl("yem_resourcenog");
        if(resourceid != null){
            resourceid.addBeforeF7SelectListener(this);
        }
        EntryGrid detailedInfor = this.getControl("yem_im_detailedinfor");
        detailedInfor.addRowClickListener(this);
        detailedInfor.addHyperClickListener(this);
    }
    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        IDataModel model = this.getModel();
        DynamicObject obj = BusinessDataServiceHelper.loadSingle(model.getDataEntity().getPkValue(), "yem_im_clientdemand");
        if(YEM.isNotEmpty(obj)){
            DynamicObject billType = obj.getDynamicObject("yem_billtype");
            if(YEM.isNotEmpty(billType)){
                model.setValue("yem_billtypetext",billType.getString("name"));
            }
        }
    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent e) {
        DynamicObject datas = this.getModel().getDataEntity(true);
        String key = e.getProperty().getName();
        QFilter qFilter = null;
        DynamicObject products = datas.getDynamicObject("yem_bd_products");
        switch (key){
            //明细信息 资源号过滤
            case "yem_resourcenog":
                int row = this.getModel().getEntryCurrentRowIndex("yem_im_detailedinfor");
                DynamicObject productmodel = (DynamicObject) this.getModel().getValue("yem_productmodel",row);
                String businesssort = (String) this.getModel().getValue("yem_businesssort",row);
                if("P".equals(businesssort)){
                    break;
                }
                if(productmodel == null){
                    this.getView().showMessage("请选择基础机型第"+ (row+1) +"行");
                    e.setCancel(true);
                }else {
                    Long yemproducts = productmodel.getLong("yem_products.id");
                    e.addCustomQFilter(new QFilter("yem_products",QCP.equals,yemproducts));
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        IDataModel model = this.getModel();
        IFormView view = this.getView();
        ChangeData changeData = e.getChangeSet()[0];
        String proName = e.getProperty().getName();
        int row = changeData.getRowIndex();
        switch (proName){
            case "yem_resourcenog":
                ClientUtils.resouceToMaterial(model,view,row);
                break;
            default:
                break;
        }
    }

    @Override
    public void entryRowClick(RowClickEvent e) {
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        Control control = (Control)e.getSource();
        String key = control.getKey();
        int row = e.getRow();
        if("yem_im_detailedinfor".equals(key)){
            String businessSort = (String)model.getValue("yem_businesssort", row);
            if("P".equals(businessSort)){
                view.setEnable(false,"yem_retrofitting","yem_advconbaritemap14","yem_advconbaritemap15");
            }
            if("Z".equals(businessSort)){
                view.setEnable(true,"yem_retrofitting","yem_advconbaritemap14","yem_advconbaritemap15");
            }
        }
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        super.afterDoOperation(e);
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        String operateKey = e.getOperateKey();
        switch (operateKey){
            case "createsourceapply" :
                ZJUtils.createSourceApplys(view,model,"yem_im_detailedinfor","A");
                break;
        }
    }


    @Override
    public void hyperLinkClick(HyperLinkClickEvent e) {
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        String fieldName = e.getFieldName();
        int rowIndex = e.getRowIndex();
        switch(fieldName){
            case "yem_sourceapplyno" :
                ZJUtils.openSourceApply(view,model,rowIndex);
                break;
        }
    }
}