package com.yem.em.bd.substitution.form;

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.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.FormOperate;

import java.util.EventObject;

/**
 * @author zhouc
 * @date 2023/10/24 15:34
 * @className SubstitutionFormEdit
 * @description 替换关系表单插件
 */
public class SubstitutionFormEdit extends AbstractBillPlugIn {
    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
//        this.addClickListeners("yem_enablesubno");
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        ChangeData[] changeData = e.getChangeSet();
        Object newValue = changeData[0].getNewValue();
        int row = changeData[0].getRowIndex();
        String key = e.getProperty().getName();
        int enrow = this.getModel().getEntryCurrentRowIndex("yem_entryentity");
        IDataModel model = this.getModel();
        IFormView view = this.getView();
        if (key.equals("yem_onumber")) {
            model.setValue("yem_pnumber", model.getValue("yem_onumber", enrow));
        }
        if (key.equals("yem_oname")) {
            model.setValue("yem_pname", model.getValue("yem_oname", enrow));
        }
        if (key.equals("yem_oenname")) {
            model.setValue("yem_penname", model.getValue("yem_oenname", enrow));
        }
        if (key.equals("yem_oqty")) {
            model.setValue("yem_pqty", model.getValue("yem_oqty", enrow));
        }
        if (key.equals("yem_repnumber")) {
            model.setValue("yem_tnumber", model.getValue("yem_repnumber", enrow));
        }
        if (key.equals("yem_repname")) {
            model.setValue("yem_tname", model.getValue("yem_repname", enrow));
        }
        if (key.equals("yem_repenname")) {
            model.setValue("yem_tenname", model.getValue("yem_repenname", enrow));
        }
        if (key.equals("yem_repenqty")) {
            model.setValue("yem_tqty", model.getValue("yem_repenqty", enrow));
        }
    }

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs e) {
        super.beforeDoOperation(e);
        IDataModel model = this.getModel();
        IFormView view = this.getView();
        DynamicObject dataEntity = this.getModel().getDataEntity(true);
        FormOperate operate = (FormOperate) e.getSource();
        String OperateKey = operate.getOperateKey();
        if (OperateKey.equals("newentry")) {
            DynamicObjectCollection dynamicObjectCollection = dataEntity.getDynamicObjectCollection("yem_entryentity");
            if (dynamicObjectCollection.size() == 1) {
                view.showErrorNotification("当前单据只能存在一条单据体，禁止由多条！");
                e.cancel = true;
            }
        }
    }
}
