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

import com.alibaba.fastjson.JSONObject;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.serialization.SerializationUtils;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.entity.tree.TreeNode;
import kd.bos.form.*;
import kd.bos.form.control.Control;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.form.events.*;
import kd.bos.form.operate.FormOperate;
import kd.bos.form.plugin.IFormPlugin;

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

/**
 * @author zhouc
 * @date 2023/8/30 9:31
 * @className BOMChangeFormEdit
 * @description BOM变更单表单插件
 */
public class BOMChangeFormEdit extends AbstractBillPlugIn {
    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        Map<String, Integer> map = new HashMap<>();
        DynamicObject dataEntity = this.getModel().getDataEntity(true);
        DynamicObjectCollection yem_entryentity = dataEntity.getDynamicObjectCollection("yem_entryentity");
        for (int j = 0; j < yem_entryentity.size(); j++) {
            DynamicObject entryentity = yem_entryentity.get(j);
            String yem_enablesubno = entryentity.getString("yem_enablesubno");
            if (yem_enablesubno.length() > 0) {
                if (!map.containsKey(yem_enablesubno)) {
                    map.put(yem_enablesubno, j);
                }
            }
        }
        for (int i = 0; i < yem_entryentity.size(); i++) {
            DynamicObject dynamicObject = yem_entryentity.get(i);
            String yem_subno = dynamicObject.getString("yem_subno");
            if (map.containsKey(yem_subno)) {
                this.getView().setEnable(false, i, "yem_replace", "yem_enablesubno", "yem_enablesubname", "yem_enablesubenname", "yem_enablesubqty");
            }
        }
    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addClickListeners("yem_enablesubno");
    }

    @Override
    public void beforePropertyChanged(PropertyChangedArgs e) {
        super.beforePropertyChanged(e);
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        String name = e.getProperty().getName();
        ChangeData changeData = e.getChangeSet()[0];
        int rowIndex = changeData.getRowIndex();
        Object newValue = changeData.getNewValue();
        Object oldValue = changeData.getOldValue();
        if (name.equals("yem_enablesubno")) {
            DynamicObject dataEntity = this.getModel().getDataEntity(true);
            DynamicObjectCollection entryentity = dataEntity.getDynamicObjectCollection("yem_entryentity");
            if (newValue != null && ((String) newValue).length() == 0) {
                model.setValue("yem_enablesubno", "", rowIndex);
                model.setValue("yem_enablesubname", "", rowIndex);
                model.setValue("yem_enablesubenname", "", rowIndex);
                model.setValue("yem_enablesubqty", new BigDecimal(0), rowIndex);
                for (int i = 0; i < entryentity.size(); i++) {
                    DynamicObject dynamicObject = entryentity.get(i);
                    String yem_subno = dynamicObject.getString("yem_subno");
                    String yem_enablesta = dynamicObject.getString("yem_enablesta");
                    if (oldValue.equals(yem_subno) && yem_enablesta.equals("A")) {
                        this.getView().setEnable(true, i, "yem_replace", "yem_enablesubno", "yem_enablesubname", "yem_enablesubenname", "yem_enablesubqty");
                    }
                }
            }
        }
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
        Control c = (Control) evt.getSource();
        String key = c.getKey().toLowerCase();
        IDataModel model = this.getModel();
        IFormView view = this.getView();
        if (key.equals("yem_enablesubno")) {
            int row = this.getModel().getEntryCurrentRowIndex("yem_entryentity");
            String yem_enablesta = (String) model.getValue("yem_enablesta", row);//A:启用  B:禁用
            boolean yem_isresource = (boolean) model.getValue("yem_isresource", row);
            boolean yem_isass = (boolean) model.getValue("yem_isass", row);
            if ("B".equals(yem_enablesta)) {
                if (yem_isresource || yem_isass) {
                    showSrcFieldForm("yem_em_selectreplace", "yem_enablesubno");
                } else {
                    int rows = row + 1;
                    view.showErrorNotification("当前行【" + rows + "】未获取到对应的总成或资源，禁止选择！");
                }
            } else {
                int rows = row + 1;
                view.showErrorNotification("当前行【" + rows + "】启用停用状态为启用，禁止选择！");
            }
        }
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        super.afterDoOperation(e);
        String operateKey = e.getOperateKey();
        if ("audit".equals(operateKey)) {
            this.getView().updateView("yem_entryentity");
        }
    }

    @Override
    public void closedCallBack(ClosedCallBackEvent e) {
        super.closedCallBack(e);
        String actionId = e.getActionId();
        IDataModel model = this.getModel();
        Object returnData = e.getReturnData();
        DynamicObject dataEntity = this.getModel().getDataEntity(true);
        if (actionId.equals("yem_enablesubno")) {
            if (returnData != null) {
                Map<String, Object> map = (Map) returnData;
                String yem_enablesubno = map.get("yem_enablesubno").toString();
                int row = this.getModel().getEntryCurrentRowIndex("yem_entryentity");
                model.setValue("yem_enablesubno", map.get("yem_enablesubno"), row);
                model.setValue("yem_enablesubname", map.get("yem_enablesubname"), row);
                model.setValue("yem_enablesubqty", map.get("yem_enablesubqty"), row);
                DynamicObjectCollection entryentity = dataEntity.getDynamicObjectCollection("yem_entryentity");
                for (int i = 0; i < entryentity.size(); i++) {
                    DynamicObject dynamicObject = entryentity.get(i);
                    String yem_subno = dynamicObject.getString("yem_subno");
                    String yem_enablesta = dynamicObject.getString("yem_enablesta");
                    if (yem_enablesubno.equals(yem_subno) && yem_enablesta.equals("A")) {
                        this.getView().setEnable(false, i, "yem_replace", "yem_enablesubno", "yem_enablesubname", "yem_enablesubenname", "yem_enablesubqty");
                    }
                }
            }
        }
    }

    /**
     * @MethodName showSrcFieldForm
     * @Author SC
     * @Description 打开 源单表单
     * @Date 14:37 2023/7/18
     **/
    private void showSrcFieldForm(String FormId, String type) {
        DynamicObject dataEntity = this.getModel().getDataEntity(true);
        FormShowParameter showParameter = new FormShowParameter();
        showParameter.setFormId(FormId);
        String id = dataEntity.getString("id");
        showParameter.getCustomParams().put("id", id);
        showParameter.getCustomParams().put("FormId", "yem_em_bomchange");
        DynamicObjectCollection entryentity = dataEntity.getDynamicObjectCollection("yem_entryentity");
        showParameter.getCustomParams().put("entryentity", entryentity);
        if (type.equals("yem_enablesubno")) {
            showParameter.setCloseCallBack(new CloseCallBack((IFormPlugin) this, "yem_enablesubno"));
        }
        showParameter.getOpenStyle().setShowType(ShowType.Modal);
        this.getView().showForm(showParameter);
    }


}
