package com.yem.em.bd.asscomponent.from;

import com.yem.em.utils.AtlasUtils;
import com.yem.wm.utils.StringUtils;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.form.FormShowParameter;
import kd.bos.form.control.AttachmentPanel;
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.AfterDoOperationEventArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.AttachmentServiceHelper;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;

import java.util.*;

/**
 * @author zhouc
 * @date 2023/10/30 10:41
 * @className AsscomPonentEdit
 * @description 总成图册部件表单插件
 */
public class AsscomPonentEdit 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);
        }
        if ("yem_attachmentpanelap".equals(key)) {
            //附件面板中不包含文件地址 ，只有附加下载地址!!!
//            List list = new ArrayList<>();
//            Object[] urls = evt.getUrls();
//            if (urls.length > 0) {
//                for (int i = 0; i < urls.length; i++) {
//                    Map url = (Map) urls[0];
//                    list.add(url.get("uid"));
//                }
//            }
//            //调用保存，如不调用保存，无法获取附件物理地址
//            if (list.size() > 0) {
//                String yem_urls = this.getModel().getValue(IMAGE_LIST_URLS).toString();
//                QFilter qFilter = new QFilter("fnumber", QCP.in, list);
//                DynamicObjectCollection collection = QueryServiceHelper.query("bos_attachment",
//                        "id,ffileid", new QFilter[]{qFilter}, null);
//                if (collection.size() > 0) {
//                    for (int i = 0; i < collection.size(); i++) {
//                        yem_urls = yem_urls + collection.get(i).getString("ffileid").toString() + ";";
//                    }
//                }
//                this.getModel().setValue(IMAGE_LIST_URLS, yem_urls);
//            }
//            this.getView().updateView(IMAGE_LIST_URLS);
        }
    }

    @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);
        }
    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        ImageList imageList = this.getView().getControl(IMAGE_LIST_NUM);
        imageList.addUploadListener(this);
        // 侦听附件面板控件的文件上传事件
        AttachmentPanel attachmentPanel = this.getView().getControl("yem_attachmentpanelap");
        attachmentPanel.addUploadListener(this);
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        super.afterDoOperation(e);
        String operateKey = e.getOperateKey();
        DynamicObject bill = this.getModel().getDataEntity(true);

        if("save".equals(operateKey)){
            ImageList imageList = this.getView().getControl(IMAGE_LIST_NUM);
            List<Map<String, Object>> yemAttachmentpanelap =
                    AttachmentServiceHelper.getAttachments(bill.getDynamicObjectType().getName(), bill.getPkValue(), "yem_attachmentpanelap");
            if(yemAttachmentpanelap.size()>0){
                String imageUrls = (String) this.getModel().getValue(IMAGE_LIST_URLS);
                if (StringUtils.isNotEmpty(imageUrls)) {
                    String[] urls = imageUrls.split(";");
                    imageList.setImageUrls(urls);
                }
            }
            else{
                String[] urls = new String[]{""};
                imageList.setImageUrls(urls);
            }
            this.getView().invokeOperation("refresh");
        }
    }
}
