<template>
  <div>
    <el-form>
      <el-form-item label="环境">
        <el-switch v-model="devMode" active-text="测试环境" inactive-text="正式环境"></el-switch>
      </el-form-item>
      <el-form-item label="场景">
        <el-switch v-model="scene" active-text="无人机" inactive-text="机库"></el-switch>
      </el-form-item>

      <el-form-item label="账号">
        <el-input v-model="account"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="password"></el-input>
      </el-form-item>
      <el-form-item label="token">{{ userInfo }}</el-form-item>
      <el-button @click="login">登录</el-button>
    </el-form>
    <el-tooltip class="item" effect="dark" content="Top Left 提示文字" placement="top-start">
      <el-button>上左</el-button>
    </el-tooltip>
    <div style="margin-top: 1080px;">
      <MMCFlightControlCenter
        style="width: 1920px; height: 1080px; position: absolute; left: 0; top: 600px;"
        v-if="userInfo"
        :userInfo="userInfo"
        :devMode="devMode"
        :uavTaskList="uavTaskList"
        :hangarTaskList="hangarTaskList"
        :airwayList="airwayList"
        :scene="scene ? 0 : 1"
        @uavStartTask="onUAVStartTask"
        @hangarChange="onHangarChange"
        @hangarChangeTaskTab="onHangarChangeTaskTab"
        @hangarStartTask="hangarStartTask"
        @hangarAddTask="onHangarAddTask"
        @hangarDelTask="onHangarDelTask"
      ></MMCFlightControlCenter>
    </div>
  </div>
</template>

<script>
import { Base64 } from "js-base64";

export default {
  name: "fkzx1",
  data() {
    return {
      baseUrl: "https://test.tmj.mmcuav.cn",
      devMode: false,
      scene: false, // 场景类型 true: 无人机 false: 机库
      account: "mmctest@admin",
      password: "test@Admin001",
      userInfo: null,
      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}',
        },
      ],
    };
  },
  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";
        }
      },
    },
  },
  async mounted() {},
  methods: {
    onUAVStartTask(uav, selectedTask, selectedAirway) {
      console.log("一键任务事件", uav, selectedTask, selectedAirway);
      // alert("一键任务事件");
    },
    /**
     * 更换机库事件
     */
    onHangarChange(hangar) {
      console.log("onHangarChange");
      if (hangar) {
        this.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: "2024-04-28 01:00:00 2024-05-28 01:00:00", //定时与周期的时间
              status: "未执行", //任务状态
              airwayId: 105,
            },
          ],
        };
      }
    },
    /**
     * 机库任务添加事件
     * @param {Object} param
     * @param {number} param.type 1: 日常任务 2: 定时任务 3:周期任务
     * @param {Object} param.airway 航线数据
     * @param {Function} params.callback 回调，参数为更新后新增的任务id, 仅日常任务有用
     * @param {Array} param.taskList 定时与周期任务创建的任务数组
     */
    onHangarAddTask({ type, airway, callback, taskList }) {
      console.log("onHangarAddTask", type, airway, taskList);
      switch (type) {
        case 1: {
          let id = Date.now();
          this.hangarTaskList.normal.push({
            name: "任务" + id,
            id: id,
            airwayId: airway.id,
          });
          callback(id);
          break;
        }

        case 2: {
          let id = Date.now();
          let _taskList = taskList.map((item, i) => {
            return {
              ...item,
              id: id + String(i),
              status: "未执行",
              name: "任务" + (this.hangarTaskList.timed.length + 1),
            };
          });
          this.hangarTaskList.timed =
            this.hangarTaskList.timed.concat(_taskList);
          break;
        }

        case 3: {
          let id = Date.now();
          let _taskList = taskList.map((item, i) => {
            return {
              ...item,
              id: id + String(i),
              status: "未执行",
              name: "任务" + (this.hangarTaskList.period.length + 1),
              time: `${item.date[0]} ${item.time[0]} ${item.date[1]} ${item.time[1]}`
            };
          });
          this.hangarTaskList.period =
            this.hangarTaskList.period.concat(_taskList);
          break;
        }
      }
    },
    /**
     * 机库任务tab更改事件
     * @param {object} param
     * @param {number} param.type  1: 日常任务 2: 定时任务 3:周期任务
     */
    onHangarChangeTaskTab({ type }) {
      console.log("onHangarChangeTaskTab", type);
    },
    /**
     * 机库开始任务
     * @param {object} param
     * @parma {number} param.type  1: 日常任务 2: 定时任务 3:周期任务
     * @param {object} param.task 任务对象
     * @param {object} param.airway 航线信息
     */
    hangarStartTask({ type, hangar, task, airway }) {
      console.log("hangarStartTask", type, hangar, task, airway);
    },
    /**
     * 删除机库任务
     * @param {object} param
     * @parma {number} param.type  1: 日常任务 2: 定时任务 3:周期任务
     * @param {object} param.task 任务对象
     */
    onHangarDelTask({ type, task }) {
      console.log("onHangarDelTask", type, task);
      switch (type) {
        case 2:
          this.hangarTaskList.timed = this.hangarTaskList.timed.filter(
            (item) => item.id !== task.id
          );
          break;

        case 3:
          this.hangarTaskList.period = this.hangarTaskList.period.filter(
            (item) => item.id !== task.id
          );
          break;
      }
    },
    async login() {
      let formData = new FormData();
      formData.append("userAccount", this.account);
      formData.append("password", Base64.encode(this.password));
      let res1 = await fetch(this.baseUrl + "/crm/login", {
        method: "post",
        headers: {
          // "Content-Type": "multipart/form-data",
        },
        body: formData,
      });
      let data1 = await res1.json();

      if (data1.code == 200) {
        await fetch(this.baseUrl + "/crm/user/platformPermission", {
          method: "get",
          headers: {
            Token: data1.data.token,
          },
        });
        this.userInfo = data1.data;
      }
    },
  },
};
</script>