<template>
  <div class="content">
    <div class="content-title">
      <div class="content-title-left">
        <img class="content-title-left-icon" :src="taskSrc">
        <div class="content-title-left-text">
          {{ taskName }}
        </div>
      </div>
      <div class="content-title-right"
        :style="dockInfo[dock.gateway.sn] && dockInfo[dock.gateway.sn].link_osd?.flighttask_step_code !== 255 ? 'color:#00EE8B' : 'color:red'">
        {{ getTextByDockTaskCode(dockInfo[dock.gateway.sn] ? dockInfo[dock.gateway.sn].link_osd?.flighttask_step_code :
          EDockModeCode.Disconnected) }}
      </div>
    </div>
    <div class="content-info">
      <div class="content-info-left" :style="{ width: lookInfo ? 'calc(100% - 20px)' : '100%' }">
        <div class="content-info-left-style">
          <div class="content-info-left-style-title">
            {{ dock.gateway.callsign }} - {{ dock.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="dockSrc">
            <div class="content-info-left-style-info-text"
              :style="dockInfo[dock.gateway.sn] && dockInfo[dock.gateway.sn].basic_osd?.mode_code !== EDockModeCode.Disconnected ? 'color:#00EE8B' : 'color:red'">
              {{ getTextByDockModeCode(dockInfo[dock.gateway.sn] ? dockInfo[dock.gateway.sn].basic_osd?.mode_code
                : EDockModeCode.Disconnected) }}
            </div>
          </div>
          <div :class="[
            'content-info-left-style-other',
            hmsInfo[dock.gateway.sn] && hmsInfo[dock.gateway.sn][0]?.level === 0 ? 'notice' : '',
            hmsInfo[dock.gateway.sn] && hmsInfo[dock.gateway.sn][0]?.level === 1 ? 'caution' : '',
            hmsInfo[dock.gateway.sn] && hmsInfo[dock.gateway.sn][0]?.level === 2 ? 'warn' : '',
          ]">
            <div v-if="hangarLogInfo">
              <span class="word-loop">
                {{ hangarLogInfo }}
              </span>
            </div>
            <div v-else>
              N/A
            </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="aircraftSrc">
            <div class="content-info-left-style-info-text"
              :style="deviceInfo[dock.sn] && deviceInfo[dock.sn].mode_code !== EModeCode.Disconnected ? 'color:#00EE8B' : 'color:red'">
              {{ getTextByModeCode(deviceInfo[dock.sn] ? deviceInfo[dock.sn].mode_code : EModeCode.Disconnected) }}
            </div>
          </div>
          <div :class="[
            'content-info-left-style-other',
            hmsInfo[dock.sn] && hmsInfo[dock.sn][0].level === 0 ? 'notice' : '',
            hmsInfo[dock.sn] && hmsInfo[dock.sn][0].level === 1 ? 'caution' : '',
            hmsInfo[dock.sn] && hmsInfo[dock.sn][0].level === 2 ? 'warn' : '',
          ]">
            <div v-if="aircraftLogInfo">
              <span class="word-loop">
                {{ aircraftLogInfo }}
              </span>
            </div>
            <div v-else>
              N/A
            </div>
          </div>
        </div>
      </div>
      <div class="content-info-right" v-if="lookInfo">
        <a-button style="padding: 0;" type="text"
          :disabled="!(dockInfo[dock.gateway.sn] && dockInfo[dock.gateway.sn].basic_osd?.mode_code !== EDockModeCode.Disconnected)"
          @click.stop="onClickLookInfo">
          <img :src="infoSelectedSrc" v-if="osdVisible.visible && osdVisible.sn === dock.sn">
          <img :src="infoSrc" v-else>
        </a-button>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { computed } from 'vue';
import taskSrc from './icons/task.svg';
import aircraftSrc from './icons/aircraft.svg';
import dockSrc from './icons/dockInfo.svg';
import infoSrc from './icons/info.svg';
import infoSelectedSrc from './icons/info_selected.svg';
import { useMyStore } from '/@/store';
import { getTextByModeCode, getTextByDockModeCode, getTextByDockTaskCode } from '/@/utils/index'
import { OnlineDevice, EModeCode, EDockModeCode } from '/@/types/device';

interface Props {
  dock: OnlineDevice,
  lookInfo: boolean,
  onClickLookInfo?: () => void,
};

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

});

const store = useMyStore()

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

// 任务名称
const taskName = computed(() => {
  const dockTask = store.state.dockTask[props.dock.gateway.sn];
  if (dockTask) {
    return dockTask.name;
  } else {
    return '无任务';
  }
})

// 飞行器告警信息
const aircraftLogInfo = computed(() => {
  const hmsList = hmsInfo.value[props.dock.sn] || [];
  const info = hmsList[0];
  if (info) {
    return info.message_zh;
  } else {
    return '';
  }
})

// 机库告警信息
const hangarLogInfo = computed(() => {
  const hmsList = hmsInfo.value[props.dock.gateway.sn] || [];
  const info = hmsList[0];
  if (info) {
    return info.message_zh;
  } else {
    return '';
  }
})
</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: 105px;
      height: 100%;
      padding-left: 5px;
      background: #4d4d4d;
      display: flex;
      align-items: center;

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

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

    &-right {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      margin-left: 5px;
    }
  }

  &-info {
    display: flex;

    &-left {
      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);
          padding-left: 5px;
          background: #4a4d4e;
          display: flex;
          align-items: center;
          overflow: hidden;
        }
      }

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

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

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

.notice {
  background: $success;
}

.caution {
  background: orange;
}

.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>