package yem.wm.cm.htqs.form;

import com.alibaba.excel.util.StringUtils;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.*;
import kd.bos.form.control.Button;
import kd.bos.form.control.Control;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.form.events.MessageBoxClosedEvent;
import kd.bos.form.events.PreOpenFormEventArgs;
import kd.bos.form.plugin.AbstractFormPlugin;
import yem.base.common.module.version.helper.NewVersionHelper;
import yem.base.common.utils.AttachmentUtil;

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

/**
 * 附件回填单据插件
 */
public class AttachmentBackFormPlugIn  extends AbstractFormPlugin {

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        Button backbut = this.getView().getControl("yem_backbut");
        Button cancelbut = this.getView().getControl("yem_cancelbut");
        cancelbut.addClickListener(this);
        backbut.addClickListener(this);
//        addItemClickListeners(new String[]{"tbmain"});
    }


    /**
     * 显示动态表单
     */
    @Override
    public void click(EventObject evt) {
        super.click(evt);
        Control source = (Control)evt.getSource();
        IFormView view = this.getView();
        IDataModel model = view.getModel();
        if (StringUtils.equals("yem_backbut", source.getKey())){
            ConfirmCallBackListener confirmCallBackListener = new ConfirmCallBackListener("saveAction", this);
            view.showConfirm("确认回填吗？", MessageBoxOptions.YesNo,confirmCallBackListener);
        }
        if (StringUtils.equals("yem_cancelbut", source.getKey())){
            ConfirmCallBackListener confirmCallBackListener = new ConfirmCallBackListener("delAction", this);
            view.showConfirm("确认取消回填吗？", MessageBoxOptions.YesNo,confirmCallBackListener);

        }
    }

    @Override
    public void confirmCallBack(MessageBoxClosedEvent messageBoxClosedEvent) {
        super.confirmCallBack(messageBoxClosedEvent);
        if("saveAction".equals(messageBoxClosedEvent.getCallBackId())){
            if (MessageBoxResult.Yes.equals(messageBoxClosedEvent.getResult())) {
                confirm();
            }
        }
        if ("delAction".equals(messageBoxClosedEvent.getCallBackId())){
            if (MessageBoxResult.Yes.equals(messageBoxClosedEvent.getResult())) {
                IFormView view = this.getView();
                view.invokeOperation("save");
                view.close();
            }
        }
    }

    public void confirm(){
        IFormView view = this.getView();
        IDataModel model = view.getModel();
        model.setValue("yem_issure",true);
        model.setValue("yem_baketime",new Date());
        view.invokeOperation("save");

        view.returnDataToParent(model.getDataEntity(true));
        view.close();
    }

    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);

    }


}
