<script setup lang="ts">
import { computed } from 'vue';
import { Avatar, Image } from 'ant-design-vue';

const props = defineProps<{
  data?: any;
}>();

// 协商历史条目类型
interface NegotiationItem {
  avatar: string;
  name: string;
  time: string;
  action: string;
  title?: string;
  content?: string;
  fields?: { label: string; value: string }[];
  images?: string[];
}

// 默认假数据 (模拟图片中的内容)
const mockNegotiationHistory: NegotiationItem[] = [
  {
    avatar: 'https://img.yzcdn.cn/vant/cat.jpeg',
    name: '谢芳',
    time: '2025-02-19 14:30:35',
    action: '撤销退款申请',
  },
  {
    avatar: 'https://img0.baidu.com/it/u=670138986,218049280&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
    name: '平台',
    time: '2025-02-19 14:30:35',
    action: '平台拒绝了申请',
    title: '平台拒绝了申请',
    content: '请选择原因为与卖家协商一致',
  },
  {
    avatar: 'https://img.yzcdn.cn/vant/cat.jpeg',
    name: '谢芳',
    time: '2025-02-19 14:30:27',
    action: '发起了退款申请',
    fields: [
      { label: '商品状态', value: '未收货' },
      { label: '退款类型', value: '退款' },
      { label: '退款金额', value: '¥240.00' },
      { label: '退款原因', value: '少件（含缺少配件）' },
      { label: '退款说明', value: 'uudksjj' },
    ],
    images: [
      'https://img0.baidu.com/it/u=3025010615,2026135017&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
      'https://img1.baidu.com/it/u=1067201099,2862800611&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
      'https://img2.baidu.com/it/u=3025010615,2026135017&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
      'https://img3.baidu.com/it/u=1067201099,2862800611&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
      'https://img0.baidu.com/it/u=3025010615,2026135017&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
    ]
  }
];

// 实际使用的数据 (如果有 props.data 则可进行转换，暂用 mock)
const displayHistory = computed(() => {
  return mockNegotiationHistory;
});
</script>

<template>
  <div class="negotiations-container">
    <div class="negotiation-list">
      <div v-for="(item, index) in displayHistory" :key="index" class="negotiation-item">
        <!-- 头部信息: 头像、名称、时间 -->
        <div class="item-header">
          <Avatar :src="item.avatar" :size="40" class="user-avatar" />
          <div class="header-info">
            <span class="user-name">{{ item.name }}</span>
            <span class="action-time">{{ item.time }}</span>
          </div>
        </div>

        <!-- 内容区域 -->
        <div class="item-content">
          <!-- 简单动作标题 -->
          <div class="action-title" v-if="item.action">{{ item.action }}</div>

          <!-- 平台拒绝等详情 -->
          <div class="detail-box" v-if="item.title || item.content">
            <div class="detail-row" v-if="item.title">【标题】{{ item.title }}</div>
            <div class="detail-row" v-if="item.content">【内容】{{ item.content }}</div>
          </div>

          <!-- 发起申请详情 -->
          <div class="fields-list" v-if="item.fields">
            <div v-for="(field, fIdx) in item.fields" :key="fIdx" class="field-item">
              <span class="field-label">{{ field.label }}:</span>
              <span class="field-value">{{ field.value }}</span>
            </div>
          </div>

          <!-- 图片凭证 -->
          <div class="image-gallery" v-if="item.images && item.images.length > 0">
            <div v-for="(img, iIdx) in item.images" :key="iIdx" class="image-wrapper">
              <Image :src="img" :width="64" :height="64" style="object-fit: cover; border-radius: 4px;" />
            </div>
          </div>
        </div>

        <!-- 分割线 (最后一条不显示) -->
        <div class="item-divider" v-if="index !== displayHistory.length - 1"></div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.negotiations-container {
  background: #fff;
  padding: 20px;
  min-height: 400px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
}

.negotiation-list {
  display: flex;
  flex-direction: column;
}

.negotiation-item {
  padding-bottom: 24px;
}

.item-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;

  .header-info {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 32px;
  }

  .user-name {
    font-weight: 600;
    font-size: 15px;
    color: #333;
  }

  .action-time {
    font-size: 13px;
    color: #999;
  }
}

.item-content {
  padding-left: 52px; // 40px avatar + 12px gap

  .action-title {
    font-weight: bold;
    font-size: 15px;
    color: #333;
    margin-bottom: 12px;
  }

  .detail-box {
    margin-bottom: 12px;
    .detail-row {
      font-size: 14px;
      line-height: 1.8;
      color: #333;
    }
  }

  .fields-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 16px;

    .field-item {
      display: flex;
      font-size: 14px;
      line-height: 1.6;

      .field-label {
        color: #333;
        width: 70px;
        flex-shrink: 0;
      }

      .field-value {
        color: #333;
      }
    }
  }

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

    .image-wrapper {
      border: 1px solid #f0f0f0;
      border-radius: 4px;
      overflow: hidden;
      background: #fafafa;
      width: 64px;
      height: 64px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
}

.item-divider {
  height: 1px;
  background-color: #f0f0f0;
  margin-top: 24px;
  margin-left: -20px;
  margin-right: -20px;
}
</style>
