package com.yem.wm.fm.gathering.dynamic;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.entity.OrmLocaleValue;
import kd.bos.form.FormShowParameter;
import kd.bos.form.control.Control;
import kd.bos.form.control.EntryGrid;
import kd.bos.form.control.events.RowClickEventListener;

import java.math.BigDecimal;
import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;

/**
 * @author power
 * @ClassName yem_paymentaccount
 * @description: 付款账户表单插件
 * @date 2023/8/23
 */
public class PaymentaccountFormPlugin extends AbstractBillPlugIn implements RowClickEventListener {
    @Override
    public void registerListener(EventObject e) {
        // TODO Auto-generated method stub
        //监听点击的行数
        super.registerListener(e);
        this.addClickListeners("btncancel","btnok");  //监听确认和取消按钮
    }

    @Override
    public void click(EventObject evt) {
        // TODO Auto-generated method stub
        super.click(evt);
        Control source = (Control)evt.getSource();
        // 判断是否为确认按钮
        if ("btnok".equals(source.getKey()))
        {
            // 获取单据体选中行
            EntryGrid entryGrid = this.getView().getControl("yem_entryentity");
            int[] selectRows = entryGrid.getSelectRows();
            if (selectRows.length > 1)
            {
                this.getView().showErrorNotification("只能选择一条数据！");
            }
            else
            {
                int index = this.getModel().getEntryCurrentRowIndex("yem_entryentity");//获取当前选中行
                // 获取区间描述
                String bankaccount = (String) this.getModel().getValue("yem_bankaccount",index);
                OrmLocaleValue accountname = (OrmLocaleValue ) this.getModel().getValue("yem_accountname",index);
                String bank = (String) this.getModel().getValue("yem_bank",index);
                HashMap<String,Object> hashMap = new HashMap<>();
                if (bankaccount != null)
                {
                    hashMap.put("yem_bankaccount", bankaccount);
                }
                if (bankaccount != null)
                {
                    hashMap.put("yem_accountname", accountname);
                }
                if (bankaccount != null)
                {
                    hashMap.put("yem_bank", bank);
                }
                this.getView().returnDataToParent(hashMap);
                this.getView().close();
            }
        }
        else
        {
            this.getView().returnDataToParent(null);
            this.getView().close();
        }
    }

    @Override
    public void afterCreateNewData(EventObject e) {
        // TODO Auto-generated method stub
        super.afterCreateNewData(e);

        FormShowParameter showParameter = this.getView().getFormShowParameter();
        DynamicObjectCollection entry = new DynamicObjectCollection();
        Map<String,Object> maps = new HashMap<>();
        maps = showParameter.getCustomParams();
        //指标 基础资料 获取到了 五个值  区间描述   指标区间从 指标区间含 得分从 得分含
        if (maps.size() != 0) {
            int size = maps.size()/5;
            for (int i = 0; i < size; i++) {
                String bankaccount = (String) maps.get("yem_bankaccount"+i);
                String accountname = (String) maps.get("yem_accountname"+i);
                String iban = (String) maps.get("yem_iban"+i);
                String bank = (String) maps.get("yem_bank"+i);
                String currency = (String) maps.get("yem_currency"+i);
                //添加分录
                if (i != 0) {
                    this.getModel().batchCreateNewEntryRow("yem_entryentity", 1);
                }
                //给 区间描述 得分从 得分到赋值
                this.getModel().setValue("yem_bankaccount",bankaccount,i);
                this.getModel().setValue("yem_accountname",accountname,i);
                this.getModel().setValue("yem_iban", iban,i);
                this.getModel().setValue("yem_bank", bank,i);
                this.getModel().setValue("yem_currency", currency,i);
            }
        }


    }
}
