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

import com.alibaba.druid.util.StringUtils;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.bill.OperationStatus;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.CloseCallBack;
import kd.bos.form.FormShowParameter;
import kd.bos.form.ShowType;
import kd.bos.form.control.Control;
import kd.bos.form.control.EntryGrid;
import kd.bos.form.control.events.CellClickEvent;
import kd.bos.form.control.events.CellClickListener;
import kd.bos.form.control.events.ClickListener;
import kd.bos.form.control.events.RowClickEventListener;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.form.field.BasedataEdit;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

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

/**
 * @author power
 * @ClassName
 * @description: 收款单 点击 按钮打开动态表单插件
 * @date 2023/8/23
 */
public class GatheringFormPlugin extends AbstractBillPlugIn implements RowClickEventListener {
    @Override
    public void registerListener(EventObject e) {
        // TODO Auto-generated method stub
        super.registerListener(e);
        this.addClickListeners("yem_paymentact");
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
        Control source = (Control)evt.getSource();
        if ("yem_paymentact".equals(source.getKey()))
        {
            String billstatus = (String) this.getModel().getValue("billstatus");
            if (!billstatus.equals("A")) {
                return;
            }
            // TODO Auto-generated method stub



            FormShowParameter showParameter = new FormShowParameter();

            // 设置弹出页面的标识
            showParameter.setFormId("yem_paymentaccount");
            Map<String, Object> map = this.showInfoForm();

            // 存入获取到的动态表单数据
            showParameter.setCustomParams(map);
            // 状态
            showParameter.setStatus(OperationStatus.ADDNEW);
            //CloseCallBack参数：回调插件，回调标识
            showParameter.setCloseCallBack(new CloseCallBack(this, "yem_gathering"));
            // 设置弹出页面打开方式
            showParameter.getOpenStyle().setShowType(ShowType.Modal);

            // 打开动态表单
            this.getView().showForm(showParameter);
        }
    }

//    @Override
//    public void cellDoubleClick(CellClickEvent cellClickEvent) {
//
//    }

    private Map<String, Object> showInfoForm() {
        //获取收款单 客户
        DynamicObject yem_comegoman = (DynamicObject) this.getModel().getValue("yem_comegoman");
        //创建DynamicObjectCollection 保存字段值
        Map<String, Object> map = new HashMap<String, Object>();

        if (yem_comegoman !=null){
            DynamicObjectCollection entry = (DynamicObjectCollection) yem_comegoman.get("entry_bank");
            int i = 0;
            for (DynamicObject dynamicObject : entry) {
                //获取银行账号
                String bankaccount = dynamicObject.getString("bankaccount");
                //获取账户名称
                String accountname = dynamicObject.getString("accountname");
                //获取国际银行账户号码
                String iban = dynamicObject.getString("iban");
                //获取开户银行
                String bank = dynamicObject.getString("bank.name");
                //获取币别
                String currency = dynamicObject.getString("currency.name");

                if (bankaccount != null) {
                    map.put("yem_bankaccount"+i, bankaccount);
                }
                if (accountname != null) {
                    map.put("yem_accountname"+i, accountname);
                }
                if (iban != null) {
                    map.put("yem_iban"+i, iban);
                }
                if (bank != null) {
                    map.put("yem_bank"+i, bank);
                }
                if (currency != null) {
                    map.put("yem_currency"+i, currency);
                }
                i++;
            }
        }


        return map;
    }


    /**
     * 动态表单回填单据字段
     */
    private void setInwardInfo (Map<String, Object> values) {
        IDataModel model = this.getModel();
        //获取行数
        model.setValue("yem_paymentact", values.get("yem_bankaccount"));
        model.setValue("yem_paymentname", values.get("yem_accountname"));
        model.setValue("yem_paymentbank", values.get("yem_bank"));

//        model.setValue("yem_paymentaccount", values.get("yem_accountname"));
//        model.setValue("yem_paymentaccount", values.get("yem_iban"));
//        model.setValue("yem_paymentaccount", values.get("yem_bank"));
//        model.setValue("yem_paymentaccount", values.get("yem_currency"));
        // 回填动态表单的区间描述
        //SaveServiceHelper.update( dataEntity );//写入数据库
    }

    @Override
    public void closedCallBack(ClosedCallBackEvent closedCallBackEvent) {

        Object returnData = closedCallBackEvent.getReturnData();

        //判断标识是否匹配，并验证返回值不为空，不验证返回值可能会报空指针
        if (StringUtils.equals(closedCallBackEvent.getActionId(),"yem_gathering") && null != closedCallBackEvent.getReturnData()) {
            //这里返回对象为Object，可强转成相应的其他类型，
            // 单条数据可用String类型传输，返回多条数据可放入map中，也可使用json等方式传输
            HashMap<String, Object> values = (HashMap<String,Object>) returnData;
            this.setInwardInfo(values);
        }
    }
}
