package yem.wm.bd.supplierbankinfor.dynamic;

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.form.IFormView;
import kd.bos.form.control.Control;
import kd.bos.form.control.EntryGrid;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.EventObject;

/**
 * @Description 供应商银行信息-表单插件
 *               1、加载界面时，默认获取《供应商》中的{银行信息}自动给单据体赋值
 *               2、选中行数据之后，点击确定给父页面返回数据
 * @Author: zhangh
 * @CreateTime: 2025-02-08  16:52
 */
public class SupBankDynamicPlugin extends AbstractBillPlugIn {

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

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

        DynamicObject supplier = (DynamicObject) parentModel.getValue("yem_supplier");//获取父页面的【供应商】字段
        if (supplier != null) {
            supplier = BusinessDataServiceHelper.loadSingleFromCache(supplier.getLong("id"), supplier.getDataEntityType().getName());

            DynamicObjectCollection banksentry = supplier.getDynamicObjectCollection("entry_bank");//获取《供应商》中的{银行信息}
            for (DynamicObject bank : banksentry) {
                int row = model.createNewEntryRow("yem_entryentity");//动态表单单据体添加行数
                model.setValue("yem_accountnumber", bank.get("bankaccount"), row);//银行账号
                model.setValue("yem_accountname", bank.get("accountname"), row);//账户名称
                model.setValue("yem_bankaccount", bank.get("bank"), 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
     */
    private 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");
    }
}
