package com.yem.wm.es.shippingdetails.dynamic;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.IFormView;
import kd.bos.form.control.Control;
import kd.tmc.cim.common.util.StringUtils;

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

/**
 * @author 张立攀
 * @version 1.0
 * @description   整机编码 动态表单
 * @date 2023-09-04 14:53
 */
public class ZjnumberEdit extends AbstractBillPlugIn {
    private static JSONArray jSONArrays = null;

    @Override
    public void registerListener(EventObject e) {
        this.addClickListeners("btnok");
    }
    @Override
    public void afterCreateNewData(EventObject e) {
        Map<String, Object> map = this.getView().getFormShowParameter().getCustomParams();
        if(map != null){
            JSONArray jSONArray = (JSONArray) map.get("yem_vehicleno");
            jSONArrays = jSONArray;
            for(int i = 0;i < jSONArray.size();i++){
                this.getModel().createNewEntryRow("yem_entryentity");
                Object ob = jSONArray.get(i);
                JSONObject dynamicObject = (JSONObject) ob;
                if(dynamicObject != null){
                    this.getModel().setValue("yem_zjnumber",dynamicObject.getString("yem_vehicleno"),i);
                    this.getModel().setValue("yem_engineno",dynamicObject.getString("yem_engineno"),i);
                    this.getModel().setValue("yem_enginemodel",dynamicObject.getString("yem_enginemodel"),i);
                    this.getModel().setValue("yem_enginevendor",dynamicObject.getString("yem_enginevendor"),i);
                    this.getModel().setValue("yem_currentstate",dynamicObject.getString("yem_currentstate"),i);
                    this.getModel().setValue("yem_currentstatedate",dynamicObject.getDate("yem_currentstatedate"),i);
                    this.getModel().setValue("yem_resourceid",dynamicObject.getLong("id"),i);

                }
            }
        }


    }
    @Override
    public void click(EventObject evt) {

        Control control = (Control)evt.getSource();
        if(StringUtils.equals(control.getKey(),"btnok")){
            IFormView parentView = this.getView().getParentView();
            IDataModel model = parentView.getModel();
            int row = this.getModel().getEntryCurrentRowIndex("yem_entryentity");
            int parRow = model.getEntryCurrentRowIndex("yem_es_salesorder_z");
            String vehicleno = (String) this.getModel().getValue("yem_zjnumber",row);
            model.setValue("yem_vehicleno",vehicleno,parRow);
            String engineno = (String) this.getModel().getValue("yem_engineno",row);
            model.setValue("yem_engineno",engineno,parRow);
            String enginemodel = (String) this.getModel().getValue("yem_enginemodel",row);
            model.setValue("yem_enginemodel",enginemodel,parRow);
            String enginevendor = (String) this.getModel().getValue("yem_enginevendor",row);
            model.setValue("yem_enginevendor",enginevendor,parRow);
            String currentstate = (String) this.getModel().getValue("yem_currentstate",row);
            model.setValue("yem_currentstate",currentstate,parRow);
            Date currentstatedate = (Date) this.getModel().getValue("yem_currentstatedate",row);
            model.setValue("yem_currentstatedate",currentstatedate,parRow);

            //给其他值赋值
            JSONObject jsonObject = null;
            if(jSONArrays != null && jSONArrays.size() > row){
                jsonObject = (JSONObject) jSONArrays.get(row);
            }

            if(jsonObject != null){
                model.setValue("yem_yieldway",jsonObject.getString("yem_yieldway"),parRow);
                model.setValue("yem_prepareway",jsonObject.getString("yem_prepareway"),parRow);
                model.setValue("yem_engineno",jsonObject.getString("yem_engineno"),parRow);
                model.setValue("yem_enginemodel",jsonObject.getString("yem_enginemodel"),parRow);
                model.setValue("yem_enginevendor",jsonObject.getString("yem_enginevendor"),parRow);
                model.setValue("yem_currentstate",jsonObject.getString("yem_currentstate"),parRow);
                model.setValue("yem_currentstatedate",jsonObject.getDate("yem_currentstatedate"),parRow);
                model.setValue("yem_assembleupline",jsonObject.getString("yem_assembleupline"),parRow);
                model.setValue("yem_upline",jsonObject.getDate("yem_upline"),parRow);
                model.setValue("yem_assembledownline",jsonObject.getString("yem_assembledownline"),parRow);
                model.setValue("yem_downline",jsonObject.getDate("yem_downline"),parRow);
                model.setValue("yem_debugst",jsonObject.getString("yem_debugst"),parRow);
                model.setValue("yem_debugstatdate",jsonObject.getDate("yem_debugstatdate"),parRow);
                model.setValue("yem_waitforinspection",jsonObject.getString("yem_waitforinspection"),parRow);
                model.setValue("yem_waitforinspecdate",jsonObject.getDate("yem_waitforinspecdate"),parRow);
                model.setValue("yem_accuracytest",jsonObject.getString("yem_accuracytest"),parRow);
                model.setValue("yem_accuratdate",jsonObject.getDate("yem_accuratdate"),parRow);
                model.setValue("yem_qualitytest",jsonObject.getString("yem_qualitytest"),parRow);
                model.setValue("yem_qualitytestdate",jsonObject.getDate("yem_qualitytestdate"),parRow);
                model.setValue("yem_factputstordate",jsonObject.getDate("yem_factputstordate"),parRow);
                model.setValue("yem_backonline",jsonObject.getString("yem_backonline"),parRow);
                model.setValue("yem_backonlinedate",jsonObject.getDate("yem_backonlinedate"),parRow);
                model.setValue("yem_putintostorage",jsonObject.getString("yem_putintostorage"),parRow);
                model.setValue("yem_putintostoragedate",jsonObject.getDate("yem_putintostoragedate"),parRow);
                model.setValue("yem_vehicleremark",jsonObject.getString("yem_vehicleremark"),parRow);
                model.setValue("yem_soureceid",jsonObject.getLong("id"),parRow);
            }

            this.getView().sendFormAction(parentView);
            this.getView().close();

        }
    }
}
