<script setup lang="ts">
import type { BorrowReturnDynamicDTO, SearchBorrowReturnDynamicQuery } from "@/common/apis/manage/order"
import { getBorrowReturnDynamicApi } from "@/common/apis/manage/order"
import { showToast, showImagePreview } from "vant"
import { computed, onMounted, ref } from "vue"

// 加载状态（用于加载更多）
const loading = ref(false)
// 下拉刷新状态
const refreshing = ref(false)
// 动态列表
const dynamicList = ref<BorrowReturnDynamicDTO[]>([])
// 分页参数
const queryParams = ref<SearchBorrowReturnDynamicQuery>({
  pageNum: 1,
  pageSize: 10
})
// 总条数
const total = ref(0)
// 是否还有更多
const hasMore = computed(() => dynamicList.value.length < total.value)

// 获取借还动态列表
async function fetchBorrowReturnDynamic(isLoadMore = false) {
  if (!isLoadMore) {
    loading.value = true
  }
  try {
    const res = await getBorrowReturnDynamicApi(queryParams.value)
    if (res.code === 0) {
      if (isLoadMore) {
        // 加载更多：追加数据
        dynamicList.value = [...dynamicList.value, ...(res.data?.rows || [])]
      } else {
        // 首次加载或刷新：替换数据
        dynamicList.value = res.data?.rows || []
      }
      total.value = res.data?.total || 0
    } else {
      showToast(res.msg || "获取数据失败")
    }
  } catch (_error) {
    showToast("网络错误，请重试")
  } finally {
    loading.value = false
    refreshing.value = false
  }
}

// 加载更多
function loadMore() {
  if (loading.value || !hasMore.value) return
  queryParams.value.pageNum!++
  fetchBorrowReturnDynamic(true)
}

// 下拉刷新
function onRefresh() {
  refreshing.value = true
  queryParams.value.pageNum = 1
  dynamicList.value = []
  fetchBorrowReturnDynamic()
}

// 安全获取图片数组
function getImageArray(imagesStr?: string): string[] {
  if (!imagesStr || imagesStr.trim() === "") return []
  return imagesStr.split(",").filter(img => img.trim() !== "")
}

// 预览商品封面图片
function previewCoverImage(imgUrl: string) {
  if (!imgUrl) return
  showImagePreview([imgUrl])
}

// 预览归还图片组
function previewReturnImages(images: string[], startIndex = 0) {
  if (!images || images.length === 0) return
  /* showImagePreview({
    images,
    startPosition: startIndex,
    closeable: true
  }) */
 showImagePreview([images[startIndex]]);
}

// 预览审批图片组
function previewAuditImages(images: string[], startIndex = 0) {
  if (!images || images.length === 0) return
  /* showImagePreview({
    images,
    startPosition: startIndex,
    closeable: true
  }) */
 showImagePreview([images[startIndex]]);
}

// 初始化加载
onMounted(() => {
  fetchBorrowReturnDynamic()
})
</script>

<template>
  <div class="borrow-return-dynamic-page">
    <!-- 下拉刷新 -->
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <!-- 时间线容器 -->
      <div class="timeline-container">
        <!-- 时间线线条 -->
        <div class="timeline-line" />

        <!-- 动态条目 -->
        <div
          v-for="item in dynamicList"
          :key="item.orderGoodsId"
          class="timeline-item"
        >
          <!-- 时间点 -->
          <div class="timeline-dot">
            <div class="dot" :class="`dot-type-${item.dynamicType}`" />
          </div>

          <!-- 内容卡片 -->
          <div class="dynamic-card">
            <!-- 时间显示 -->
            <div class="dynamic-time">
              {{ item.dynamicType === 0 ? item.orderTime : item.operateTime }}
            </div>

            <!-- 卡片头部：类型和状态 -->
            <div class="card-header">
              <span class="dynamic-type" :class="`type-${item.dynamicType}`">
                {{ item.dynamicTypeStr }}
              </span>
              <span v-if="item.dynamicType === 1" class="audit-status" :class="`status-${item.status}`">
                {{ item.statusStr }}
              </span>
            </div>

            <!-- 分隔线 -->
            <van-divider />

            <!-- 商品信息 -->
            <div class="goods-info">
              <!-- 商品图片 -->
              <van-image
                v-if="item.coverImg"
                :src="item.coverImg"
                width="70"
                height="70"
                class="goods-image"
                fit="cover"
                @click="previewCoverImage(item.coverImg)"
              />
              <div v-else class="goods-image-placeholder">
                <div class="placeholder-icon">
                  商品
                </div>
              </div>
              <div class="goods-details">
                <div class="goods-name">
                  {{ item.goodsName }}
                </div>
                <div class="goods-price">
                  ¥{{ item.goodsPrice.toFixed(2) }}
                </div>
                <div class="order-info">
                  <div class="order-name">
                    {{ item.orderName }}
                  </div>
                  <div class="order-mobile">
                    {{ item.orderMobile }}
                  </div>
                </div>
                <div class="cabinet-info">
                  <span class="cabinet-name">{{ item.cabinetName }}</span>
                  <span class="cell-no">格口号: {{ item.cellNo }}</span>
                </div>
              </div>
            </div>

            <!-- 归还图片和审核图片 -->
            <div v-if="item.dynamicType === 1" class="image-section">
              <!-- 归还图片 -->
              <div v-if="getImageArray(item.images).length > 0" class="image-group">
                <div class="image-label">
                  归还图片
                </div>
                <div class="image-list">
                  <van-image
                    v-for="(img, imgIndex) in getImageArray(item.images)"
                    :key="imgIndex"
                    :src="img"
                    width="50"
                    height="50"
                    fit="cover"
                    class="return-image"
                    @click="previewReturnImages(getImageArray(item.images), imgIndex)"
                  />
                </div>
              </div>

              <!-- 审核图片 -->
              <div v-if="getImageArray(item.auditImages).length > 0" class="image-group">
                <div class="image-label">
                  审核图片
                </div>
                <div class="image-list">
                  <van-image
                    v-for="(img, imgIndex) in getImageArray(item.auditImages)"
                    :key="imgIndex"
                    :src="img"
                    width="50"
                    height="50"
                    fit="cover"
                    class="audit-image"
                    @click="previewAuditImages(getImageArray(item.auditImages), imgIndex)"
                  />
                </div>
              </div>
            </div>

            <!-- 审批人和审核说明 -->
            <div v-if="item.dynamicType === 1 && (item.auditName || item.auditRemark)" class="audit-info">
              <div v-if="item.auditName" class="audit-person">
                <span class="label">审批人:</span>
                <span class="value">{{ item.auditName }}</span>
              </div>
              <div v-if="item.auditRemark && item.auditRemark != '自动审批'" class="audit-remark">
                <span class="label">审核说明:</span>
                <span class="value">{{ item.auditRemark }}</span>
              </div>
            </div>
          </div>
        </div>

        <!-- 加载更多 -->
        <div v-if="hasMore" class="load-more">
          <van-button
            :loading="loading"
            loading-text="加载中..."
            @click="loadMore"
            block
          >
            加载更多
          </van-button>
        </div>
        <div v-else-if="dynamicList.length > 0" class="no-more">
          没有更多了
        </div>
      </div>

      <!-- 空状态 -->
      <div v-if="!loading && dynamicList.length === 0" class="empty-state">
        <van-empty description="暂无借还动态" />
      </div>
    </van-pull-refresh>
  </div>
</template>

<style scoped>
.borrow-return-dynamic-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #f0f4f8 100%);
  padding: 16px;
  padding-bottom: 32px;
}

.dynamic-time {
  padding: 10px 16px;
  font-size: 13px;
  color: #666;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 6px 6px 0 0;
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
  font-weight: 500;
  letter-spacing: 0.3px;
}

.timeline-container {
  position: relative;
  margin-top: 8px;
  min-height: 90vh;
  padding-left: 32px;
}

.timeline-line {
  position: absolute;
  left: 16px;
  top: 0;
  bottom: 0;
  width: 3px;
  background: linear-gradient(
    to bottom,
    rgba(25, 137, 250, 0.2) 0%,
    rgba(25, 137, 250, 0.6) 30%,
    rgba(25, 137, 250, 0.8) 50%,
    rgba(25, 137, 250, 0.6) 70%,
    rgba(25, 137, 250, 0.2) 100%
  );
  border-radius: 2px;
  box-shadow: 0 0 10px rgba(25, 137, 250, 0.15);
}

.timeline-item {
  position: relative;
  margin-bottom: 20px;
  transition:
    transform 0.3s ease,
    opacity 0.3s ease;
}

.timeline-item:hover {
  transform: translateY(-2px);
}

.timeline-dot {
  position: absolute;
  left: -30px;
  top: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 32px;
  z-index: 2;
}

.dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background-color: #1989fa;
  border: 3px solid white;
  box-shadow:
    0 0 0 3px #1989fa,
    0 0 15px rgba(25, 137, 250, 0.4);
  transition: all 0.3s ease;
  position: relative;
}

.dot::after {
  content: "";
  position: absolute;
  top: -3px;
  left: -3px;
  right: -3px;
  bottom: -3px;
  border-radius: 50%;
  background: inherit;
  filter: blur(5px);
  opacity: 0.4;
  z-index: -1;
}

.dot-type-0 {
  background-color: #1989fa;
  box-shadow:
    0 0 0 3px #1989fa,
    0 0 15px rgba(25, 137, 250, 0.4);
}

.dot-type-1 {
  background-color: #07c160;
  box-shadow:
    0 0 0 3px #07c160,
    0 0 15px rgba(7, 193, 96, 0.4);
}

.timeline-time {
  margin-top: 8px;
  font-size: 12px;
  color: #666;
  text-align: center;
  line-height: 1.4;
  white-space: nowrap;
}

.dynamic-card {
  border-radius: 10px;
  overflow: hidden;
  box-shadow:
    0 4px 20px rgba(0, 0, 0, 0.08),
    0 2px 6px rgba(0, 0, 0, 0.04),
    inset 0 1px 0 rgba(255, 255, 255, 0.8);
  background-color: white;
  border: 1px solid rgba(0, 0, 0, 0.05);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
}

.dynamic-card:hover {
  box-shadow:
    0 8px 30px rgba(0, 0, 0, 0.12),
    0 4px 12px rgba(0, 0, 0, 0.06),
    inset 0 1px 0 rgba(255, 255, 255, 0.9);
  transform: translateY(-4px);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  background: linear-gradient(to right, rgba(25, 137, 250, 0.03), rgba(7, 193, 96, 0.03));
  border-bottom: 1px solid rgba(0, 0, 0, 0.04);
}

.dynamic-type {
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 0.5px;
  position: relative;
}

.type-0 {
  color: #1989fa;
}

.type-1 {
  color: #07c160;
}

.audit-status {
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 16px;
  font-weight: 600;
  letter-spacing: 0.3px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: all 0.2s ease;
}

.audit-status:hover {
  transform: scale(1.05);
}

.status-1 {
  background: linear-gradient(135deg, #fff8e1 0%, #ffecb3 100%);
  color: #f57c00;
  border: 1px solid rgba(245, 124, 0, 0.2);
}

.status-2 {
  background: linear-gradient(135deg, #e8f5e9 0%, #c8e6c9 100%);
  color: #07c160;
  border: 1px solid rgba(7, 193, 96, 0.2);
}

.status-3 {
  background: linear-gradient(135deg, #ffebee 0%, #ffcdd2 100%);
  color: #ee0a24;
  border: 1px solid rgba(238, 10, 36, 0.2);
}

.goods-info {
  display: flex;
  padding: 16px;
  gap: 12px;
  align-items: center;
}

.goods-image {
  border-radius: 8px;
  overflow: hidden;
  flex-shrink: 0;
  border: 2px solid white;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  cursor: pointer;
}

.goods-image:hover {
  transform: scale(1.05);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.15);
}

.goods-image-placeholder {
  width: 70px;
  height: 70px;
  flex-shrink: 0;
  background: linear-gradient(135deg, #f5f5f5 0%, #e0e0e0 100%);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px dashed rgba(0, 0, 0, 0.1);
}

.placeholder-icon {
  color: #999;
  font-size: 14px;
  font-weight: 500;
}

.goods-details {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.goods-name {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  line-height: 1.4;
  letter-spacing: 0.2px;
}

.goods-price {
  font-size: 18px;
  font-weight: 700;
  color: #ee0a24;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.order-info {
  display: flex;
  gap: 12px;
  font-size: 13px;
  color: #666;
  background: rgba(0, 0, 0, 0.02);
  padding: 8px 10px;
  border-radius: 6px;
  margin-top: 4px;
}

.order-info .order-name,
.order-info .order-mobile {
  display: flex;
  align-items: center;
  gap: 6px;
}

.cabinet-info {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  color: #666;
  background: rgba(25, 137, 250, 0.04);
  padding: 8px 10px;
  border-radius: 6px;
  margin-top: 4px;
}

.cabinet-name {
  font-weight: 500;
  color: #333;
}

.cell-no {
  color: #1989fa;
  font-weight: 600;
  background: rgba(25, 137, 250, 0.1);
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 13px;
}

.image-section {
  padding: 0 16px 16px;
  display: flex;
  gap: 16px;
}

.image-group {
  flex: 1;
}

.image-label {
  font-size: 13px;
  color: #666;
  margin-bottom: 8px;
  font-weight: 500;
  padding-left: 8px;
  border-left: 3px solid #1989fa;
}

.image-list {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.return-image,
.audit-image {
  border-radius: 6px;
  overflow: hidden;
  border: 2px solid white;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  cursor: pointer;
}

.return-image:hover,
.audit-image:hover {
  transform: translateY(-3px);
  box-shadow: 0 6px 15px rgba(0, 0, 0, 0.15);
}

.audit-info {
  padding: 0 16px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 13px;
}

.audit-person,
.audit-remark {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  background: rgba(0, 0, 0, 0.02);
  padding: 10px;
  border-radius: 8px;
}

.label {
  color: #666;
  min-width: 70px;
  font-weight: 500;
}

.value {
  color: #333;
  flex: 1;
  line-height: 1.5;
}

.load-more {
  text-align: center;
  padding: 20px 0;
}

.load-more .van-button {
  background: linear-gradient(135deg, #1989fa 0%, #36b5ff 100%);
  color: white;
  border: none;
  border-radius: 25px;
  padding: 12px 24px;
  font-weight: 600;
  font-size: 15px;
  box-shadow: 0 4px 15px rgba(25, 137, 250, 0.3);
  transition: all 0.3s ease;
}

.load-more .van-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(25, 137, 250, 0.4);
}

.load-more .van-button:active {
  transform: translateY(0);
}

.no-more {
  text-align: center;
  padding: 24px 0;
  color: #999;
  font-size: 14px;
  font-style: italic;
}

.empty-state {
  padding: 80px 0;
}

/* 响应式调整 */
/* @media (max-width: 400px) {
  .borrow-return-dynamic-page {
    padding: 12px;
  }

  .timeline-container {
    padding-left: 30px;
  }

  .timeline-dot {
    left: -30px;
    width: 30px;
  }

  .goods-info {
    flex-direction: column;
    gap: 16px;
    align-items: flex-start;
  }

  .goods-image,
  .goods-image-placeholder {
    width: 100%;
    height: auto;
    aspect-ratio: 1;
  }

  .image-section {
    flex-direction: column;
    gap: 16px;
  }

  .order-info {
    flex-direction: column;
    gap: 8px;
  }
}
 */
/* 动画效果 */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(15px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.timeline-item {
  animation: fadeIn 0.4s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

/* 大屏幕优化 */
/* @media (min-width: 768px) {
  .borrow-return-dynamic-page {
    max-width: 800px;
    margin: 0 auto;
    padding-top: 24px;
  }

  .dynamic-card {
    border-radius: 16px;
  }
} */

/* 美化分隔线 */
.van-divider {
  margin: 0 16px;
  border-color: rgba(0, 0, 0, 0.06);
}

/* 美化下拉刷新区域 */
.van-pull-refresh {
  min-height: calc(100vh - 32px);
}
</style>
