<script setup lang="ts">
import dayjs from 'dayjs'
import { VueDraggable } from 'vue-draggable-plus'
import BasicInformation from './components/BasicInformation.vue'
import ContainersView from './components/ContainersView.vue'
import EmailView from './components/EmailView.vue'
import { cloneDeep } from 'lodash'
import { transportationMode } from '@/components/TransportationMode'

// 可拖拽模块的列表
const boxList = ref([
  { id: 1, name: 'Basic Information' },
  { id: 2, name: 'Containers' }
])

const infoContentRef = ref()

const isBusinessPartnersCollapse = ref(false)
const basicInformationRef = ref()
const handleBusinessPartnersCollapse = (isCollapse: boolean) => {
  // 这里使用数组的方式是因为basic information处于循环中，所以需要使用数组的方式来获取
  // 处理Business Partners折叠
  basicInformationRef.value[0].businessPartnersCollapse(isCollapse)
  // 处理Basic Information折叠
  basicInformationRef.value[0].basicInformationCollapse(isCollapse)
}

const handleDraggable = (type: string, id: number) => {
  const index = boxList.value.findIndex((item) => item.id === id)
  if (index === -1) return
  const curBox = cloneDeep(boxList.value[index])
  switch (type) {
    case 'Move to Top':
      if (index > 0) {
        boxList.value.splice(index, 1)
        boxList.value.unshift(curBox)
      }
      break
    case 'Move Up':
      if (index > 0) {
        const temp = cloneDeep(boxList.value[index - 1])
        boxList.value[index - 1] = curBox
        boxList.value[index] = temp
      }
      break
    case 'Move Down':
      if (index < boxList.value.length - 1) {
        const temp = cloneDeep(boxList.value[index + 1])
        boxList.value[index + 1] = curBox
        boxList.value[index] = temp
      }
      break
    case 'Move to Bottom':
      if (index < boxList.value.length - 1) {
        boxList.value.splice(index, 1)
        boxList.value.push(curBox)
      }
      break
    default:
      break
  }
}

const allData = ref()
const loading = ref(false)
const getData = () => {
  loading.value = true
  $api
    .getBookingDetail({
      status: 'Confirmed',
      // a: 'UjwCcE8rEPXDQ6jYG3xp9V208RYJ6UrRpAH%2FRna8t%2BqjYnUcZnqOnvrE4Gg',
      a: 'AjxXeBouEvrDQ6jYG3xp9V208RYJ6UrRpAH%2FRna8t%2BqjYnUcZnqOnvrE4Gg5'
    })
    .then((res: any) => {
      if (res.code === 200) {
        // 获取数据
        allData.value = res.data
      }
    })
    .finally(() => {
      loading.value = false
    })
}
getData()

const formatTime = (time: string) => {
  return time ? dayjs(time).format('MMM-DD-YYYY hh:mm A') : '--'
}
</script>

<template>
  <div class="booking-detail" v-vloading="loading">
    <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">Booking No. {{ allData?.transportInfo?.['bookingNo.'] }}</div>
        <VTag large type="Confirmed">{{ allData?.transportInfo?.status }}</VTag>
      </div>
      <div class="detail-info">
        <div class="item transport-way">
          <div class="place">
            <div class="title">Origin</div>
            <div class="content">
              <span>{{ allData?.transportInfo?.origin }}</span>
              <div class="line_container">
                <hr color="#000000" />
                <div class="right-icon"></div>
              </div>
            </div>
          </div>
          <div class="place">
            <div class="title">Destination</div>
            <div class="content">{{ allData?.transportInfo?.destination }}</div>
          </div>
        </div>
        <div class="item">
          <div class="title">ETD/ATD</div>
          <div class="content">
            <span
              >{{ formatTime(allData?.transportInfo?.etd) }}
              /
            </span>
            <span style="color: var(--color-neutral-1)">{{
              formatTime(allData?.transportInfo?.atd)
            }}</span>
          </div>
        </div>
        <div class="item">
          <div class="title">ETA/ATA</div>
          <div class="content">
            <span>{{ formatTime(allData?.transportInfo?.eta) }} / </span>
            <span>{{ formatTime(allData?.transportInfo?.ata) }}</span>
          </div>
        </div>
      </div>
    </div>
    <div class="info-content">
      <VueDraggable
        style="display: flex; flex-direction: column; gap: 16px"
        ref="infoContentRef"
        ghost-class="ghost-class"
        :forceFallback="true"
        fallback-class="fallback-class"
        v-model="boxList"
        handle=".handle-draggable"
      >
        <template v-for="item in boxList" :key="item.id">
          <!-- Basic Information -->
          <div v-if="item.id === 1">
            <VBox
              :id="item.id"
              v-model="isBusinessPartnersCollapse"
              @draggable="handleDraggable"
              @collapse="handleBusinessPartnersCollapse"
            >
              <template #header>
                <div style="display: flex; border-radius: 12px 12px 0 0">
                  <div class="basic-information-header">
                    <span>Basic Information</span>
                    <SeeAllIcon
                      v-model="isBusinessPartnersCollapse"
                      @collapse="handleBusinessPartnersCollapse"
                    ></SeeAllIcon>
                  </div>
                  <div style="padding-left: 16px">Business Partners</div>
                </div>
              </template>
              <template #content>
                <BasicInformation :data="allData" ref="basicInformationRef"></BasicInformation>
              </template>
            </VBox>
          </div>

          <!-- Containers -->
          <div v-if="item.id === 2">
            <VBox :id="item.id" :isSeeAll="false" @draggable="handleDraggable">
              <template #header>Containers</template>
              <template #content>
                <ContainersView :data="allData"></ContainersView>
              </template>
            </VBox>
          </div>
        </template>
      </VueDraggable>
    </div>
    <EmailView :data="allData"></EmailView>
  </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);
}
.booking-detail {
  padding: 0 16px 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: 0 16px;
      border-bottom: 1px solid var(--color-border);
      .no {
        margin-left: 6px;
        margin-right: 8px;
        font-size: 24px;
        font-weight: 700;
      }
    }
    .detail-info {
      display: flex;
      height: 96px;
      // border-bottom: 1px solid var(--color-border);
      & > .item {
        flex: 1;
        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 {
        display: flex;
        flex-direction: row;
        justify-content: flex-start;
        gap: 16px;
        .place {
          flex: 1;
        }
        .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;
            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;
          }
        }
      }
    }
  }
}

.ghost-class {
  opacity: 0;
}
.fallback-class {
  opacity: 1 !important;
  // background-color: #fff1e5 !important;
  cursor: move !important;
  background-color: #fff;
  box-shadow: 4px 4px 32px 0px rgba(0, 0, 0, 0.2);
  border-radius: 12px;
}
.info-content {
  margin-top: -1px;
  margin-bottom: 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);
  }
}
</style>
