package yem.wm.bd.companybankinfor.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 yem.wm.bd.util.BothCardHelper;

import java.util.EventObject;

/**
 * @Description 公司银行信息
 * 1、加载界面时，默认获取《公司抬头》中的{银行信息}自动给单据体赋值
 * 2、选中行数据之后，点击确定给父页面返回数据
 * @Author: zhangh
 * @CreateTime: 2025-02-08  11:42
 */
public class CompanyBankInforDynamicPlugin 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 company = (DynamicObject) parentModel.getValue("yem_org");//获取父页面的【公司抬头】
        if (company != null) {
            String orgtype = (String) parentModel.getValue("yem_orgtype");//获取父页面的【组织类型】
            DynamicObject org = null;
            if ("bos_org".equals(orgtype)) {
                org = BothCardHelper.queryCompany(company);//系统参数是《业务单元》，根据《业务单元》获取《公司抬头》
            } else {
                org = BusinessDataServiceHelper.loadSingleFromCache(company.getLong("id"), company.getDataEntityType().getName());
            }

            DynamicObjectCollection banksentry = org.getDynamicObjectCollection("yem_banksentry");//获取《公司抬头》中的{银行信息}
            for (DynamicObject bank : banksentry) {
                int row = model.createNewEntryRow("yem_entryentity");//动态表单添加行
                model.setValue("yem_account", bank.get("yem_account"), row);//银行账号
                model.setValue("yem_accountnumber", bank.get("yem_accountnumber"), row);//银行账号
                model.setValue("yem_accountname", bank.get("yem_accountname"), row);//账户名称
                model.setValue("yem_bankaccount", bank.get("yem_bankaccount"), row);//开户银行
                model.setValue("yem_bankaddress", bank.get("yem_bankaddress"), row);//银行地址
                model.setValue("yem_swiftcode", bank.get("yem_swiftcode"), row);//swift code
            }
        }
    }

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

    /**
     * @return void
     * @Description: 点击确定给父页面返回信息
     * @function 功能
     * @purposes 用途
     */
    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");
    }
}
