package yem.wmc.ydn.formplugin.dynamic;

import com.alibaba.fastjson.JSONObject;
import kd.bos.entity.IFrameMessage;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.IFormView;
import kd.bos.form.IPageCache;
import kd.bos.form.control.Control;
import kd.bos.form.control.IFrame;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.sdk.plugin.Plugin;

import java.util.EventObject;

/**
 * 视图界面-地图
 *
 * @description:
 * @date: 2024/4/17
 * @auther: longh
 */
public class ViewMapEdit extends AbstractFormPlugin implements Plugin {

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addClickListeners("yem_buttonap");
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
        String key = ((Control) evt.getSource()).getKey();
        switch (key) {
            case "yem_buttonap":
                postMessage();
                break;
            default:
                break;
        }
    }

    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        IFrame iFrame = this.getControl("yem_iframeap");
        String mmsi ="";
        String begin ="";
        String end ="";
        IFormView view = this.getView();
        IFormView parentView = view.getParentView();
        IDataModel parentModel = parentView.getModel();
        String formName = parentModel.getDataEntity(true).getDataEntityType().getName();
        mmsi = (String) parentModel.getValue("yem_mmsi");
        begin = (String) parentModel.getValue("yem_poldate");
        end = (String) parentModel.getValue("yem_dtpdate");
        if("yem_ydh_seafreight".equals(formName)) {
            iFrame.setSrc("http://120.26.66.135:8084/ydn/map?" + "mmsi=" + "477836900" + "&begin=" + "2023-10-01" + "&end=" + "2023-11-30");
        }else if("yem_ydn_viewshipping".equals(formName)){
            iFrame.setSrc("http://120.26.66.135:8084/ydn/map?" + "mmsi=" + "477836900" + "&begin=" + "2024-02-01" + "&end=" + "2024-05-01");
        }else{
            iFrame.setSrc("http://120.26.66.135:8084/ydn/map-china");
        }
        parentModel.setValue("yem_textfield", view.getPageId());
        view.sendFormAction(parentView);
    }

    private void postMessage() {
        IFormView view = this.getView();
        IFormView parentView = view.getParentView();
        IDataModel parentModel = parentView.getModel();
        String mmsi = (String) parentModel.getValue("yem_mmsi"); //
        String begin = (String) parentModel.getValue("yem_poldate");
        String end = (String) parentModel.getValue("yem_dtpdate");
        IFrame iFrame = this.getControl("yem_iframeap");
        IFrameMessage msg = new IFrameMessage();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("mmsi",mmsi);
        jsonObject.put("begin",begin);
        jsonObject.put("end",end);
        msg.setContent(jsonObject);
        iFrame.postMessage(msg);
    }
}