<template>
  <div class="page-observe-fckernel-uav">
    <div class="warn w440" v-if="warnShow">
      <div class="content" @click="loggerFlag = true">{{ warnMessage }}</div>
      <div class="close" @click="warnShow = false">关闭</div>
    </div>
    <!-- 日志信息 -->
    <Logger
      @exit="loggerFlag = false"
      v-if="loggerFlag && !isAirwayEdit"
      :list="uav.msg_list"
      @clear-msg="uav.msg_list = []"
    ></Logger>
    <div class="nsetLeftBox" :class="{ collapse: collapseFlag }">
      <img
        @click="collapseFlagfn"
        v-if="collapseFlag"
        style="transform: rotate(180deg)"
        src="~./assets/hs.png"
        class="icon-collapse nsetLeftBox_btn"
      />
      <img
        @click="collapseFlagfn"
        v-else
        src="~./assets/collapse.png"
        class="icon-collapse nsetLeftBox_btn"
      />
      <div class="uav-list-header">
        <img class="uav-list-header__icon" src="~./assets/uav_list_header.png" />
        <span class="uav-list-header__text">无人机列表</span>
      </div>
      <div class="uav-search">
        <el-input
          class="uav-search__input"
          clearable
          placeholder="请输入无人机名称/机构名称"
          v-model="uavSearchContent"
          v-on:keyup.enter.native="onUavSearch"
        >
          <i
            slot="suffix"
            class="el-input__icon el-icon-search"
            style="color: rgba(123, 181, 213, 1);"
          ></i>
        </el-input>
        <el-button class="uav-search__btn" @click="onUavSearch">搜索</el-button>
      </div>
      <UavList
        :class="uav.TakeOverFlag ? 'top0' : ''"
        @exit="flag = false"
        v-if="flag"
        @videoChange="videoChange"
        :containerStyle="{
          width: '400PX',
          height: '275px',
        }"
        :list="uav.list"
        @fn="uav_fn"
        @refresh="onUavSearch"
      />
      <!-- <div class="w50 ht100 pa left0 top0" style="background: rgba(0, 0, 0, 0.42)">
        <div class="iconfont icon-yingchaoliebiao ml15 cp f20 cf mt20 acitve"></div>
      </div>-->
      <div></div>
    </div>

    <component
      v-if="uav.control.mount"
      :is="uav.control.mount.component"
      :PoliceKeyCode="uav.control.device && uav.control.device.deviceHardId"
      @directive="uav_mount_directive"
      @close="uav_change_mount"
      :deviceid="uav.control.device"
    />
    <Control
      :device="uav.control.device"
      :wsShow="wsShow"
      :data="uav.control.channelData"
      v-if="ControlFlag || (uav.control.data && !isAirwayEdit)"
      :uav-battery="uav.control.battery"
      :uav-data="uav.control.data"
      @fn="(data, type) => uav_mqtt_fn(data, type)"
      @fun="(data) => uav_send_ws(data)"
    ></Control>
    <AirwayEdit
      :flag="false"
      @add="airway_add"
      v-if="isAirwayEdit"
      :isShow="false"
      @quit="AirwayQuit"
    ></AirwayEdit>
    <!-- -->
    <div class="videoBox">
      <!-- uav_move_directive -->
      <FloatPlayer
        @directiveFn="uav_move_directive"
        @imgUrl="getimgUrl"
        :device="uav.control.device"
        v-for="(video, index) in uav.videos"
        :uav-data="uav.control.data"
        @videoItemFn="videoItemFn(index)"
        @close="uav_hide_video"
        @fn="uav_mqtt_fn"
        :key="video.id"
        :data="video"
        :class="`_${index}`"
        :videoItem="videoItem"
        :index="index"
      /> -->
    </div>
    <TaskList
      :device="uav.control.device"
      :taskstyle="collapseFlag"
      @Lsdom="Lsdom"
      @taskType="taskTypeFn"
      @iconShow="iconShow = true"
      ref="TaskListRef"
      @fn="(data, type) => uav_mqtt_fn(data, type)"
      @fun="(data) => uav_send_ws(data)"
      @craeteRoute="CraeteRoute"
      v-if="controlMenuFlag"
      @createTaskClick="createTaskClick"
    ></TaskList>
    <ControlMenu
      ref="ControlMenu"
      :wsShow="wsShow"
      :taskstyle="collapseFlag"
      @imgUrl="getimgUrl"
      @clearId="clearIdFn"
      :wrjType="uav.wrjType"
      :device="uav.control.device"
      @Lsdom="Lsdom"
      :mount="uav.control.mounts"
      @uav-location="uav_show_location(uav.control.device)"
      @airway-display="uav_show_airway(uav.control.device)"
      :taskType="taskType"
      :uav-mounts="uav.mounts"
      v-if="uav.control.device"
      @yxrz="LoggerFn"
      :iconShow="iconShow"
      @fn="(data, type) => uav_mqtt_fn(data, type)"
      @fun="(data) => uav_send_ws(data)"
      @changeTask="onChangeTask"
    ></ControlMenu>
    <!-- 请求控制权限 -->
    <Notice
      @close="uav.NoticeFlag = false"
      @handle="uav_jursdiction"
      class
      @qzjg="qzjg"
      @exit="uav.NoticeFlag = false"
      v-if="uav.NoticeFlag"
      :leftText="uav.NoticeData.leftText"
      :rightText="uav.NoticeData.rightText"
      :content="uav.NoticeData.content"
      :title="uav.NoticeData.title"
      :flag="true"
    ></Notice>
    <!-- 接管记录 -->
    <TakeOver @exit="uav.TakeOverFlag = false" v-if="uav.TakeOverFlag"></TakeOver>
    <VideoTomap v-if="VideoTomapFlag && videoFlag && uav.videos.length > 0"></VideoTomap>
    <el-dialog width="40%" :visible.sync="progressT">
      <el-progress :text-inside="true" :stroke-width="26" :percentage="jindu"></el-progress>
    </el-dialog>
    <div class="imgshowurl" v-if="imgshow">
      <div @click="imgshow = false" class="close">关闭</div>
      <img :src="imgshowurl" />
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import UavList from "./components/list";
// 视频播放器
import FloatPlayer from "./components/float-playerfk";
import AirwayEdit from "./components/airway-edit";

import data from "./data";
import methods from "./methods";
import Header from "./components/header/index";
// 下面的油门图标
import Control from "./components/control/index.vue";
// 任务库
import TaskList from "./components/taskList";
// 下面一键任务的小图标
import ControlMenu from "./components/control/controlMenu";
import Notice from "./components/control/components/notice";
import Logger from "./components/control/logger/index";
import TakeOver from "./components/control/components/takeOver";
import VideoTomap from "./components/control/videoTomap";
// 引用bus
import Bus from "mmc/utils/bus.js";
// import API from "@/api";
export default {
  components: {
    UavList,
    FloatPlayer,
    Header,
    Control,
    TaskList,
    ControlMenu,
    AirwayEdit,
    Notice,
    TakeOver,
    Logger,
    VideoTomap,
  },
  data() {
    return {
      ...data,
      level: null,//M300信号等级
      warnShow: false, //异常消息弹窗
      warnMessage: "", //异常消息
      imgshow: false, //拍照图片展示
      imgshowurl: null,
      videoFlag: true,
      // 头部状态是否为展示状态
      flag: true,
      controlMenuFlag: false,
      loggerFlag: false,
      // 运行监测
      powerFlag: true,
      // 创建航线窗口
      isAirwayEdit: false,
      // 创建任务
      CreateTaskFlag: false,
      // 左下角切换视频
      VideoTomapFlag: false,
      // 下方展示数据面板
      ControlFlag: false,
      iconShow: false,
      taskType: "",
      videoItem: 0,
      collapseFlag: false,
      linevisible: false,
      takeOff: false,
      uavId: null, //无人机id
      progressT: false, //进度条弹框
      jindu: 0, //进度条进度
      uavSearchContent: "", //无人机搜索内容
      wsShow: false, //判断是走飞控中心链路还是地面站链路,
    };
  },
  inject: ["g_cesium_layer"],
  provide() {
    return {
      uav_This: this,
      findList: (search) => {
        if (search) {
          this.uav_list(search);
        } else {
          this.uav_list(search);
          // this.uav_mountList(data.name);
        }
      },
      uav_mqtt_fn: this.uav_mqtt_fn,
      uav: this.uav,
    };
  },

  watch: {
    data: function (vla, old) {},
    "uav.control.device": function (val) {
      this.uav.msg_list = [];
    },
  },
  computed: {
    ...mapGetters(["user_info"]),
    uav_mounts() {
      let { mounts } = this.uav.control;
      return mounts
        .map((item) => {
          let find_item = this.mount.list.find(
            (m) => m.name === item.gimbalName
          );
          return find_item || undefined;
        })
        .filter((item) => item);
    },
  },
  created() {
    Bus.$on("remove_model", () => {
      // 删除飞控 无人机 模型和航线
      // 搜索关键字：删除飞控模型
      this.uav_remove_model(this.uav.control.device);
    });
    // 等待航线上传成功在执行
    // 判断是否以一键任务
    Bus.$on("off_take", () => {
      this.takeOff = true;
    });
  },
  mounted() {
    console.log(this.mount, "mount");
    this.uav_list();
    this.mqtt_init();
    this.uav_init_ws();
  },
  beforeDestroy() {
    this.uav.control.device = null;
    this.uav.control.device = null;
    this.uav.control.data = null;
    this.uav.control.battery = null;
    this.uav.control.mount = null;
    this.uav.control.mounts = [];
    this.uav.control.mounts = [];
    this.$store.commit("mapmanage/SET_MAP_IS_STATUSl", {
      flag: false,
      type: "wrj",
    });
    this.uav.videos = [];
    if (this.mqtt.client) {
      this.mqtt.client.end();
      this.mqtt.client = null;
    }
  },
  methods: {
    ...methods,
    auto() {
      //定点-->航线
      //摇杆手动-->自动
      this.$refs.ControlMenu.changeAuto();
    },
    getimgUrl(val) {
      this.imgshow = true;
      this.imgshowurl = val;
    },
    async qzjg() {
      /* let res = await API.DEVICE.forceTakeOver({
        deviceHardId: this.uav.NoticeData.deviceHardId,
      });
      if (res.code == 200) {
        this.$message.success(res.msg);
        this.uav.NoticeFlag = false;
      } */
    },
    fly_take_off() {
      // 等待航线上传成功在执行
      this.$refs.TaskListRef.upload_complete();
      // Bus.$emit("take_off");
    },
    collapseFlagfn() {
      this.collapseFlag = !this.collapseFlag;
      if (this.collapseFlag == true) {
        // 修改飞控 无人机 左边'任务库'的位置
        Bus.$emit("ydh", false);
      } else {
        Bus.$emit("ydh", true);
      }
    },
    Lsdom(item) {
      this.$emit("Lsdom", item);
    },
    clearIdFn() {
      if (this.$refs.TaskListRef) {
        this.$refs.TaskListRef.close();
        try {
          let deviceHardId = this.uav.control.device.deviceHardId;
          this.uav_hide_airway(this.uav.control.device);
          this.uav.online[deviceHardId].positions = [
            this.uav.online[deviceHardId].position,
          ];
        } catch (error) {}
      }
    },
    videoItemFn(index) {
      this.videoItem = index;
      this.$store.commit("mapmanage/SET_MAP_IS_VIDEOITEM", index);
    },
    LoggerFn() {
      this.loggerFlag = !this.loggerFlag;
      // this.collapseFlag = false;
    },
    videoChange() {
      this.videoFlag = !this.videoFlag;
    },
    showTakeOver() {
      this.uav.TakeOverFlag = !this.uav.TakeOverFlag;
      if (this.uav.TakeOverFlag) {
        this.collapseFlag = true;
      }
    },
    // 关闭航线创建列表
    AirwayQuit() {
      this.isAirwayEdit = false;
      // 运行监测关闭
      this.powerFlag = true;
      this.Videoflag = true;
      this.ControlFlag = true;
      this.collapseFlag = false;
    },
    taskTypeFn(item) {
      this.taskType = item;
    },
    // 创建航线
    CraeteRoute() {
      this.isAirwayEdit = true;
      // // 运行监测关闭
      this.powerFlag = false;
      this.ControlFlag = false;
      this.collapseFlag = true;
    },
    createTaskClick() {
      this.CreateTaskFlag = !this.CreateTaskFlag;
      this.controlMenuFlag = false;
    },
    async checkUseOperateFn(device) {
      // 查看是否有控制权限
      /* let res = await API.FCKERNEL.checkUseOperate({ deviceHardId: device });
      if (res.code == 201) {
        this.$message.warning(res.msg);
        return false;
      } else {
        return true;
      } */
    },
    /**
     * 无人机搜索
     */
    onUavSearch() {
      this.uav_list({
        search: this.uavSearchContent,
      });
    },
    //点击任务按钮
    onChangeTask(open) {
      if (open === undefined) {
        this.controlMenuFlag = !this.controlMenuFlag;
      } else {
        this.controlMenuFlag = open;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.collapse {
  transform: translateX(-100%);
  transition: 0.3s;
}
.page-observe-fckernel-uav {
  height: 100%;

  .videoBox {
    /* width: 382px; */
    // border: 1px solid #D8D8D8;
    position: absolute;
    top: 60px;
    padding-bottom: 10px;
  }
}
.nsetLeftBox {
  position: absolute;
  width: 460px;
  left: 0;
  top: 60px;
  // height: 86vh;
  transition: 0.3s;
  background: rgba(9, 32, 87, 0.7);
  border-radius: 10px 10px 0px 0px;
  // border: 1px solid #70daf9;
  .nsetLeftBox_btn {
    position: absolute;
    cursor: pointer;
    right: -22px;
    top: 50%;
    margin-top: -90px;
  }
}
.acitve {
  color: #70daf9;
}

.uav-search {
  padding: 0 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  // margin-bottom: 7px;
  margin-top: 14px;

  .uav-search__input ::v-deep {
    width: 317px;
    height: 32px;
    box-shadow: 0px 2px 4px 0px rgba(28, 94, 124, 0.5),
      inset 0px 0px 3px 0px #00b6ff;
    border-radius: 4px;

    .el-input__icon {
      line-height: 32px;
    }

    input {
      height: 32px;
      background: #02173d;
      color: #43deff;
      border: 1px solid #315ec7;

      &::placeholder {
        font-size: 12px;
        color: #7bb5d5;
      }
    }
  }

  .uav-search__btn {
    padding: 7px 25px 6px 26px;
    // background: rgba(38, 71, 238, 0.71);
    background: url("~../../../assets/img/twobg.png");
    border-radius: 4px;
    border: 1px solid;
    font-size: 14px;
    color: #fff;
  }
}

.uav-list-header {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  width: 460px;
  height: 33px;
  background: linear-gradient(
    180deg,
    #9198ff 0%,
    rgba(45, 81, 153, 0.45) 40%,
    #05091a 100%
  );
  box-shadow: inset 0px 0px 10px 2px #3f9dff;
  border-radius: 10px 10px 0px 0px;
  border: 1px solid #427dff;

  .uav-list-header__text {
    font-size: 20px;
    font-family: YouSheBiaoTiHei;
    color: #14faff;
    line-height: 26px;
    text-shadow: 0px 1px 1px rgba(2, 32, 56, 0.2);
    background: linear-gradient(
      135deg,
      #f7b67d 38%,
      #f9eacb 58%,
      #f5d2a6 79%,
      #f59743 100%
    );
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
  }

  .uav-list-header__icon {
    width: 26px;
    margin-left: 9px;
  }
}
.imgshowurl {
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0px;
  top: 0px;
  background: rgba($color: #000000, $alpha: 0.6);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: 100;
  img {
    width: 1000px;
  }
  .close {
    color: #fff;
    margin-left: 1000px;
    margin-bottom: 16px;
    // position: absolute;
    // right: 10px;
    // top: 10px;
  }
}
.warn {
  position: absolute;
  left: calc(50% - 220px);
  top: 10%;
  padding: 10px 14px;
  // height: 36px;
  background: rgba(249, 42, 42, 0.6);
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .content {
    word-break: break-all;
    width: 400px;
    color: #fff;
  }
  .close {
    cursor: pointer;
    color: #fff;
  }
}

.uav-list-header {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  width: 460px;
  height: 33px;
  background: linear-gradient(
    180deg,
    #9198ff 0%,
    rgba(45, 81, 153, 0.45) 40%,
    #05091a 100%
  );
  box-shadow: inset 0px 0px 10px 2px #3f9dff;
  border-radius: 10px 10px 0px 0px;
  border: 1px solid #427dff;

  .uav-list-header__text {
    font-size: 20px;
    font-family: YouSheBiaoTiHei;
    color: #14faff;
    line-height: 26px;
    text-shadow: 0px 1px 1px rgba(2, 32, 56, 0.2);
    background: linear-gradient(
      135deg,
      #f7b67d 38%,
      #f9eacb 58%,
      #f5d2a6 79%,
      #f59743 100%
    );
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
  }

  .uav-list-header__icon {
    width: 26px;
    margin-left: 9px;
  }
}
  .imgshowurl{
    width: 100%;
    height: 100%;
    position: fixed;
    left: 0px;
    top: 0px;
    background: rgba($color: #000000, $alpha: 0.6);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 100;
    img{
      width: 1000px;
    }
    .close{
      color: #fff;
      margin-left: 1000px;
      margin-bottom: 16px;
      // position: absolute;
      // right: 10px;
      // top: 10px;
    }
  }
  .warn{
    position: absolute;
    left: calc(50% - 220px);
    top: 10%;
    padding: 10px 14px;
    // height: 36px;
    background: rgba(249,42,42,0.6);
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .content{
      word-break: break-all;
      width: 400px;
      color: #fff;
    }
    .close{
      cursor: pointer;
      color: #fff;
    }
  }
</style>