<template>
  <MMCFlightControlCenter
    style="width: 100%; height: 100%;"
    v-if="userInfo"
    :userInfo="userInfo"
    :devMode="devMode"
    :uavTaskList="uavTaskList"
    :hangarTaskList="hangarTaskList"
    :scene="scene"
    :useSTLAirway="useSTLAirway"
    @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, // 使用标准航线
      uavTaskList: [
        {
          name: "任务1",
          id: 1,
          children: [
            {
              name: "任务2",
              id: 2,
              // children: [],
              airway: {
                name: "航线名称2",
                id: 2,
              },
            },
          ],
          airway: {
            name: "航线名称1",
            id: 1,
            content:
              '{"filename":"肇庆航线20240318","line":{"baseSpeed":3},"content":[{"uuid":"1nmI-Fo18IagbcVJsia7Q","latitude":23.178153411812204,"longitude":112.57807281336807,"alt":100,"yawAngle":0,"pitchAngle":0,"speed":3,"actions":[]},{"uuid":"9pTbBPlF8iIwbUNqusyHK","latitude":23.17783116525969,"longitude":112.57797543441967,"alt":100,"yawAngle":0,"pitchAngle":0,"speed":3,"actions":[]},{"uuid":"s91IhN22wuaeyG-UQs0XR","latitude":23.17786413506686,"longitude":112.57824336604547,"alt":100,"yawAngle":0,"pitchAngle":0,"speed":3,"actions":[]},{"uuid":"xS_JIl3wxQrhMPdpcjcSn","latitude":23.17820934975604,"longitude":112.5781357731637,"alt":100,"yawAngle":0,"pitchAngle":0,"speed":3,"actions":[]}],"baseSpeed":3,"gimbalYaw":0,"gimbalPitch":0,"alt":100}',
          },
        },
        {
          name: "任务2",
          id: 2,
          children: [],
          airway: {
            name: "航线名称2",
            id: 2,
            content:
              '{"filename":"肇庆航线20240318","line":{"baseSpeed":3},"content":[{"uuid":"1nmI-Fo18IagbcVJsia7Q","latitude":23.178153411812204,"longitude":112.57807281336807,"alt":100,"yawAngle":0,"pitchAngle":0,"speed":3,"actions":[]},{"uuid":"9pTbBPlF8iIwbUNqusyHK","latitude":23.17783116525969,"longitude":112.57797543441967,"alt":100,"yawAngle":0,"pitchAngle":0,"speed":3,"actions":[]},{"uuid":"s91IhN22wuaeyG-UQs0XR","latitude":23.17786413506686,"longitude":112.57824336604547,"alt":100,"yawAngle":0,"pitchAngle":0,"speed":3,"actions":[]},{"uuid":"xS_JIl3wxQrhMPdpcjcSn","latitude":23.17820934975604,"longitude":112.5781357731637,"alt":100,"yawAngle":0,"pitchAngle":0,"speed":3,"actions":[]}],"baseSpeed":3,"gimbalYaw":0,"gimbalPitch":0,"alt":100}',
          },
        },
      ],
      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: [
        {
          name: "航线名称1",
          id: 1,
          organizationName: "所属单位1",
          status: 1, //空域状态 1: 可用 2: 待申请 3: 待审批 4: 通过 5: 驳回
          isSafe: 1, //空域状态 1: 安全 2: 待确定
          labelName: "航线标签1",
          content:
            '{"filename":"肇庆航线20240318","line":{"baseSpeed":3},"content":[{"uuid":"1nmI-Fo18IagbcVJsia7Q","latitude":23.178153411812204,"longitude":112.57807281336807,"alt":100,"yawAngle":0,"pitchAngle":0,"speed":3,"actions":[]},{"uuid":"9pTbBPlF8iIwbUNqusyHK","latitude":23.17783116525969,"longitude":112.57797543441967,"alt":100,"yawAngle":0,"pitchAngle":0,"speed":3,"actions":[]},{"uuid":"s91IhN22wuaeyG-UQs0XR","latitude":23.17786413506686,"longitude":112.57824336604547,"alt":100,"yawAngle":0,"pitchAngle":0,"speed":3,"actions":[]},{"uuid":"xS_JIl3wxQrhMPdpcjcSn","latitude":23.17820934975604,"longitude":112.5781357731637,"alt":100,"yawAngle":0,"pitchAngle":0,"speed":3,"actions":[]}],"baseSpeed":3,"gimbalYaw":0,"gimbalPitch":0,"alt":100}',
        },
        {
          name: "航线名称2",
          id: 2,
          organizationName: "所属单位2",
          status: 2, //空域状态 1: 可用 2: 待申请 3: 待审批 4: 通过 5: 驳回
          isSafe: 2, //空域状态 1: 安全 2: 待确定
          labelName: "航线标签2",
          content:
            '{"filename":"肇庆航线20240318","line":{"baseSpeed":3},"content":[{"uuid":"1nmI-Fo18IagbcVJsia7Q","latitude":23.178153411812204,"longitude":112.57807281336807,"alt":100,"yawAngle":0,"pitchAngle":0,"speed":3,"actions":[]},{"uuid":"9pTbBPlF8iIwbUNqusyHK","latitude":23.17783116525969,"longitude":112.57797543441967,"alt":100,"yawAngle":0,"pitchAngle":0,"speed":3,"actions":[]},{"uuid":"s91IhN22wuaeyG-UQs0XR","latitude":23.17786413506686,"longitude":112.57824336604547,"alt":100,"yawAngle":0,"pitchAngle":0,"speed":3,"actions":[]},{"uuid":"xS_JIl3wxQrhMPdpcjcSn","latitude":23.17820934975604,"longitude":112.5781357731637,"alt":100,"yawAngle":0,"pitchAngle":0,"speed":3,"actions":[]}],"baseSpeed":3,"gimbalYaw":0,"gimbalPitch":0,"alt":100}',
        },
      ],
      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>