<script setup lang="ts">
import { transportationMode } from '@/components/transportationMode'
import { useRouter } from 'vue-router'
import { getTimezone } from '@/utils/tools'
import dayjs from 'dayjs'

const router = useRouter()

interface EventCardPropsData {
  type: string // 'milestone' | 'container' | 'delay' | 'change'
  numericRecords?: number // 多条记录数 (Daily Update消息)
  isRead?: boolean // 是否已读 （true 已读，false 未读）
  title?: string // Milestone Update
  mode?: string // 运输方式
  no: string // HBOL: SHJN2301234
  tag: string // tag  Booking Confirmed
  location: string
  timezone?: string // 时区
  time: string
  timeLabel: string
  serial_no?: string // 单号 用来跳转到Tracking详情页
  order_from?: string // 订单来源 用来跳转到Tracking详情页
  insert_date_format?: string // 用来跳转到System Message详情页
  frequency_type?: string // 用来跳转到System Message详情页
  rules_type?: string // 用来跳转到System Message详情页
  is_display_hbol: boolean // 是否显示HBOL
  previous?: {
    date: string
    tag: string
    time: string
    timezone: string
  }
  info?: {
    route?: []
    etdOrdeparturNum?: number
    etaOrarrivalNum?: number
    time: string
    timeLabel: string
    delayTimeTip: string
    timezone?: string // 时区
    leg?: []
  }
}

const props = defineProps<{
  data: EventCardPropsData
}>()

const emit = defineEmits<{ seeAll: []; jumpTracking: [] }>()
const handleSeeAll = (data: EventCardPropsData) => {
  emit('seeAll')
  router.push({
    name: 'System Message Detail',
    query: {
      frequency_type: data.frequency_type,
      insert_date_format: data.insert_date_format,
      rules_type: data.rules_type
    }
  })
}

const jumpTracking = (data: EventCardPropsData) => {
  emit('jumpTracking')
  router.push({
    path: '/tracking/detail',
    query: { a: data.serial_no, _schemas: data.order_from }
  })
}
</script>

<template>
  <div class="notification-card" :class="{ 'is-read': data.isRead }">
    <div class="header" v-if="data.title">
      <div class="status-icon"></div>
      <div class="title">{{ data.title }}</div>
    </div>
    <div class="content">
      <div
        class="more-tips"
        v-if="(data.type === 'milestone' || data.type === 'container') && data.numericRecords"
      >
        <span>Latest Status Updates ({{ data.numericRecords }}) </span>
        <el-button @click="handleSeeAll(data)" class="see-all-icon el-button--text">
          See All
          <span class="font_family icon-icon_next_b"></span>
        </el-button>
      </div>
      <div class="more-tips" v-if="data.info?.etdOrdeparturNum || data.info?.etaOrarrivalNum">
        <div>
          <span v-if="data.info?.etdOrdeparturNum"
            >{{ data.type === 'delay' ? 'Departure Delay' : 'ETD Change' }} ({{
              data.info?.etdOrdeparturNum
            }})</span
          >
          <span v-if="data.info?.etdOrdeparturNum && data.info?.etaOrarrivalNum">
            &nbsp;&nbsp;|&nbsp;&nbsp;</span
          >
          <span v-if="data.info?.etaOrarrivalNum">
            {{ data.type === 'delay' ? 'Arrival Delay' : 'ETA Change' }} ({{
              data.info?.etaOrarrivalNum
            }})
          </span>
        </div>
        <el-button @click="handleSeeAll(data)" class="see-all-icon el-button--text">
          See All
          <span class="font_family icon-icon_next_b"></span>
        </el-button>
      </div>

      <div class="base-info">
        <!-- 除了container类型，其他类型都显示运输方式图标 -->
        <div style="display: inline-block" v-if="data.type !== 'container'">
          <span class="font_family" :class="[`icon-${transportationMode?.[data.mode]}`]"></span>
          <span @click="jumpTracking(data)" class="no no-link"
            >{{ data.is_display_hbol ? 'HBOL:' : 'MAWB:' }} {{ data.no }}</span
          >
        </div>
        <!-- container类型显示图标 -->
        <div v-else>
          <span class="font_family icon-icon_container__filled_b"></span>
          <span class="no">Container: {{ data.no }}</span>
        </div>
        <div class="tag" :class="{ delay: data.type === 'delay', change: data.type === 'change' }">
          <span class="dot"></span>
          <span class="text">{{ data.tag }}</span>
        </div>
      </div>
      <div class="route" v-if="data?.info?.route?.length > 0">
        <span class="font_family icon-icon_route_b"></span>
        <span>Route:&nbsp;</span>
        <template v-for="(item, index) in data.info.route" :key="index">
          <span>{{ item }}</span
          ><span style="margin: 0 3px" v-if="index !== data.info.route.length - 1">→</span>
        </template>
      </div>
      <!-- change多程情况中的Leg-->
      <div class="location" v-if="data?.info?.leg?.length > 0">
        <span class="font_family icon-icon_location_b"></span>
        <span>Current Leg:&nbsp;</span>
        <template v-for="(item, index) in data.info.leg" :key="index">
          <span>{{ item }}</span
          ><span style="margin: 0 3px" v-if="index !== data.info.leg.length - 1">→</span>
        </template>
      </div>
      <div class="location" v-if="data.location">
        <span class="font_family icon-icon_location_b"></span>
        <span>{{ data.location }}</span>
      </div>
      <div
        :class="{ 'delay-time': data.type === 'delay', 'change-time': data.type === 'change' }"
        v-if="(data.type === 'delay' || data.type === 'change') && data.info?.time"
      >
        <span
          v-if="data.type === 'delay'"
          style="margin-right: 6px"
          class="font_family icon-icon_delay_b"
        ></span>
        <span v-else class="font_family icon-icon_time_b"></span>
        <span style="margin-right: 2px" v-if="data.info.timeLabel">{{ data.info.timeLabel }}:</span>
        <span style="margin-right: 3px">{{
          dayjs(data.info.time).format('MMM DD, YYYY HH:mm')
        }}</span>
        <span>{{ getTimezone(data.info.timezone, data.info.time) }}</span>
      </div>
      <!-- <div class="change-time" v-if="data.type === 'change' && data.info?.time">
        <span style="margin-left: 1px" class="font_family icon-icon_time_b"></span>
        <span>{{ data.info?.time }}</span>
      </div> -->
      <div class="time" :class="{ grey: data.type === 'delay' || data.type === 'change' }">
        <span class="font_family icon-icon_time_b"></span>
        <span style="margin-right: 3px" v-if="data.timeLabel">{{ data.timeLabel }}:</span>
        <span style="margin-right: 3px">{{ dayjs(data.time).format('MMM DD, YYYY HH:mm') }}</span>
        <span>{{ getTimezone(data.timezone, data.time) }}</span>
      </div>
      <div class="previous" v-if="data.previous">
        <span class="previous-icon"></span>
        <span
          >{{ data.previous.tag }}&nbsp;({{ data.previous.time }}
          {{ getTimezone(data.previous.timezone, data.previous.time) }})</span
        >
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.notification-card {
  margin-bottom: 16px;
  &.is-read {
    & > .header {
      .status-icon {
        background-color: var(--color-border);
      }
      .title {
        color: #b5b9bf;
      }
    }
  }
  .header {
    display: flex;
    align-items: center;
    margin-bottom: 8px;
    .status-icon {
      width: 8px;
      height: 8px;
      border-radius: 50%;
      background-color: var(--color-theme);
      margin-right: 10px;
    }
    .title {
      flex: 1;
      font-weight: 700;
    }
  }
  .content {
    padding: 16px 8px;
    padding-top: 0;
    background-color: var(--color-header-bg);
    border-radius: 6px;
    .more-tips {
      display: flex;
      justify-content: space-between;
      padding: 8px 0;
      border-bottom: 1px dashed var(--color-border);
      font-size: 12px;
      line-height: 24px;
      .see-all-icon {
        width: 68px;
        height: 24px;
        font-size: 12px;
        :deep(span) {
          color: var(--color-theme);
        }
      }
    }
    .base-info {
      display: flex;
      padding-top: 16px;
      .no {
        margin-left: 8px;
        font-weight: 700;
        line-height: 18px;
      }
      .no-link {
        &:hover {
          color: var(--color-theme);
          cursor: pointer;
        }
      }
      .tag {
        display: flex;
        align-items: center;
        height: 18px;
        margin-top: 2px;
        margin-left: 4px;
        padding-left: 8px;
        padding-right: 6px;
        background-color: var(--color-milestone-tag-bg);
        border-radius: 3px;
        &.delay {
          background-color: var(--color-delay-tag-bg);
          .dot {
            background-color: #c9353f;
          }
          .text {
            color: #c9353f;
          }
        }
        &.change {
          background-color: var(--color-change-tag-bg);
          .dot {
            background-color: #edb82f;
          }
          .text {
            margin-top: 0;
            color: #edb82f;
          }
        }
        .dot {
          width: 5px;
          height: 5px;
          border-radius: 50%;
          background-color: #5bb462;
          margin-right: 4px;
        }
        .text {
          font-size: 10px;
          font-weight: 600;
          color: #5bb462;
          line-height: 10px;
          vertical-align: middle;
        }
      }
    }
    .route,
    .location,
    .change-time,
    .delay-time,
    .time {
      display: flex;
      align-items: center;
      margin-top: 8px;
      height: 16px;
      &.grey {
        span {
          color: var(--color-neutral-3);
        }
      }
      span {
        color: var(--color-neutral-2);
        font-size: 12px;
        line-height: 14px;
      }
      .font_family {
        font-size: 16px;
        font-family: 'iconfont';
        color: var(--color-neutral-2);
        margin-right: 8px;
      }
    }
    div.delay-time {
      height: 19px;
      margin-top: 6px;
      margin-bottom: -2px;
      span,
      .font_family {
        color: #c9353f;
      }
      span {
        display: inline-block;
        margin-top: 3px;
        line-height: 19px;
      }
      .font_family {
        margin: 0 6px 0 -1px;
        line-height: 12px;
        font-size: 18px;
      }
    }
    div.change-time {
      span,
      .font_family {
        color: #edb82f;
      }
    }
    .previous {
      margin-top: 8px;
      padding-left: 8px;
      line-height: 24px;
      background-color: var(--color-previous-bg);
      border-radius: 6px;
      span {
        font-size: 12px;
      }
      .previous-icon {
        display: inline-block;
        width: 4px;
        height: 4px;
        background-color: var(--color-neutral-1);
        border-radius: 50%;
        margin-right: 8px;
        vertical-align: middle;
      }
    }
  }
}
</style>
