<template>
  <div class="cpt-app-uav-list">
    <div class="uav-item-box">
      <div class="uav-item-inner" @click="$set(data, 'collapse', !data.collapse)">
        <div class="title-box">
          <span class="el-icon-caret-right" :class="{ collapse: data.collapse }"></span>
          <img class="level-icon" src="./assets/images/uav_item1_1.png" v-if="level === 1" />
          <img class="level-icon" src="./assets/images/uav_item2.svg" v-if="level === 2" />
          <img class="level-icon" src="./assets/images/uav_item3.svg" v-if="level === 3" />
          <div class="org-name" :title="data.name">{{ data.name }}</div>
          <i
            class="refresh-icon el-icon-refresh-right"
            v-if="level === 1"
            @click.stop="$emit('refresh')"
          />
        </div>
        <div class="online-info">
          （共 {{data.online + data.offline}} 架
          <span
            class="ml10"
            :class="{ online: data.online }"
          >{{ data.online }} 在线</span>
          /
          {{ data.offline }} 离线）
        </div>
      </div>
      <div class="uav-item-child-box" :class="{ collapse: data.collapse }">
        <Item v-for="child in data.childs" :key="child.id" :data="child" :level="level+1" @refresh="$emit('refresh')" />
        <div
          class="device-item-box"
          :class="{ online: device.online == 1 }"
          v-for="device in data.uavs"
          :key="`device_${device.id}`"
        >
          <div class="device-name">
            <span
              style="margin-right:10px"
              :class="device.online==1&&device.currentOperator==1? 'blue' : device.online==1&&device.currentOperator!=1 ?'yellow':'' "
              :title="device.name"
            >{{device.name}}</span>
            <span style="color:#31DB24 " class="dib" v-if=" device.online == 1 ">(在线)</span>
            <span v-else class="dib">(离线)</span>
            <div class="symbol-icon-box">
              <template v-if="device.modelName && device.modelName.includes('入云龙1')">
                <div class="uav_version status-icon cp">
                  <img src="./assets/images/I.svg" />
                </div>
              </template>
              <template v-if="device.modelName && device.modelName.includes('入云龙2')">
                <div class="uav_version status-icon cp">
                  <img src="./assets/images/I.svg" />
                  <img src="./assets/images/I.svg" />
                </div>
              </template>
              <template v-if="device.modelName && device.modelName.includes('插翅虎')">
                <div class="uav_version status-icon cp">
                  <img src="./assets/images/cq.svg" />
                </div>
              </template>
            </div>
          </div>
          <div class="device-fns">
            <div
              class="iconfont icon-luxiang_xianxing"
              :class="{ active: uav && device.id === uav.id && uav.showPlayer }"
              title="视频"
              @click="showPlayer(device)"
            ></div>

            <div
              v-if="device.online"
              class="iconfont icon-kongzhi_xianxing"
              :class="{ active: uav && device.id === uav.id && uav.showPanel }"
              title="控制面板"
              @click="showPanel(device)"
            ></div>
            <div
              class="jieg"
              :class="{ active: device.currentOperator === userInfo.id }"
              title="接管无人机  "
              v-if="device.online"
              @click="onTakeOver(device)"
            ></div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions, mapState } from "vuex";
import { Control_API } from "../../../../../../../../api";

export default {
  name: "Item",
  data() {
    return {};
  },
  props: {
    data: {
      type: Object,
      default: () => ({}),
    },
    level: {
      type: Number,
      default: -1,
    },
  },
  computed: {
    ...mapState("MMCFlightControlCenter/uavApplications", ["uav"]),
    ...mapState("MMCFlightControlCenter", ["userInfo"]),
  },
  methods: {
    ...mapActions("MMCFlightControlCenter/uavApplications", [
      "showPlayer",
      "showPanel",
    ]),
    /**
     * 接管无人机
     */
    async onTakeOver(uav) {
      if (!uav.currentOperator) {
        let res = await Control_API.setUavControlOn({
          force: false,
          id: uav.id,
          uid: this.userInfo.id,
        });
        if (res.code === 200) {
          this.$message.success(`请开始操作${uav.name}`);
          this.$emit('refresh');
        }
      } else if (
        // 判断当前接管人是不是自已, 是则提示退出接管, 不是则提示是否强制接管
        uav.currentOperator === this.userInfo.id
      ) {
        try {
          await this.$confirm(`请确认是否退出接管${uav.name}?`, "安全确认", {
            cancelButtonText: "取消",
            confirmButtonText: "确定",
            customClass: "uav_controlPane",
            showClose: false,
          });
          let res = await Control_API.setUavControlOff(uav.id);
          if (res.code === 200) {
            this.$message.success(`已退出接管${uav.name}`);
            this.$emit('refresh');
          }
        } catch (e) {
        }
      } else {
        try {
          await this.$confirm(
            `${uav.name}已被接管, 请确认是否强制接管?`,
            "安全确认",
            {
              cancelButtonText: "取消",
              confirmButtonText: "确定",
              customClass: "uav_controlPane",
              showClose: false,
            }
          );
          let res = await Control_API.setUavControlOn({
            force: false,
            id: uav.id,
            uid: this.userInfo.id,
          });
          if (res.code === 200) {
            this.$message.success(`请开始操作${uav.name}`);
            this.$emit('refresh');
          }
        } catch (e) {
        }
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.cpt-app-uav-list {
  .uav-item-box {
    font-family: Microsoft YaHei;
    font-size: 16px;
    color: #b3bbc5;
    font-weight: 400;

    .uav-item-inner {
      height: 30px;
      display: flex;
      align-items: center;
      box-sizing: border-box;
      cursor: pointer;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .title-box {
        display: flex;
        align-content: center;
        align-items: center;

        .el-icon-caret-right {
          cursor: pointer;
          transition: 0.3s;
          transform-origin: center;
          transform: rotate(90deg);
          margin-right: 4px;

          &.collapse {
            transform: rotate(0deg);
          }
        }

        .org-name {
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          font-size: 13px;
        }
      }

      .online-info {
        font-size: 12px;
        white-space: nowrap;

        .online {
          color: #31db24;
        }
      }
    }

    .uav-item-child-box {
      padding-left: 20px;

      max-height: 20000px;
      transition: 0.3s;
      overflow: hidden;
      position: relative;

      &::before {
        content: " ";
        display: block;
        height: 100%;
        width: 1px;
        background-color: #129c9c;
        position: absolute;
        left: 7px;
        top: 0;
      }

      &.collapse {
        max-height: 0;
      }

      .device-item-box {
        height: 30px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        box-sizing: border-box;
        transition: 0.2s;

        .device-name {
          display: flex;
          align-items: center;
          width: calc(100% - 150px);
          white-space: nowrap;
          text-overflow: ellipsis;
          font-size: 13px;
        }

        &.online {
          .device-name {
            color: #00ffff;
          }
        }

        .device-fns {
          display: flex;
          align-items: center;
          justify-content: space-between;

          .jieg {
            width: 20px;
            cursor: pointer;
            height: 20px;
            background: url("./assets/images/jieguan.svg") no-repeat;
            background-size: 100% 100%;
            &.active {
              background: url("./assets/images/jieguan_active.svg") no-repeat;
            }
          }
          .iconfont {
            font-size: 24px;
            margin-right: 8px;
            cursor: pointer;

            &.active {
              color: #00ffff;
            }
          }
        }
      }
    }
  }
  .goodsName_two {
    background-image: url("./assets/images/goodsName_two.svg");
    background-size: 100% 100%;
  }
  .goodsName_one {
    background-image: url("./assets/images/goodsName_one.svg");
    background-size: 100% 100%;
  }
}
.green {
  color: #31db24;
}
.red {
  color: red;
}
.item_items {
  align-items: center;
}
.level-icon {
  width: 16px;
  margin-right: 6px;
}
.refresh-icon {
  width: 16px;
  margin-left: 34px;
}
.jcsb {
  justify-content: left;
}
.status-icon {
  margin-left: 12px;
  font-size: 14px;
  color: RGBA(251, 192, 1, 1);
}
.uav_version {
  margin-top: 1px;
  width: 14px;
  height: 12px;
  background: rgba(255, 255, 255, 0.4);
  border-radius: 2px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.symbol-icon-box {
  display: flex;
  align-items: center;

  img {
    height: 8px;
  }
}
</style>