<template>
  <div class="content" @click="onClickLocation">
    <div class="content-title" v-if="type === 'AIRPORT'">
      <div class="content-title-left">
        <img class="content-title-left-icon" :src="taskSrc">
        <div>
          无任务
        </div>
      </div>
      <div class="content-title-right">
        N/A
      </div>
    </div>
    <div class="content-info">
      <div class="content-info-left">
        <div class="content-info-left-one">
          {{ device.callsign }}
        </div>
        <div class="content-info-left-two">
          <div class="content-info-left-two-info">
            <img class="content-info-left-three-info-icon" :src="aircraftSrc">
            <div class="content-info-left-two-info-text">
              <span class="scrollable-text">
                {{ getTextByModeCode(deviceInfo[device.sn] ? deviceInfo[device.sn].mode_code : EModeCode.Disconnected)
                }}
              </span>
            </div>
          </div>
          <div class="content-info-left-two-other">
            {{ deviceReason }}
          </div>
        </div>
        <div class="content-info-left-three">
          <div class="content-info-left-three-info">
            <img class="content-info-left-three-info-icon" :src="controllerSrc">
            <div class="content-info-left-three-info-text">
              {{ device.gateway.callsign }}
            </div>
          </div>
          <div class="content-info-left-three-other">
            <div class="capacity" v-if="capacity">
              <img :src="batteryOneSrc" v-if="capacity >= 75 && capacity < 100">
              <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>
        </div>
      </div>
      <div class="content-info-right">
        <img :src="deviceInfoSrc" @click.stop="onClickLookInfo" v-if="type === 'MANUAL'">
        <img :src="dockInfoSrc" @click.stop="onClickLookInfo" v-else-if="type === 'AIRPORT'">
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { computed } from 'vue';
import taskSrc from './icons/task.svg';
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 deviceInfoSrc from './icons/deviceInfo.svg';
import dockInfoSrc from './icons/dockInfo.svg';
import { useMyStore } from '/@/store';
import { getTextByModeCode } from '/@/utils/index'
import { OnlineDevice, EModeCode } from '/@/types/device';
import { wgs84togcj02 } from '/@/vendors/coordtransform'
import { getRoot } from '/@/root';

interface Props {
  type: 'MANUAL' | 'AIRPORT',
  device: OnlineDevice,
  onClickLookInfo: () => void,
};

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

});

const root: any = getRoot()

const store = useMyStore()

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

// 设备原因
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;
  }
});

const onClickLocation = () => {
  const sn = props.device.gateway.sn;
  const gatewayInfo = store.state.deviceState.gatewayInfo[sn];
  if (gatewayInfo) {
    const coordinate = wgs84togcj02(gatewayInfo.longitude, gatewayInfo.latitude);
    root.$map.setCenter(coordinate)
  }
}
</script>

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

  &-title {
    width: 100%;
    height: 30px;
    border-bottom: 1px solid #3d3d3d;
    display: flex;
    align-items: center;

    &-left {
      width: 80px;
      height: 100%;
      padding-left: 5px;
      background: #4d4d4d;
      display: flex;
      align-items: center;

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

    &-right {
      margin-left: 5px;
    }
  }

  &-info {
    display: flex;

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

      &-one {
        width: 100%;
        height: 24px;
      }

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

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

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

          &-text {
            width: 50px;
            white-space: nowrap;
            overflow: hidden;
          }
        }

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

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

        &-info {
          width: 75px;
          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% - 75px);
          height: 100%;
          padding-left: 5px;
          background: #4a4d4e;

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

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

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

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

  .scrollable-text {
    width: 100%;
    display: inline-block;
    animation: scroll-left 5s linear infinite;

    @keyframes scroll-left {
      0% {
        transform: translateX(100%);
      }

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