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

import com.yem.wm.utils.StringUtils;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.FormShowParameter;
import kd.bos.form.control.Control;
import kd.bos.form.control.Image;
import kd.bos.form.control.ImageList;
import kd.bos.form.control.events.UploadEvent;
import kd.bos.form.control.events.UploadListener;
import kd.bos.form.events.BeforeClosedEvent;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.bos.url.UrlService;

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

/**
 * @author zhouc
 * @date 2023/8/25 14:59
 * @className ComponentFormPlugIn
 * @description 设备部件（新）表单插件
 */
public class ComponentFormPlugIn extends AbstractBillPlugIn implements UploadListener {
    private final String IMAGE_LIST_NUM = "yem_imagelistap";
    private final String IMAGE_LIST_URLS = "yem_imageurl";
    @Override
    public void upload(UploadEvent evt) {
        UploadListener.super.upload(evt);
        String key = evt.getCallbackKey();
        if (key.equals(IMAGE_LIST_NUM)) {
            Object[] imageLists = evt.getUrls();
            for(int i =0;i<imageLists.length;i++){
                String url = imageLists[i].toString() + ";";
                String urls = this.getModel().getValue(IMAGE_LIST_URLS).toString();
                this.getModel().setValue(IMAGE_LIST_URLS, urls + url);
            }
            this.getView().updateView(IMAGE_LIST_URLS);
        }
    }

    @Override
    public void beforeBindData(EventObject e) {
        super.beforeBindData(e);
        DynamicObject bill = this.getModel().getDataEntity();
        FormShowParameter FormShow = this.getView().getFormShowParameter();
        Map<String, Object> values = FormShow.getCustomParams();
        String Nsttype = null;
        if (null != values && values.size() > 0) {
            if(values.get("Nsttype")!=null){
                Nsttype =  values.get("Nsttype").toString();
            }
        }
        if(Nsttype!=null){
            if(Nsttype.equals("modif")){
                bill.set("status","A");
                this.getModel().setValue("status","A");
            }
        }
    }

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        ImageList imageList = this.getView().getControl(IMAGE_LIST_NUM);
        String imageUrls = (String) this.getModel().getValue(IMAGE_LIST_URLS);
        if (StringUtils.isNotEmpty(imageUrls)) {
            String[] urls = imageUrls.split(";");
            imageList.setImageUrls(urls);
        }


        DynamicObject bill = this.getModel().getDataEntity();
        long equipmentlistid = bill.getLong("yem_equipmentlistid");
        if(equipmentlistid!=0L){
            DynamicObject NewEquipment = BusinessDataServiceHelper.loadSingle(equipmentlistid, "yem_em_equipmentlist");
            this.getModel().setValue("yem_equipmentlist",NewEquipment);
            bill.set("yem_equipmentlist",NewEquipment);
            this.getModel().setDataChanged(false);
        }

        FormShowParameter FormShow = this.getView().getFormShowParameter();
        Map<String, Object> values = FormShow.getCustomParams();
        String Nsttype = null;
        if (null != values && values.size() > 0) {
            if(values.get("Nsttype")!=null){
                Nsttype =  values.get("Nsttype").toString();
            }
        }
        if(Nsttype!=null){
            this.getView().setEnable(false,"createorg","parent","yem_equipmentlist","yem_basemodelib");
            this.getView().setVisible(true,"yem_toolbarap");
            this.getView().setVisible(false,"tbmain");
        }else{
            this.getView().setVisible(false,"yem_toolbarap");
        }

    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        ImageList imageList = this.getView().getControl(IMAGE_LIST_NUM);
        imageList.addUploadListener(this);
    }

    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        IDataModel Model= (IDataModel) e.getSource();
        DynamicObject bill = Model.getDataEntity();
        FormShowParameter FormShow = this.getView().getFormShowParameter();
        Map<String, Object> values = FormShow.getCustomParams();
        long equipmentlistid = 0L;
        long pid ;
        if (null != values && values.size() > 0) {
            if(values.get("equipmentlistid")!=null){
                equipmentlistid = (long) values.get("equipmentlistid");
                bill.set("yem_equipmentlistid",equipmentlistid);
                DynamicObject NewEquipment = BusinessDataServiceHelper.loadSingle(equipmentlistid, "yem_em_equipmentlist");
                Model.beginInit();
                this.getModel().setValue("yem_equipmentlist",NewEquipment);
                Model.endInit();
                this.getModel().setValue("yem_basemodelib",NewEquipment.get("yem_basemodelib"));

            }
            if(values.get("pid")!=null){
                pid = (long) values.get("pid");
                DynamicObject component = BusinessDataServiceHelper.loadSingle(pid, "yem_em_component");
                bill.set("parent",component);
                bill.set("createorg",component.get("createorg"));
                bill.set("org",component.get("org"));
                bill.set("useorg",component.get("useorg"));
                bill.set("ctrlstrategy",component.get("ctrlstrategy"));
                bill.set("level",component.getInt("level")+1);
            }
        }
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        IDataModel model = this.getModel();
        String name = e.getProperty().getName();
        ChangeData changeData = e.getChangeSet()[0];
        Object newValue = changeData.getNewValue();
        switch (name) {
            case "yem_equipmentlist":
                if(newValue!=null){
                    DynamicObject NewEquipment = (DynamicObject) newValue;
                    model.setValue("yem_equipmentlistid",NewEquipment.getLong("id"));
                }else{
                    model.setValue("yem_equipmentlistid",0L);
                }

                break;
            default:
                break;
        }
    }

    @Override
    public void beforeClosed(BeforeClosedEvent e) {
        super.beforeClosed(e);
        DynamicObject bill = this.getModel().getDataEntity();
        FormShowParameter FormShow = this.getView().getFormShowParameter();
        Map<String, Object> values = FormShow.getCustomParams();
        if (null != values && values.size() > 0) {
            if(values.get("Nsttype")!=null){
                long id = bill.getLong("id");
                if(id!=0L){
                    bill = BusinessDataServiceHelper.loadSingle(id, bill.getDynamicObjectType().getName());
                    bill.set("status","C");
                    SaveServiceHelper.save(new DynamicObject[] { bill });
                }
            }
        }
    }
}
