<template>
  <web-view
    id="amapView"
    ref="amapView"
    src="/static/amap/mapFacilitiesView.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";

import { baseFacilityType, baseGgpFacilityList } from "@/api/business/fireIot/facilitiesManage.js";

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

const { proxy } = getCurrentInstance();
const pages = getCurrentPages();

const dataArray = ref();
const typeCode = ref("");

/**
 * @初始化
 */
function init() {
  handleSelectApi();
}

/**
 * @设施地图列表查询
 * @api接口查询
 */
function handleSelectApi() {
  baseFacilityType({
    pageNum: 1,
    pageSize: 20000,
  }).then((requset) => {
    if (requset.status === "SUCCESS") {
      if (requset.data.length > 0) {
        setTimeout(() => {
          var message = {
            funcName: "初始化",
            param: JSON.stringify(requset.data[0]),
          };

          handleChildren(message);
        }, 1000);
      }
    }
  });
}

/**
 * @撒点查询
 * @api接口查询
 */
function handleMarkerApi(param) {
  baseGgpFacilityList({
    facilityType: param,
  }).then((requset) => {
    if (requset.status === "SUCCESS") {
      var message = {
        funcName: "撒点",
        param: JSON.stringify(requset.data),
      };
      handleChildren(message);
    }
  });
}

/**
 * @解析父页面传回的数据
 */
function analysisData(data) {
  if ("funcName" in data) {
    if (data.funcName == "撒点") {
      var param = data.param.split(",");
      handleMarkerApi(param);
    }
  }
}

/**
 * @向子页面发送数据
 */
function handleChildren(data) {
  // #ifdef APP-PLUS
  var currentWebview = pages[pages.length - 1].$getAppWebview();
  var wv = currentWebview.children()[0];
  wv.evalJS(`receiveData(${JSON.stringify(data)})`);
  // #endif

  // #ifdef H5
  var iframe = document.getElementById("amapView");
  iframe.contentWindow.postMessage(data, "*");
  // #endif
}

/**
 * @接收子页面传过来的值
 */
function onMessage(e) {
  analysisData(e.detail.data[0]);
}
// #ifdef H5
window.onmessage = function (event) {
  analysisData(event.data);
};
// #endif

onLoad((options) => {
  if ("typeCode" in options) {
    typeCode.value = options.typeCode;
    init();
  }
});

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

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