<!-- 飞控中心机库列表 -->
<template>
  <div class="list" :class="{ collapse: listCollapse }">
    <div class="list-header">
      <span class="list-header__text">{{ $t('common.鹰巢列表') }}</span>
      <div class="list-header__count">
        {{ $t('common.共') }}{{ count.sumCount }}{{ $t('common.架') }} /
        <span class="online">{{ count.onlineCount }}{{ $t('common.在线') }}</span>
        /
        {{ count.offlineCount }}{{ $t('common.离线') }}
      </div>
    </div>
    <div class="uav-search">
      <el-input
        class="uav-search__input"
        clearable
        :placeholder="$t('common.请输入无人机名称/机构名称')"
        v-model="searchContent"
        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" v-hover>{{ $t('common.搜索') }}</el-button>
    </div>
    <div class="cpt-observe-nest-list">
      <template v-if="list && list.length">
        <Item v-for="item in list" :key="item.id" :data="item" @refresh="getList" />
      </template>
    </div>
    <!-- 未进入机库或者进入机库后列表是展开的情况下，才显示收纳按钮 -->
    <div
      class="hangar-list_btn"
      @click="listCollapse = !listCollapse;"
    >
      <!-- v-if="!hangar || (hangar && !listCollapse)" -->
      <img style src="./assets/images/collapse.svg" />
    </div>
  </div>
  <!-- </Dialog> -->
</template>

<script>
import { Control_API } from "../../../../api";
import Item from "./components/item";
import { mapState } from "vuex";

export default {
  name: "List",
  props: {},
  components: { Item },
  inject: ["bus"],
  data() {
    return {
      list: /* mock */[],
      deviceType: 2, // 设备类型 2鹰巢
      timeHandle: null,
      searchContent: '',
    };
  },

  computed: {
    ...mapState("MMCFlightControlCenter", ["listCollapse", "showAirwayEdit"]),
    ...mapState("MMCFlightControlCenter/hangar", ["hangar"]),
    count() {
      let countObj = {
        onlineCount: 0,
        offlineCount: 0,
        sumCount: 0,
      };
      this.list.forEach((item) => {
        countObj.onlineCount += item?.onLineCount || 0;
        countObj.offlineCount += item?.offLineCount || 0;
      });
      countObj.sumCount = countObj.onlineCount + countObj.offlineCount;
      return countObj;
    },
    // 收起列表按钮
    listCollapse: {
      get() {
        return this.$store.state.MMCFlightControlCenter.listCollapse;
      },
      set(val) {
        this.$store.commit("MMCFlightControlCenter/setState", {
          key: "listCollapse",
          value: val,
        });
      },
    },
  },
  watch: {
    showAirwayEdit(newVal){
      if(newVal){
        this.listCollapse = true;
      }
    }
  },

  mounted() {
    this.getList();
    this.bus.$on("uas-device-getTree-message", this.initList);
    this.bus.$on("update-hangar-list", this.updateHangarList);
    // this.timeHandle = setInterval(() => {
    //   this.getList();
    // }, 10000);
  },
  beforeDestroy() {
    // clearInterval(this.timeHandle);
  },
  methods: {
    async getList() {
      let res = await Control_API.getUavTree({
        serchKey: this.searchContent,
        deviceType: this.deviceType,
      });
      this.list = res.data;
    },
    /**
     * 无人机搜索
     */
    onUavSearch() {
      this.getList();
    },
    /**
     * 更新机库列表数据
     */
    updateHangarList(data) {
      console.log('接收到hangar列表更新数据:', data);
      
      // 保留原有的isCheck和collapse状态
      const oldList = this.list;
      const newList = JSON.parse(JSON.stringify(data)).map(newItem => {
        const oldItem = oldList.find(old => old.id === newItem.id);
        if (oldItem) {
          if (oldItem.isCheck !== undefined) {
            newItem.isCheck = oldItem.isCheck;
          }
          if (oldItem.collapse !== undefined) {
            newItem.collapse = oldItem.collapse;
          }
        }
        // 如果有子设备，也需要保留子设备的isCheck和collapse状态
        if (newItem.children && oldItem && oldItem.children) {
          newItem.children = newItem.children.map(newChild => {
            const oldChild = oldItem.children.find(old => old.id === newChild.id);
            if (oldChild) {
              if (oldChild.isCheck !== undefined) {
                newChild.isCheck = oldChild.isCheck;
              }
              if (oldChild.collapse !== undefined) {
                newChild.collapse = oldChild.collapse;
              }
            }
            return newChild;
          });
        }
        return newItem;
      });
      
      this.list = newList;
    },
  },
};
</script>

<style lang="scss" scoped>
.list {
  display: flex;
  flex-direction: column;
  position: absolute;
  width: 420px;
  height: 100%;
  left: 0;
  top: 0;
  // height: 86vh;
  z-index: 99;
  transition: 0.3s;
  overflow: visible;
  background: #222222;

  &.collapse {
    transform: translateX(-100%);

    .hangar-list_btn {
      &::before {
        transform: translate(-63%, -50%) rotate(90deg) !important;
      }
    }
  }

  .hangar-list_btn {
    position: absolute;
    cursor: pointer;
    right: -35px;
    top: 23%;
    transform: translate(0, -50%);
    z-index: 99;

    &::before {
      content: "";
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-63%, -50%) rotate(-90deg);
      width: 0px;
      height: 0px;
      border-left: 7px solid transparent;
      border-right: 7px solid transparent;
      border-bottom: 9px solid #fff;
    }
  }

  .uav-search::v-deep {
    flex-shrink: 0;
    padding: 0 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .uav-search__input {
      width: 292px;
      height: 32px;
      border-radius: 4px;

      .el-input__icon {
        line-height: 32px;
        color: #a8abb2 !important;
      }

      input {
        height: 32px;
        background: #191919;
        font-family: PingFangSC, PingFang SC;
        font-weight: 400;
        font-size: 12px;
        color: #fff;
        border: 1px solid #4b4b4b;

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

    .uav-search__btn {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 80px;
      height: 32px;
      font-size: 14px;
      color: #fff;
      border-radius: 2px;
      border: 0;
      background: #3388ff;
    }
  }
}
.cpt-observe-nest-list {
  display: flex;
  flex: 1;
  flex-direction: column;
  overflow-y: auto;
  padding: 0 16px;

  /* 滚动条整体样式 */
  &::-webkit-scrollbar {
    width: 10px; /* 滚动条宽度 */
  }

  /* 滚动条滑块样式 */
  &::-webkit-scrollbar-thumb {
    background-color: #02173d; /* 滑块颜色 */
  }

  /* 滚动条滑块悬停样式 */
  &::-webkit-scrollbar-thumb:hover {
    background-color: #112d5b; /* 悬停时滑块颜色 */
  }

  &::-webkit-scrollbar-track-piece {
    /* background-color: #182F5C; */
  }

  &::-webkit-scrollbar-track {
    background-color: #182f5c;
  }
}
.list-header {
  box-sizing: border-box;
  display: flex;
  justify-content: space-between;
  align-items: center;
  // width: 100%;
  height: 54px;
  padding: 0 16px;

  .list-header__text {
    font-family: PingFangSC, PingFang SC;
    font-weight: 600;
    font-size: 16px;
    color: #ffffff;
  }

  .list-header__count {
    color: #fff;
    font-weight: 500;
    font-size: 14px;

    .online {
      font-size: 16px;
      font-weight: 900;
      color: #31db24;
    }
  }
}
</style>
