package yem.wm.bd.customermaninfor.dynamic;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.IFormView;
import kd.bos.form.control.Control;
import kd.bos.form.control.EntryGrid;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.EventObject;

/**
 * @Description 客户人员信息
 *              1、加载界面时，默认获取《客户》中的{联系人信息}以及{联系人信息}中【关联地值】属性【详细地址】、【微信】、【QQ】自动给单据体赋值
 *              2、选中行数据之后，点击确定给父页面返回数据
 * @Author: zhangh
 * @CreateTime: 2025-02-08  16:12
 */
public class CusManDynamicPlugin extends AbstractFormPlugin {

    @Override
    public void afterCreateNewData(EventObject e) {
        IFormView view = this.getView();
        IDataModel model = view.getModel();

        IFormView parentView = view.getParentView();
        IDataModel parentModel = parentView.getModel();

        DynamicObject customer = (DynamicObject) parentModel.getValue("yem_customer");//获取父页面的【客户】字段
        if (customer != null) {
            customer = BusinessDataServiceHelper.loadSingleFromCache(customer.getLong("id"), customer.getDataEntityType().getName());

            DynamicObjectCollection linkmanentry = customer.getDynamicObjectCollection("entry_linkman");//《客户》中的{联系人信息}
            for (DynamicObject linkman : linkmanentry) {
                int row = model.createNewEntryRow("yem_entryentity");//动态表单单据体添加行数
                model.setValue("yem_customeruser", linkman.get("contactperson"), row);//人员
                model.setValue("yem_customertel", linkman.get("phone"), row);//电话
                model.setValue("yem_customeremail", linkman.get("email"), row);//E-Mail
                model.setValue("yem_customerfax", linkman.get("fax"), row);//传真

                DynamicObject associatedaddress = linkman.getDynamicObject("associatedaddress");//关联地址
                if (associatedaddress != null) {
                    associatedaddress = BusinessDataServiceHelper.loadSingleFromCache(associatedaddress.getLong("id"), associatedaddress.getDataEntityType().getName());
                    model.setValue("yem_customervx", associatedaddress.get("yem_suppliervx"), row);//微信(公司)--微信
                    model.setValue("yem_customerqq", associatedaddress.get("yem_supplierqq"), row);//QQ(公司)--QQ
                    model.setValue("yem_customeraddr", associatedaddress.get("detailaddress"), row);//地址(客户)--详细地址
                }
            }
        }
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
        String key = ((Control) evt.getSource()).getKey();
        switch (key) {
            case "btnok"://确定按钮
                this.btnOk();//点击确定按钮返回数据
                break;
        }
    }

    /**
     * @Description: 点击确定给父页面返回信息
     * @function 功能
     * @purposes 用途
     * @return void
     */
    public void btnOk() {
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        int selectRows = ((EntryGrid) view.getControl("yem_entryentity")).getEntryState().getFocusRow();//获取选中行的行数
        DynamicObject row = model.getEntryRowEntity("yem_entryentity", selectRows);//获取选中行的数据
        view.returnDataToParent(row);
        view.close();
    }

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