<template>
  <MMCFlightControlCenter
    style="width: 100%; height: 100%;"
    v-if="userInfo"
    :userInfo="userInfo"
    :devMode="devMode"
    :uavTaskList="uavTaskList"
    :hangarTaskList="hangarTaskList"
    :scene="scene"
    :useSTLAirway="useSTLAirway"
    :useTimedTask="useTimedTask"
    @uavChange="dispatchEvent('uavChange', $event)"
    @uavTaskStart="dispatchEvent('uavTaskStart', $event)"
    @uavTaskAdd="dispatchEvent('uavTaskAdd', $event)"
    @hangarChange="dispatchEvent('hangarChange', $event)"
    @hangarTaskTabChange="dispatchEvent('hangarTaskTabChange', $event)"
    @hangarTaskStart="dispatchEvent('hangarTaskStart', $event)"
    @hangarTaskAdd="dispatchEvent('hangarTaskAdd', $event)"
    @hangarTaskDel="dispatchEvent('hangarTaskDel', $event)"
    @airwayPageChange="dispatchEvent('airwayPageChange', $event)"
    @airwayGet="dispatchEvent('airwayGet', $event)"
  ></MMCFlightControlCenter>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      baseUrl: "https://test.tmj.mmcuav.cn",
      devMode: false,
      scene: 0, // 场景类型 1: 无人机 2: 机库
      account: "mmctest@admin",
      password: "test@Admin001",
      userInfo: null,
      useSTLAirway: true, // 使用标准航线
      useTimedTask: false, // 使用定时任务
      uavTaskList: [
      ],
      hangarTaskList: {
        // 常态
        normal: [
          /* 
          {
            name: "任务1",
            id: 1,
            airwayId: 105,
          },
          {
            name: "任务2",
            id: 2,
            airwayId: 106,
          }, */
        ],
        // 定时
        timed: [
          /* {
            name: "任务",
            id: 1,
            time: "2024-04-28 01:00:00", //定时与周期的时间
            status: "未执行", //任务状态
            airwayId: 105,
          }, */
        ],
        // 周期
        period: [
          /* {
                name: '任务',
                id: 1,
                time: '', //定时与周期的时间
                status: '', 任务状态
                airway: {
                  name: '航线名称',
                  id: 1
                }
              } */
        ],
      },
      airwayList: [
      ],
      hangarTaskAddCB: null, //机库创建任务回调
      uavTaskAddCB: null, //机库创建任务回调
      airwayPageChangeCB: null, //航线翻页时回调
      airwayGetCB: null, //获取航线数据回调
    };
  },
  computed: {},
  watch: {
    devMode: {
      immediate: true,
      handler() {
        if (this.devMode) {
          this.account = "mmctest@admin";
          this.password = "test@Admin001";
          this.baseUrl = "https://test.tmj.mmcuav.cn";
        } else {
          this.account = "mmcadmin@kbt001";
          this.password = "TMJMMC@kbta0227&adm";
          this.baseUrl = "https://tmj.mmcuav.cn";
        }
      },
    },
  },
  created() {
    this.postTop({
      type: "ready",
      data: true,
    });
    this.eventHandler();
  },
  async mounted() {},
  methods: {
    /**
     * 给父窗口发送消息
     */
    postTop(data) {
      window.top.postMessage(
        {
          module: "MMCFlightControlCenter",
          ...data,
        },
        "*"
      );
    },
    /**
     * 事件处理
     */
    eventHandler() {
      window.addEventListener(
        "message",
        (event) => {
          if (event.data.module === "MMCFlightControlCenter") {
            console.log("iframe收到数据", event.data);
            this.paramHandler(event.data);
            this.callbackHandler(event.data);
            this.logHandler(event.data);
          }
        },
        false
      );
    },
    /**
     * 参数处理
     */
    paramHandler({ type, param, data }) {
      if (type === "param") {
        this.$set(this, param, data);
      }
    },
    /**
     * 回调处理
     */
    callbackHandler({ type, event, data }) {
      if (type === "callback") {
        let cb = this[event + "CB"];
        if (cb) {
          cb(data);
          this[event + "CB"] = null;
        }
      }
    },
    /**
     * 日志处理
     */
    logHandler({ type, keys, keyType }) {
      if (type === "log") {
        let arr = keys.split(".");
        let value = window;
        arr.forEach((key, i) => {
          value = value[key];
          if (i === arr.length - 1) {
            switch (keyType) {
              case "value":
                this.postTop({
                  event: "log",
                  data: {
                    keys,
                    value,
                  },
                });
                break;
              case "method":
                value();
                break;
            }
          }
        });
      }
    },
    /**
     * 分发事件
     * @param {string} event 事件名
     * @param {object} data 事件数据
     */
    dispatchEvent(event, data) {
      // 回调函数不能通过postMessage传递
      switch (event) {
        case "hangarTaskAdd":
          this.hangarTaskAddCB = data.callback;
          delete data.callback;
          break;

        case "uavTaskAdd":
          this.uavTaskAddCB = data.callback;
          delete data.callback;
          break;

        case "airwayPageChange":
          this.airwayPageChangeCB = data.callback;
          delete data.callback;
          break;

        case "airwayGet":
          this.airwayGetCB = data.callback;
          delete data.callback;
          break;
      }
      this.postTop({
        type: "event",
        event: event,
        data: data,
      });
    },
  },
};
</script>
<style lang="scss">
html,
body {
  height: 100%;
}
</style>