<script setup lang="ts">
import type { SystemCwModel } from '@/model/care.model';
import { computed } from 'vue';
import { notification } from 'ant-design-vue';
const props = defineProps<{
  data: SystemCwModel['items'][number];
}>();
// 复制物流信息
function handleCopyTracking() {
  const trackingNumber = mockLogisticsData.value.trackingNumber;
  if (trackingNumber) {
    navigator.clipboard.writeText(trackingNumber).then(() => {
      notification.success({ message: '复制成功' });
    }).catch(() => {
      notification.error({ message: '复制失败' });
    });
  }
}
// 分账信息数据
const mockSplitAccountList = computed(() => {
  return (props.data as any).profitSharings;
});
// 物流信息数据
const mockLogisticsData = computed(() => {
  console.log('props.data===', props.data);
  return {
    trackingNumber: `${expressTypeText[props.data.expressType || '']} ${props.data.expressNo || ''}`,
    recipientName: props.data.liaison ? props.data.liaison + ', ' : '',
    recipientPhone: props.data.phone ? props.data.phone + ', ' : '',
    recipientAddress: (props.data.provinceName !== null ? props.data.provinceName + ' ' : '') + (props.data.cityName !== null ? props.data.cityName + ' ' : '') + (props.data.areaName !== null ? props.data.areaName + ' ' : '') + (props.data.detailAddress !== null ? props.data.detailAddress : '')
  };
});
const expressTypeText: Record<string, string> = {
  '0': '邮政速递',
  '1': '顺丰速运',
  '2': '京东快递',
  '3': '中通快递',
  '4': '圆通速递',
  '5': '申通快递',
  '6': '韵达快递',
  '7': '极兔速递',
};
// 包裹数据
const mockPackageItems = computed(() => {
  console.log('props.data===', props.data);

  return (props.data as any).sameExpress ?? []
});
// 商品状态映射 - 收货状态（实体商品使用）
const receiptStatusText: Record<string, string> = {
  '0': '待发货',
  '1': '已发货',
  '2': '已收货',
};
// 进度状态映射（线下服务和线上权益使用）
const progressText: Record<string, string> = {
  '0': '进行中',
  '1': '已完成',
  '2': '未开始',
};
// 商品类型映射
const sellTypeText: Record<string, string> = {
  '1': '实体商品',
  '2': '线下服务',
  '3': '线上权益',
};
</script>

<template>
  <div class="service-detail">
    <!-- 商品信息 -->
    <div class="info-section">
      <h3 class="info-title">商品信息</h3>
      <div class="info-content-wrapper">
        <div class="product-info">
          <div class="product-image">
            <!-- 商品图片 -->
            <a-image v-if="data?.conditioningProgramPhoto" :width="80" :height="80" style="border-radius: 5px;"
              :src="data?.conditioningProgramPhoto" class="product-img" />
          </div>
          <div class="product-details">
            <!-- 商品名称 -->
            <div class="product-name">{{ data?.conditioningProgramName }}</div>
            <!-- 一口价计价 -->
            <div class="product-spec" v-if="data?.conditioningProgramDetail?.pricingType === '0'">
              {{ data?.convertDose }} {{ data?.convertUnit || '次' }}
            </div>
            <!-- 按穴位/经络/部位计价 -->
            <div class="product-spec" v-if="data?.conditioningProgramDetail?.pricingType === '1'">
              1次
            </div>
            <!-- 商品价格 -->
            <div class="product-price" v-if="data?.unitPrice">
              ¥{{ data?.unitPrice }}
            </div>
          </div>
        </div>
        <div class="info-content">
          <div class="info-row">
            <div class="info-item">
              <span class="info-label">商品类型:</span>
              <span class="info-value">{{ sellTypeText[data.sellType] || '-' }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">方案类型:</span>
              <span class="info-value">{{ data.conditioningProgramType || '-' }}</span>
            </div>
          </div>
          <div class="info-row">
            <div class="info-item">
              <span class="info-label">数量:</span>
              <span class="info-value">{{ data?.totalMeasure || '-' }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">商品总价:</span>
              <span class="info-value">¥{{ data?.totalPrice || '-' }}</span>
            </div>
            <div class="info-item" v-if="data?.sellType === '1' ? data?.receiptStatus : data?.progress">
              <span class="info-label">商品状态:</span>
              <span class="info-value">
                {{ data?.sellType === '1'
                  ? (data?.receiptStatus ? receiptStatusText[data.receiptStatus] : '-')
                  : (data?.progress ? progressText[data.progress] : '-') }}
              </span>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 分账信息 -->
    <div class="info-section">
      <h3 class="info-title">分账信息</h3>
      <vxe-table :data="mockSplitAccountList" border>
        <vxe-column field="profitSharingTime" title="分账时间" align="center" />
        <vxe-column field="conditioningProgramSupplierName" title="供应商" align="center" />
        <vxe-column field="profitSharing" title="分账比例" align="center">
          <template #default="{ row }">
            {{ row.profitSharing || '-' }}%
          </template>
        </vxe-column>
        <vxe-column field="realAmount" title="分账金额" align="center">
          <template #default="{ row }">
            {{ row.realAmount ?? 0 }}元
          </template>
        </vxe-column>
      </vxe-table>
    </div>
    <!-- 物流信息和包裹内商品 实体商品才显示 sellType==='1'-->
    <div class="info-section" v-if="data.sellType === '1'">
      <!-- receiptType	收货方式 0-快递 1-线下取货 -->
      <!-- <h3 class="info-title">{{ data.receiptType === '0' ? '物流信息' : data.receiptType === '1' ? '线下取货' : '' }}</h3> -->
      <h3 class="info-title">物流信息</h3>
    
      <div class="info-content-wrapper" v-if="data.receiptType">
        <!-- 物流信息 -->
        <div class="logistics-content" v-if="data.receiptType === '0'">
          <template v-if="mockPackageItems.length > 0">
            <div class="logistics-tracking">
              <span class="tracking-number">{{ mockLogisticsData.trackingNumber }}</span>
              <a @click="handleCopyTracking" class="copy-link">复制</a>
            </div>
            <div class="logistics-recipient">
              <span class="receive-icon">收</span>
              <span class="recipient-info">
                {{ mockLogisticsData.recipientName
                  || '' }} {{ mockLogisticsData.recipientPhone || '' }} {{
                  mockLogisticsData.recipientAddress || '' }}
              </span>
            </div>
          </template>
          <template v-else>
            暂无
          </template>
        </div>
        <div class="logistics-content" v-if="data.receiptType === '1'">
          线下取货
        </div>
        <!-- 包裹内商品 -->
        <div class="package-items-wrapper" v-if="mockPackageItems.length > 0">
          <h3 class="info-title package-title" v-if="data.receiptType === '0'">
            <span class="star-icon">★</span>
            包裹内商品
          </h3>
          <div class="package-items">
          
            <div v-for="(item, index) in mockPackageItems" :key="index" class="package-item">
              <div class="package-item-image" v-if="item.conditioningProgramPhoto">
                <a-image v-if="item.conditioningProgramPhoto" :width="60" :height="60" style="border-radius: 4px;"
                  :src="item.conditioningProgramPhoto" class="item-img" />
              </div>
              <div class="package-item-placeholder" v-else>
                <text class="placeholder-icon">📦</text>
              </div>
              <div class="package-item-details">
                <div class="package-item-name">{{ item.conditioningProgramName }}</div>
                <div class="package-item-spec">{{ item.convertDose }} {{ item.convertUnit }}</div>
              </div>
              <div class="package-item-details">
                <div class="package-item-price">¥{{ item.unitPrice }}</div>
                <div class="package-item-quantity">x{{ item.totalMeasure }}</div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="info-content-wrapper" v-else>
        暂无
      </div>
    </div>

    <!-- 服务记录 线下服务才显示  1-实体商品 2-线下服务 3-线上权益-->
    <div class="info-section" v-if="data?.sellType === '2'">
      <h3 class="info-title">
        服务记录
        <span class="title-count">({{ data?.patientConditioningOfflines?.length || 0 }}/{{ data?.totalMeasure || 0
        }})</span>
      </h3>
      <vxe-table :data="data?.patientConditioningOfflines" border>
        <vxe-column type="seq" title="序号" width="60" align="center" />
        <vxe-column field="arrangeDate" title="服务日期" align="center" />
        <vxe-column field="arrangePeriod" title="服务时间段" align="center" />
        <vxe-column field="operateTime" title="服务状态" align="center">
          <template #default="{ row }">
            {{ row.operateTime ? '已核销' : '已预约' }}
          </template>
        </vxe-column>
        <vxe-column field="applyTime" title="预约时间" align="center" />
        <vxe-column field="pieBy" title="派单员" align="center" />
        <vxe-column field="pieTime" title="派单时间" align="center" />
        <vxe-column field="conditioningProgramSupplierName" title="服务机构" align="center" />
        <vxe-column field="operateTime" title="核销时间" align="center" />
        <vxe-column field="startTime" title="操作开始时间" align="center" />
        <vxe-column field="endTime" title="操作结束时间" align="center" />
        <vxe-column field="operateDuration" title="治疗时长" align="center">
          <template #default="{ row }">
            {{ row.operateDuration ? row.operateDuration + '分钟' : '' }}
          </template>
        </vxe-column>
        <vxe-column field="arrangeDuration" title="预定服务时长" align="center">
          <template #default="{ row }">
            {{ row.arrangeDuration ? row.arrangeDuration + '分钟' : '' }}
          </template>
        </vxe-column>
        <vxe-column field="operateBy" title="操作人" align="center" />
        <vxe-column field="feedback" title="治疗备注" align="center" />
      </vxe-table>
    </div>
  </div>
</template>

<style scoped lang="scss">
.service-detail {
  padding: 20px;
  color: black;
  background: #fff;

  .info-section {
    margin-top: 24px;
    margin-bottom: 24px;
    background: #fff;
  }

  .service-package-placeholder {
    width: 120rpx;
    height: 120rpx;
    border-radius: 8rpx;
    margin-right: 20rpx;
    background-color: #f5f5f5;
    border: 1px solid #e8e8e8;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
  }

  .placeholder-icon {
    font-size: 40rpx;
    opacity: 0.3;
  }

  .info-title {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 12px;
    color: #333;
  }

  .info-content-wrapper .info-title {
    margin-top: 20px;
    margin-bottom: 12px;
    padding-top: 20px;
    border-top: 1px solid #f0f0f0;
  }

  .info-content-wrapper .info-title:first-child {
    margin-top: 0;
    padding-top: 0;
    border-top: none;
  }

  .info-content-wrapper {
    border: 1px solid #e8e8e8;
    border-radius: 4px;
    padding: 10px 20px;
  }

  .product-info {
    display: flex;
    align-items: flex-start;
    // margin-bottom: 20px;
    gap: 16px;
    padding-bottom: 10px;
    // border-bottom: 1px solid #f0f0f0;
  }

  .product-image {
    flex-shrink: 0;
  }

  .product-img {
    border-radius: 4px;
    object-fit: cover;
  }

  .product-details {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 6px;
  }

  .product-name {
    font-size: 14px;
    font-weight: 500;
    color: #333;
    line-height: 20px;
  }

  .product-spec {
    font-size: 13px;
    color: #666;
    line-height: 18px;
  }

  .product-price {
    font-size: 14px;
    font-weight: 500;
    color: #333;
    line-height: 20px;
  }

  .info-content {
    display: flex;
    flex-direction: column;
    gap: 12px;
  }

  .info-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 24px;
    margin-top: 4px;
  }

  .info-item {
    display: flex;
    align-items: center;
    line-height: 24px;
  }

  .info-label {
    color: #666;
    margin-right: 8px;
    white-space: nowrap;
    // min-width: 80px;
  }

  .info-value {
    color: #333;
    font-weight: 400;
  }

  .title-badge {
    display: inline-block;
    width: 18px;
    height: 18px;
    line-height: 18px;
    text-align: center;
    background: #ffc107;
    color: #333;
    border-radius: 2px;
    margin-left: 6px;
    font-size: 12px;
    font-weight: 600;
    vertical-align: middle;
  }

  .title-count {
    margin-left: 8px;
    font-size: 14px;
    font-weight: 400;
    color: #666;
  }

  .logistics-content {
    display: flex;
    flex-direction: column;
    gap: 12px;
  }

  .logistics-tracking {
    display: flex;
    align-items: center;
    gap: 12px;
    line-height: 24px;
  }

  .tracking-number {
    font-weight: 600;
    color: #333;
  }

  .copy-link {
    color: #1890ff;
    cursor: pointer;
    text-decoration: none;
    font-weight: 400;
  }

  .copy-link:hover {
    text-decoration: underline;
  }

  .logistics-recipient {
    display: flex;
    align-items: flex-start;
    line-height: 24px;
  }

  .receive-icon {
    color: #ff4d4f;
    font-weight: 500;
    margin-right: 8px;
    flex-shrink: 0;
  }

  .recipient-info {
    color: #333;
    font-weight: 400;
    flex: 1;
  }

  .package-title {
    display: flex;
    align-items: center;
    gap: 6px;
  }

  .star-icon {
    color: #ffc107;
    font-size: 20px;
    margin-right: 0;
  }

  .package-items {
    display: flex;
    flex-direction: column;
    gap: 16px;
    width: 25%;
  }

  .package-item {
    display: flex;
    align-items: flex-start;
    gap: 12px;
  }

  .package-item-image {
    flex-shrink: 0;
  }

  .package-item-placeholder {
    flex-shrink: 0;
    width: 60px;
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #f5f5f5;
    border-radius: 4px;
  }

  .item-img {
    border-radius: 4px;
    object-fit: cover;
  }

  .package-item-details {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 6px;
    min-width: 0;
  }

  .package-item-name {
    font-size: 14px;
    font-weight: 500;
    color: #333;
    line-height: 20px;
  }

  .package-item-spec {
    font-size: 13px;
    color: #666;
    line-height: 18px;
  }

  .package-item-price {
    font-size: 14px;
    font-weight: 500;
    color: #333;
    line-height: 20px;
    text-align: end;
  }

  .package-item-quantity {
    font-size: 14px;
    color: #666;
    // margin-left: auto;
    flex-shrink: 0;
    padding-top: 2px;
    text-align: end;
  }

  .detail-item {
    display: flex;
    margin-bottom: 20px;

    .label {
      //   width: 80px;
      margin-right: 10px;
    }

    .content {
      //   flex: 1;

      .service-image {
        max-width: 200px;
        height: auto;
      }
    }
  }
}

.derivation-label {
  font-weight: 500;
  color: #222;
  margin-right: 8px;
  white-space: nowrap;
  font-size: 13px;
}

.derivation-item {
  display: flex;
  align-items: center;
  min-width: 200px;
  max-width: 300px;
  padding: 6px 12px;
  background: #f5f5f5;
  border-radius: 4px;
  border: 1px solid #e8e8e8;
}

.derivation-content {
  color: #555;
  font-size: 13px;
  flex: 1;
  word-break: break-all;
}

.derivation-container {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  align-items: flex-start;
}

.video-preview-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 120px;
  height: 100%;
  padding: 10px;
  background: #fafafa;
  border-radius: 8px;
}

.video-thumbnail {
  position: relative;
  width: 100%;
  height: 100px;
  border-radius: 6px;
  overflow: hidden;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 8px;
}

.video-thumbnail .video-preview {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.video-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.7);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.video-preview-container:hover .video-overlay {
  opacity: 1;
}

.video-overlay .ant-btn {
  background: rgba(255, 255, 255, 0.95);
  border: none;
  color: #333;
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 4px;
  transition: all 0.3s ease;
  min-width: 40px;
  height: 28px;
  line-height: 1.2;
  display: flex;
  align-items: center;
  justify-content: center;
  white-space: nowrap;
}

.video-overlay .ant-btn:hover {
  background: rgba(255, 255, 255, 1);
  transform: scale(1.05);
}

.video-overlay .ant-btn-danger {
  background: rgba(255, 77, 79, 0.95);
  color: #fff;
}

.video-overlay .ant-btn-danger:hover {
  background: rgba(255, 77, 79, 1);
}

.video-info {
  text-align: center;
  font-size: 11px;
  color: #666;
}

.video-name {
  font-weight: 500;
  color: #333;
  margin-bottom: 2px;
}

.video-size {
  color: #999;
  font-size: 10px;
}

.package-items-wrapper {
  border: 1px solid #e8e8e8;
  padding: 10px;
  margin-top: 10px;
}
</style>
