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

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

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.form.FormShowParameter;
import kd.bos.form.control.AttachmentPanel;
import kd.bos.form.control.events.BeforeAttachmentRemoveEvent;
import kd.bos.form.control.events.BeforeAttachmentRemoveListener;
import kd.bos.form.control.events.UploadEvent;
import kd.bos.form.control.events.UploadListener;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.AbstractOperate;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.user.UserServiceHelper;

/**
 * @author 夜子
 * @ClassName MaterialpicuploadEdit
 * @description: 图片上传表单插件
 * @date 2023年07月25日
 * @version: 1.0
 */
public class MaterialpicuploadEdit extends AbstractBillPlugIn implements UploadListener, BeforeAttachmentRemoveListener{

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);
        AbstractOperate operate = (AbstractOperate) args.getSource();
        String key = operate.getOperateKey();
        if (key.equals("match") ) {
            if (this.getView().getModel().getDataChanged()) {
                this.getView().showErrorNotification(ResManager.loadKDString("请先保存单据！", "EtcmanualImportEdit_0", "bfcl_equipment"));
                args.cancel = true;
            }
        }
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        super.afterDoOperation(e);
        String operateKey = e.getOperateKey();
        if ("match".equals(operateKey)) {
            this.getView().invokeOperation("refresh");
        }
    }

    /**
     * 附件表格
     */
    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        AttachmentPanel attachmentPanel = this.getView().getControl("attachmentpanel");
        attachmentPanel.addUploadListener(this);
        attachmentPanel.addBeforeRemoveListener(this);
    }

    @Override
    public void afterUpload(UploadEvent evt) {
        Object[] Urls = evt.getUrls();
        for(Object Url:Urls){
            Map<String,Object>  urlmap = (Map) Url;
            String name = urlmap.get("name").toString();
           int rowinx = this.getModel().createNewEntryRow("yem_attachmentpanel");
           this.getModel().setValue("yem_attachmentname",name,rowinx);
           this.getModel().setValue("yem_syncstatus","A",rowinx);
        }
    }

    @Override
    public void afterRemove(UploadEvent evt) {
        DynamicObjectCollection attachmentpanelCol= this.getModel().getEntryEntity("yem_attachmentpanel");
        Object[] Urls = evt.getUrls();
        for(Object Url:Urls){
            Map<String,Object>  urlmap = (Map) Url;
            String name = urlmap.get("name").toString();
            for(int i = attachmentpanelCol.size()-1;i>=0;i--){
                DynamicObject attachmentpanel = attachmentpanelCol.get(i);
                String yem_attachmentname =  attachmentpanel.getString("yem_attachmentname");
                if(yem_attachmentname.equals(name)){
                    this.getModel().deleteEntryRow("yem_attachmentpanel",i);
                }
            }
        }
    }

    @Override
    public void beforeAttachmentRemove(BeforeAttachmentRemoveEvent e) {
        FormShowParameter showParameter = this.getView().getFormShowParameter();
        String status = showParameter.getStatus().toString();
        if (!status.equals("ADDNEW")) {
            long userId = UserServiceHelper.getCurrentUserId();
            DynamicObject bos_user = BusinessDataServiceHelper.loadSingleFromCache(userId, "bos_user");
            Map<String, Object> map = e.getAttachemnt();
            AttachmentPanel attachmentPanel = (AttachmentPanel) e.getSource();
            String uid = (String) map.get("uid");
            List<Map<String, Object>> attachmentData = attachmentPanel.getAttachmentData();
            for (Map<String, Object> data : attachmentData) {
                String uidAttachment = (String) data.get("uid");
                if (!StringUtils.isEmpty(uidAttachment)) {
                    if (uidAttachment.equals(uid)) {
                        String creator = data.get("creator").toString();
                        if (!creator.contains(bos_user.getString("name"))) {
                            e.setMsg("不能删除别人上传的附件!!!");
                            e.setCancel(true);
                        }
                    }
                }
            }
        }
    }

}
