<template>
  <div class="content">
    <div class="content-info">
      <div class="content-info-left">
        <div class="content-info-left-style">
          <div class="content-info-left-style-title">
            {{ device.callsign }}
          </div>
        </div>
        <div class="content-info-left-style">
          <div class="content-info-left-style-info">
            <img class="content-info-left-style-info-icon" :src="aircraftSrc">
            <div class="content-info-left-style-info-text"
              :style="deviceInfo[device.sn] && deviceInfo[device.sn].mode_code !== EModeCode.Disconnected ? 'color:#00EE8B' : 'color:red'">
              {{ getTextByModeCode(deviceInfo[device.sn] ? deviceInfo[device.sn].mode_code : EModeCode.Disconnected) }}
            </div>
          </div>
          <div :class="['content-info-left-style-other', deviceReason ? 'warn' : '']">
            <div v-if="deviceReason">
              <span class="word-loop">
                {{ deviceReason }}
              </span>
            </div>
            <div v-else>
              当前正常
            </div>
          </div>
        </div>
        <div class="content-info-left-style">
          <div class="content-info-left-style-info">
            <img class="content-info-left-style-info-icon" :src="controllerSrc">
            <div class="content-info-left-style-info-text" v-if="capacity">
              {{ device.gateway.callsign }}
            </div>
            <div class="content-info-left-style-info-text" style="color: red;" v-else>
              未连接
            </div>
          </div>
          <div class="content-info-left-style-other">
            <div class="capacity" v-if="capacity">
              <img :src="batteryOneSrc" v-if="capacity >= 75">
              <img :src="batteryTwoSrc" v-else-if="capacity >= 50 && capacity < 75">
              <img :src="batteryThreeSrc" v-else-if="capacity >= 25 && capacity < 50">
              <img :src="batteryFourSrc" v-else-if="capacity < 25">
              <div>
                {{ capacity }}%
              </div>
            </div>
            <div v-else>
              当前正常
            </div>
          </div>
        </div>
      </div>
      <div class="content-info-right">
        <a-button style="padding: 0;" type="text"
          :disabled="!(deviceInfo[device.sn] && deviceInfo[device.sn].mode_code !== EModeCode.Disconnected)"
          @click.stop="onClickLookInfo">
          <img :src="infoSelectedSrc" v-if="osdVisible.visible && osdVisible.sn === device.sn">
          <img :src="infoSrc" v-else>
        </a-button>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { computed } from 'vue';
import controllerSrc from './icons/controller.svg';
import aircraftSrc from './icons/aircraft.svg';
import batteryOneSrc from './icons/batteryOne.svg';
import batteryTwoSrc from './icons/batteryTwo.svg';
import batteryThreeSrc from './icons/batteryThree.svg';
import batteryFourSrc from './icons/batteryFour.svg';
import infoSrc from './icons/info.svg';
import infoSelectedSrc from './icons/info_selected.svg';
import { useMyStore } from '/@/store';
import { getTextByModeCode } from '/@/utils/index'
import { OnlineDevice, EModeCode } from '/@/types/device';

interface Props {
  device: OnlineDevice,
  onClickLookInfo: () => void,
};

const props = withDefaults(defineProps<Props>(), {

});

const store = useMyStore()

const deviceInfo = computed(() => store.state.deviceState.deviceInfo)
const osdVisible = computed(() => store.state.osdVisible);

// 设备原因
const deviceReason = computed(() => {
  const device = store.state.deviceReason[props.device.sn];
  if (device) {
    return device.modeCodeReason;
  } else {
    return '';
  }
})

// 电池容量
const capacity = computed(() => {
  const device: any = deviceInfo.value[props.device.sn];
  if (device) {
    return device.battery.capacity_percent;
  } else {
    return 0;
  }
});
</script>

<style lang="scss" scoped>
.content {
  width: 208px;
  border: 1px solid #3d3d3d;
  border-radius: 4px;
  overflow: hidden;
  color: #FFFFFF;
  cursor: pointer;

  &-info {
    display: flex;

    &-left {
      width: calc(100% - 20px);
      padding: 5px;
      font-size: 12px;

      &-style {
        width: 100%;
        height: 24px;
        display: flex;
        margin-bottom: 5px;

        &-title {
          font-size: 13px;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }

        &-info {
          width: 100px;
          padding-left: 5px;
          background: #3d3d3d;
          display: flex;
          align-items: center;

          &-icon {
            width: 14px;
            height: 14px;
            margin-right: 5px;
          }

          &-text {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
          }
        }

        &-other {
          width: calc(100% - 100px);
          height: 100%;
          padding-left: 5px;
          background: #4a4d4e;
          display: flex;
          align-items: center;
          overflow: hidden;

          .capacity {
            width: 100%;
            height: 100%;
            display: flex;
            align-items: center;

            img {
              width: 14px;
              height: 14px;
              margin-right: 5px;
            }
          }
        }
      }

      &-style:first-child {
        margin: 0;
      }
    }

    &-right {
      width: 24px;
      background: #3d3d3d;
      display: flex;
      justify-content: center;
      align-items: center;

      img {
        width: 16px;
        height: 16px;
      }
    }
  }
}

.warn {
  background: red;
}

.word-loop {
  white-space: nowrap;
  display: inline-block;
  animation: 5s loop linear infinite normal;
}

@keyframes loop {
  0% {
    transform: translateX(20px);
    -webkit-transform: translateX(20px);
  }

  100% {
    transform: translateX(-100%);
    -webkit-transform: translateX(-100%);
  }
}
</style>