package yem.wm.cm.htqs.form;

import com.alibaba.fastjson.JSONObject;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.bill.BillShowParameter;
import kd.bos.bill.OperationStatus;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.exception.KDBizException;
import kd.bos.form.*;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.form.field.LargeTextEdit;
import kd.bos.ksql.util.StringUtil;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.AttachmentServiceHelper;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.bos.servicehelper.user.UserServiceHelper;
import yem.base.common.module.version.helper.NewVersionHelper;
import yem.base.common.utils.AttachmentUtil;
import yem.base.common.utils.YEM;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 单据界面插件
 */
public class BackfillContract extends AbstractBillPlugIn {

    private final static String backfilll = "yem_cm_attbackfilll";


    /**
     * 显示动态表单
     */
    @Override
    public void itemClick(ItemClickEvent evt) {
        String itemKey = evt.getItemKey().toLowerCase();
        IFormView view = this.getView();
        if (itemKey.equals("yem_attachment")) {
            showFormAttachment(this, view, this.getModel());
        }

    }

    /**
     * 显示附件回填动态表单
     *
     * @param plugin
     * @param view
     * @param model
     */
    private void showFormAttachment(AbstractBillPlugIn plugin, IFormView view, IDataModel model) {

        DynamicObject dataEntity = model.getDataEntity(true);
        String billno = dataEntity.getString("billno");
        DynamicObject newDataEntity = BusinessDataServiceHelper.newDynamicObject(backfilll);
        SimpleDateFormat format = new SimpleDateFormat("yyMMddHHmmss");
        String date = format.format(new Date());

        newDataEntity.set("billno","back_"+billno);
        newDataEntity.set("yem_modifier", UserServiceHelper.getCurrentUser("name"));
        newDataEntity.set("yem_receiptid",billno);
        //查库查出单据实体对象
        String orgName = dataEntity.getDataEntityType().getName();
        DynamicObject orgBillObject =
                BusinessDataServiceHelper.loadSingle("bos_entityobject", new QFilter[]{new QFilter("number", "=", orgName)});
        newDataEntity.set("yem_receipt",orgBillObject);
        newDataEntity.set("yem_attachmentcon_tag",dataEntity.get("yem_attachmentcon_tag"));

        OperationResult result = SaveServiceHelper.saveOperate(backfilll, new DynamicObject[]{newDataEntity}, OperateOption.create());
        if (!result.isSuccess()){
            view.showMessage("回填失败！！！");
            return;
        }

        BillShowParameter bsp = new BillShowParameter();
        bsp.setFormId("yem_cm_attbackfilll");
        bsp.setPkId(Long.valueOf(newDataEntity.getPkValue().toString()));
        bsp.setStatus(OperationStatus.EDIT);
        //打开方式
        OpenStyle openStyle = new OpenStyle();
        openStyle.setShowType(ShowType.MainNewTabPage);
        bsp.setOpenStyle(openStyle);
        bsp.setCloseCallBack( new CloseCallBack(plugin, view.getFormShowParameter().getFormId()));
        view.showForm(bsp);

        AttachmentUtil.uploadAttachmentPanel(dataEntity, newDataEntity);


    }


    /**
     * 动态表单关闭回调事件
     */
    @SuppressWarnings("unchecked")
    @Override
    public void closedCallBack(ClosedCallBackEvent e) {
        DynamicObject fromObj = (DynamicObject) e.getReturnData();
        DynamicObject toObj = this.getView().getModel().getDataEntity(true);
        if (fromObj != null) {
            toObj.set("yem_attachmentcon_tag",fromObj.get("yem_attachmentcon_tag"));
            this.getView().invokeOperation("save");
            AttachmentUtil.uploadAttachmentPanel(fromObj,toObj);
            this.getView().invokeOperation("refresh");
        }

    }

}