package com.yem.em.bd.bomchange.dynamic;


//import json.JSON;
//import json.JSONArray;
//import json.JSONObject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.FormShowParameter;
import kd.bos.form.control.Button;
import kd.bos.form.control.Control;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.servicehelper.BusinessDataServiceHelper;

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

/**
 * @author zhouc
 * @date 2023/8/30 10:19
 * @className SelectReplaceEdit
 * @description 选择替换件动态表单插件
 */
public class SelectReplaceEdit extends AbstractFormPlugin {
    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        FormShowParameter showParameter = this.getView().getFormShowParameter();
        Map<String, Object> customParas = showParameter.getCustomParams();
        if (customParas != null) {
            Buildentryentity(customParas);
            this.getView().updateView("yem_entryentity");
        }
    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        Button btnok = this.getView().getControl("btnok");
        if (null != btnok) {
            this.addClickListeners(new String[]{"btnok"});
        }
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
        Control c = (Control) evt.getSource();
        String key = c.getKey().toLowerCase();
        if (key.equals("btnok")) {
            int row = this.getModel().getEntryCurrentRowIndex("yem_entryentity");
            DynamicObject dataEntity = this.getModel().getDataEntity(true);
            DynamicObjectCollection yem_entryentity = dataEntity.getDynamicObjectCollection("yem_entryentity");
            if (yem_entryentity.size() > 0) {
                Map<String, Object> map = new HashMap<>();
                DynamicObject entryentity = yem_entryentity.get(row);
                map.put("yem_enablesubno", entryentity.getString("yem_enablesubno"));
                map.put("yem_enablesubname", entryentity.getString("yem_enablesubname"));
                map.put("yem_enablesubqty", entryentity.getBigDecimal("yem_enablesubqty"));
//                map.put("yem_replace", entryentity.getString("yem_replace"));
//                map.put("yem_arfinarbillno", model.getValue("yem_arfinarbillno", i));
//                map.put("yem_arfinarbill", model.getValue("yem_arfinarbill", i));
//                map.put("yem_busrecamount", model.getValue("yem_busrecamount", i));
//                map.put("yem_amtscale", model.getValue("yem_amtscale", i));
//                map.put("yem_feecuramt", model.getValue("yem_feecuramt", i));
//                map.put("yem_insurcuramt", model.getValue("yem_insurcuramt", i));
//                map.put("yem_othercuramt", model.getValue("yem_othercuramt", i));
//                map.put("yem_busrecurrency", model.getValue("yem_busrecurrency", i));
                this.getView().returnDataToParent(map);
                this.getView().close();

            }
        }
    }

    /**
     * 根据入参构建单据数据
     *
     * @param customParas 传入参数
     */
    public void Buildentryentity(Map<String, Object> customParas) {
        IDataModel model = this.getModel();
        Map<String, String> map = new HashMap<>();
//        if(){}
//        DynamicObject component = BusinessDataServiceHelper.loadSingle(customParas.get("id"), customParas.get("FormId").toString());
//        if (component != null) {
//            DynamicObjectCollection yem_entryentity = component.getDynamicObjectCollection("yem_entryentity");
//        }
//        DynamicObjectCollection yem_entryentity = (DynamicObjectCollection)customParas.get("entryentity");
        JSONArray yem_entryentity = (JSONArray) JSON.parse(customParas.get("entryentity").toString());
        if (yem_entryentity.size() > 0) {
            for (int j = 0; j < yem_entryentity.size(); j++) {
                JSONObject entryentity = yem_entryentity.getJSONObject(j);
                String yem_enablesubno = entryentity.getString("yem_enablesubno");
                if (yem_enablesubno.length() > 0 && !map.containsKey(yem_enablesubno)) {
                    map.put(yem_enablesubno, yem_enablesubno);
                }
            }
            for (int i = 0; i < yem_entryentity.size(); i++) {
                JSONObject entryentity = yem_entryentity.getJSONObject(i);
                if (entryentity != null) {
                    String yem_enablesta = entryentity.getString("yem_enablesta");//A:启用  B:禁用
                    Boolean yem_isass = entryentity.getBoolean("yem_isass");
                    Boolean yem_isresource = entryentity.getBoolean("yem_isresource");//A:启用  B:禁用
                    String yem_subno = entryentity.getString("yem_subno");
                    if (yem_enablesta.equals("A")) {
                        if (!map.containsKey(yem_subno)) {
                            if (yem_isass || yem_isresource) {
                                int rowIndex = model.createNewEntryRow("yem_entryentity");
                                model.setValue("yem_enablesubno", entryentity.getString("yem_subno"), rowIndex);
                                model.setValue("yem_enablesubname", entryentity.getString("yem_subname"), rowIndex);
                                model.setValue("yem_enablesubqty", entryentity.getBigDecimal("yem_qty"), rowIndex);
                            }
                        }

                    }
                }
            }
        }
    }
}
