<template>
  <web-view
    id="amapView"
    ref="amapView"
    src="/static/amap/mapGather.html"
    bindmessage="receiveMessage"
    :webview-styles="{
      height: proxy.$settingStore.webViewHeight,
    }"
    @message="onMessage"
  ></web-view>
</template>

<script setup>
import { onReady, onLoad, onShow, onNavigationBarButtonTap } from "@dcloudio/uni-app";
import { ref, onMounted, inject, shallowRef, reactive, getCurrentInstance, toRefs, nextTick } from "vue";
import { useStores, commonStores } from "@/store/modules/index";

const commonStore = commonStores(); //全局公共Store

const { proxy } = getCurrentInstance();

const pages = getCurrentPages();

const dataArray = commonStore.facilitiesGatherArray;
const dataType = commonStore.facilitiesGatherType;

// #ifdef APP-PLUS
setTimeout(() => {
  var currentWebview = pages[pages.length - 1].$getAppWebview();
  var wv = currentWebview.children()[0];
  wv.evalJS(`receiveData(${JSON.stringify({ dataArray: dataArray, dataType: dataType })})`);
}, 1000);
// #endif

function onMessage(e) {
  console.log("父页面：", e.detail.data);
  commonStore.facilitiesGatherArray = JSON.parse(e.detail.data);
  uni.redirectTo({
    url: "/pages/business/fireIot/facilitiesGather/index",
  });
}

// #ifdef H5
setTimeout(() => {
  var iframe = document.getElementById("amapView");
  var message = {
    funcName: "children",
    param: JSON.stringify(dataArray),
    typeName: dataType,
  };

  iframe.contentWindow.postMessage(message, "*");
}, 1000);

window.onmessage = function (event) {
  if ("funcName" in event.data) {
    console.log("父页面：", event);
    commonStore.facilitiesGatherArray = JSON.parse(event.data.param);
    uni.redirectTo({
      url: "/pages/business/fireIot/facilitiesGather/index",
    });
  }
};
// #endif

onLoad((options) => {});

onShow(() => {
  //调用系统主题颜色
  proxy.$settingStore.systemThemeColor([1]);
});

onReady(() => {});

onMounted(() => {});
</script>
