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.ksql.util.StringUtil;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.EventObject;

/**
 * @author xwudd
 * @date 17:01
 * @Description
 **/
public class CompanyBankInfo 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 company = (DynamicObject)parentModel.getValue("yem_company");
        if(company != null){
            company = BusinessDataServiceHelper.loadSingle(company.getPkValue(),company.getDynamicObjectType().getName());
            DynamicObjectCollection bankInfos = company.getDynamicObjectCollection("yem_bankinfo");
            for(DynamicObject bankInfo : bankInfos){
                int entryRow = model.createNewEntryRow("yem_bankinfor");
                DynamicObject accountBanks = bankInfo.getDynamicObject("yem_bd_accountbanks");
                if(accountBanks != null){
                    model.setValue("yem_companybankacc",accountBanks.getString("bankaccountnumber"),entryRow);
                    model.setValue("yem_companyaccname",accountBanks.getString("name"),entryRow);
                }
                DynamicObject beBank = bankInfo.getDynamicObject("yem_bd_bebank");
                if(beBank != null){
                    model.setValue("yem_companyopenbank",beBank.getString("name"),entryRow);
                    model.setValue("yem_openingbank",beBank.getString("name_eng"),entryRow);
                }

                model.setValue("yem_companyswift",bankInfo.getString("yem_bankswift"),entryRow);
                model.setValue("yem_companybankaddress",bankInfo.getString("yem_bankaddress"),entryRow);
                model.setValue("yem_bankaddress",bankInfo.getString("yem_bankaddressen"),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_companybankacc",model.getValue("yem_companybankacc",selectRows[0]));
            parentModel.setValue("yem_companyopenbank",model.getValue("yem_companyopenbank",selectRows[0]));
            parentModel.setValue("yem_companyaccname",model.getValue("yem_companyaccname",selectRows[0]));
            parentModel.setValue("yem_companyswift",model.getValue("yem_companyswift",selectRows[0]));
            parentModel.setValue("yem_companybankaddress",model.getValue("yem_companybankaddress",selectRows[0]));
            parentModel.setValue("yem_openbankengaddress",model.getValue("yem_openingbank",selectRows[0]));
            parentModel.setValue("yem_bankengaddress",model.getValue("yem_bankaddress",selectRows[0]));
            view.sendFormAction(parentView);
            view.close();
        }
    }
}
