package com.yem.wm.es.salesorder.from;

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.dataentity.entity.DynamicObjectCollection;
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.TextEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;

import kd.bos.form.field.BasedataEdit;

import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.EventObject;

/**
 * @author xwudd
 * @date 18:30
 * @Description
 **/
public class SaleOrderProEdit extends AbstractBillPlugIn implements RowClickEventListener, BeforeF7SelectListener, HyperLinkClickListener {


    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        EntryGrid materialInfo = this.getControl("yem_es_materialinfo");
        materialInfo.addRowClickListener(this);
        BasedataEdit resourceid = this.getView().getControl("yem_resourcenog");
        if(resourceid != null){
            resourceid.addBeforeF7SelectListener(this);
        }
        EntryGrid detailedInfor = this.getControl("yem_es_materialinfo");
        detailedInfor.addHyperClickListener(this);
    }

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        IDataModel model = this.getModel();
        DynamicObject obj = BusinessDataServiceHelper.loadSingle(model.getDataEntity().getPkValue(), "yem_es_salesorder");
        if(YEM.isNotEmpty(obj)){
            DynamicObject billType = obj.getDynamicObject("yem_billtype");
            if(YEM.isNotEmpty(billType)){
                model.setValue("yem_billtypetext",billType.getString("name"));
            }
        }
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        String name = e.getProperty().getName();
        ChangeData changeData = e.getChangeSet()[0];
        int rowIndex = changeData.getRowIndex();
        switch (name) {
            case "yem_resourcenog" :
                ClientUtils.resouceToMaterial(model,view,rowIndex);
                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_es_materialinfo".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");
            }
        }
    }

    public void selectF7SourceNo(BeforeF7SelectEvent e, int row){
        IDataModel model = this.getModel();
        DynamicObject productModel = (DynamicObject)model.getValue("yem_productmodel", row);
        if(YEM.isNotEmpty(productModel)){
            DynamicObject products = productModel.getDynamicObject("yem_products");
            if(products != null){
                e.addCustomQFilter(new QFilter("yem_products",QCP.equals,products.getPkValue()));
            }
        }
    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent e) {
        String name = e.getProperty().getName();
        int row = e.getRow();
        switch(name){
            case "yem_resourcenog" :
                selectF7SourceNo(e,row);
                break;
        }
    }

    @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_es_materialinfo","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;
        }
    }

}



