package com.yem.wm.bc.companytitle.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;

/**
 * @author xwudd
 * @date 18:47
 * @Description
 **/
public class CustomerBankInfo extends AbstractFormPlugin {
    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addClickListeners("btnok");
    }

    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(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.loadSingle(customer.getPkValue(),customer.getDynamicObjectType().getName());
            DynamicObjectCollection bankInfos = customer.getDynamicObjectCollection("entry_bank");
            for(DynamicObject bankInfo : bankInfos){
                int entryRow = model.createNewEntryRow("yem_bankinfor");
                model.setValue("yem_customerbankacc",bankInfo.getString("bankaccount"),entryRow);//银行账户
                model.setValue("yem_customeraccname",bankInfo.getLocaleString("accountname").getLocaleValue(),entryRow);//账户名称

                DynamicObject beBank = bankInfo.getDynamicObject("bank");
                if(beBank != null){
                    model.setValue("yem_customeropenbank",beBank.getString("name"),entryRow);//开户行名称
                    model.setValue("yem_customeracccountry",beBank.getDynamicObject("country"),entryRow);//开户国家
                }
                model.setValue("yem_customeraccurrency",bankInfo.getDynamicObject("currency"),entryRow);//账户币别
            }
        }
    }


    @Override
    public void click(EventObject e) {
        Control control = (Control) e.getSource();
        String key = control.getKey();
        switch (key){
            case "btnok" :
                backFill();
                break;
        }
    }


    /**
     * @description 回填信息
     * @author xwudd
     * return
     * @time 2023/7/26 17:47
     */

    public void backFill(){
        IFormView view = this.getView();
        IDataModel model = view.getModel();
        IFormView parentView = view.getParentView();
        IDataModel parentModel = parentView.getModel();
        EntryGrid bankInfor = view.getControl("yem_bankinfor");
        int[] selectRows = bankInfor.getSelectRows();
        if(selectRows.length > 1){
            view.showErrorNotification("只能选择一条银行信息！");
        }else if(selectRows.length < 1){
            view.showErrorNotification("请选择一条银行信息！");
        }else{
            parentModel.setValue("yem_customerbankacc",model.getValue("yem_customerbankacc",selectRows[0]));
            parentModel.setValue("yem_customeropenbank",model.getValue("yem_customeropenbank",selectRows[0]));
            parentModel.setValue("yem_customeraccname",model.getValue("yem_customeraccname",selectRows[0]));
            parentModel.setValue("yem_customeraccurrency",model.getValue("yem_customeraccurrency",selectRows[0]));
            parentModel.setValue("yem_customeracccountry",model.getValue("yem_customeracccountry",selectRows[0]));
            view.sendFormAction(parentView);
            view.close();
        }
    }
}
