<script setup lang="ts">
import BasicInformation from './BasicInformation.vue'
import MilestonesTable from './MilestonesTable.vue'
import { transportationMode } from '@/components/TransportationMode'
import { useRoute } from 'vue-router'
import { useOverflow } from '@/hooks/useOverflow'
import { formatTimezone } from '@/utils/tools'

const route = useRoute()

const allData: any = ref({
  transportInfo: {
    'Tracking No.': '',
    mode: '',
    status: '',
    origin: '',
    destination: '',
    etd: '',
    atd: '',
    eta: '',
    ata: ''
  },
  basicInfo: {
    'HAWB/HBOL': '',
    Carrier_Booking_No: '',
    PO_NO: ''
  },
  businessPartners: {
    origin: {
      company: '',
      address: '',
      phone: ''
    },
    destination: {
      company: '',
      address: '',
      phone: ''
    }
  },
  packing: {
    'Quantity/Unit': '',
    'G. Weight': '',
    'Ch. Weight': '',
    Volume: ''
  }
})
const sharedData = JSON.parse(sessionStorage.getItem('publicTrackingData') || '{}')
sessionStorage.removeItem('publicTrackingData')
if (Object.keys(sharedData).length === 0) {
  const loading = ref(false)
  loading.value = true
  $api
    .getPublicTrackingDetail({ reference_number: route.query.searchNo })
    .then((res) => {
      if (res.code === 200) {
        const { data } = res
        allData.value = data.data
      }
    })
    .finally(() => {
      loading.value = false
    })
} else {
  allData.value = sharedData
}

const originRef = ref()
const destinationRef = ref()
const { isOverflow: isOriginOverflow } = useOverflow(originRef, allData)
const { isOverflow: isDestinationOverflow } = useOverflow(destinationRef, allData)
</script>

<template>
  <div class="tracking-detail">
    <div class="header">
      <div class="detail-status">
        <span
          class="font_family"
          :class="[`icon-${transportationMode?.[allData?.transportInfo?.mode]}`]"
          style="font-size: 64px"
        ></span>
        <div class="no">Tracking No. {{ allData.transportInfo['Tracking No.'] }}</div>
        <VTag large :type="allData.transportInfo?.status">{{ allData.transportInfo?.status }}</VTag>
      </div>
      <div class="detail-info">
        <div class="item transport-way">
          <div class="origin">
            <div class="title">Origin</div>
            <div class="content">
              <el-tooltip v-if="isOriginOverflow" placement="top">
                <template #content>{{ allData?.transportInfo?.origin || '--' }}</template>
                <span ref="originRef" class="info single-line-ellipsis">{{
                  allData?.transportInfo?.origin || '--'
                }}</span>
              </el-tooltip>
              <span ref="originRef" v-else class="info single-line-ellipsis">{{
                allData?.transportInfo?.origin || '--'
              }}</span>
              <div class="line_container">
                <hr color="#000000" />
                <div class="right-icon"></div>
              </div>
            </div>
          </div>
          <div class="destination">
            <div class="title">Destination</div>
            <div class="content">
              <el-tooltip v-if="isDestinationOverflow" placement="top">
                <template #content>{{ allData?.transportInfo?.destination || '--' }}</template>
                <span ref="destinationRef" class="info single-line-ellipsis">{{
                  allData?.transportInfo?.destination || '--'
                }}</span>
              </el-tooltip>
              <span v-else ref="destinationRef" class="info single-line-ellipsis">{{
                allData?.transportInfo?.destination || '--'
              }}</span>
            </div>
          </div>
        </div>
        <div class="item">
          <div class="title">ETD/ATD</div>
          <div class="content">
            <span
              >{{
                formatTimezone(allData?.transportInfo?.etd, allData?.transportInfo?.etd_timezone)
              }}
              /
            </span>
            <span style="color: var(--color-neutral-1)">{{
              formatTimezone(allData?.transportInfo?.atd, allData?.transportInfo?.atd_timezone)
            }}</span>
          </div>
        </div>
        <div class="item">
          <div class="title">ETA/ATA</div>
          <div class="content">
            <span
              >{{
                formatTimezone(allData?.transportInfo?.eta, allData?.transportInfo?.eta_timezone)
              }}
              /
            </span>
            <span>{{
              formatTimezone(allData?.transportInfo?.ata, allData?.transportInfo?.ata_timezone)
            }}</span>
          </div>
        </div>
      </div>
    </div>
    <div class="info-content">
      <VBox :is-see-all="false" :is-draggable="false" ref="basicInformationRef">
        <template #header>
          <div style="display: flex; border-radius: 12px 12px 0 0">
            <div class="basic-information-header">
              <span>Basic Information</span>
            </div>
            <div style="padding-left: 16px">Business Partners</div>
          </div>
        </template>
        <template #content>
          <BasicInformation :data="allData"></BasicInformation>
        </template>
      </VBox>
      <!-- Milestones -->
      <VBox style="margin-top: 8px" :isSeeAll="false" :is-draggable="false">
        <template #header>Milestones</template>
        <template #content>
          <MilestonesTable :data="allData"></MilestonesTable>
        </template>
      </VBox>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.basic-information-header {
  display: flex;
  justify-content: space-between;
  width: 334px;
  padding-right: 16px;
  border-right: 1px solid var(--color-border);
}
.tracking-detail {
  padding-bottom: 16px;
  & > .header {
    background: linear-gradient(
      251deg,
      rgba(255, 255, 255, 0.3),
      rgba(255, 244, 235, 0.5) 22.66%,
      rgba(240, 243, 255, 0.5) 44.57%,
      rgba(224, 247, 249, 0.6) 80.46%,
      rgba(255, 255, 255, 0.3)
    );

    .detail-status {
      position: relative;
      display: flex;
      align-items: center;
      height: 64px;
      padding-left: 14px;
      border-bottom: 1px solid var(--color-border);
      margin: 0 8px;
      .no {
        margin-left: 6px;
        margin-right: 8px;
        font-size: 24px;
        font-weight: 700;
      }
    }
    .detail-info {
      display: flex;
      height: 96px;
      margin: 0 8px;
      & > .item {
        flex: 1 1 33%;
        max-width: 40%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        gap: 8px;
        padding: 16px;
        border-right: 1px solid var(--color-border);
        &:last-child {
          border-right: none;
        }
        .title {
          font-size: 12px;
          color: var(--color-neutral-2);
        }
        .content {
          font-weight: 700;
          color: var(--color-neutral-2);
        }
      }
      .transport-way {
        flex: 1 1 20%;
        display: flex;
        flex-direction: row;
        justify-content: flex-start;
        gap: 16px;
        .origin {
          flex: 1;
          width: calc(60% - 16px);
        }
        .destination {
          max-width: 40%;
        }
        .title {
          margin-top: 11px;
        }
        .content {
          position: relative;
          display: flex;
          align-items: center;
          margin-top: 8px;
          font-size: 18px;
          font-weight: 700;
          color: var(--color-neutral-1);
          .line_container {
            flex: 1;
            position: relative;
            min-width: 26px;
            margin-left: 16px;
          }
          .line_container hr {
            width: 100%;
            border-radius: 0 2px 6px 0;
          }
          .line_container .right-icon {
            position: absolute;
            top: -3px;
            right: 0px;
            width: 10px;
            height: 11px;
            border-top: 2px solid #000000;
            transform: rotate(30deg);
            border-radius: 0 1px 0 0;
          }
        }
      }
    }
  }
}

.info-content {
  margin: -1px 24px 8px;
}

:deep(.data-item) {
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: flex-start;
  gap: 8px;
  .title {
    font-size: 12px;
    color: var(--color-neutral-2);
  }
  .content {
    min-height: 16px;
    font-size: 14px;
    font-weight: 700;
    color: var(--color-neutral-1);
  }
}
.single-line-ellipsis {
  display: inline-block; /* 或者根据需要使用 inline-block */
  white-space: nowrap; /* 不换行 */
  overflow: hidden; /* 隐藏超出部分 */
  text-overflow: ellipsis; /* 超出部分显示省略号 */
}
</style>
