<template>
  <el-dialog
    title="积分详情"
    :visible.sync="visible"
    width="500px"
    :close-on-click-modal="false"
    @close="handleClose"
  >
    <div class="detail-content">
      <!-- 基础信息 -->
      <div class="detail-row">
        <span class="label">变化原因：</span>
        <span class="value">{{ detailData.changeReason || '-' }}</span>
      </div>
      
      <div class="detail-row">
        <span class="label">变化时间：</span>
        <span class="value">{{ detailData.changeTime || '-' }}</span>
      </div>
      
      <div class="detail-row">
        <span class="label">积分变动：</span>
        <span class="value points-value" :style="{ color: getPointsColor(detailData.pointsChange) }">
          {{ detailData.pointsChange > 0 ? '+' : '' }}{{ detailData.pointsChange || 0 }}
        </span>
      </div>

      <!-- 有关联订单时显示的字段 -->
      <template v-if="hasRelatedOrder">
        <div class="detail-row">
          <span class="label">订单编号：</span>
          <span class="value">{{ detailData.relatedOrder || '-' }}</span>
        </div>
        
        <div class="detail-row">
          <span class="label">订单总金额：</span>
          <span class="value">¥{{ detailData.orderAmount || '0.00' }}</span>
        </div>
        
        <div class="detail-row">
          <span class="label">订单时间：</span>
          <span class="value">{{ detailData.orderTime || '-' }}</span>
        </div>
        
        <div class="detail-row">
          <span class="label">订单归属摊位：</span>
          <span class="value">{{ detailData.shopName || '-' }}</span>
        </div>
      </template>

      <!-- 无关联订单且是使用积分时显示的字段 -->
      <template v-if="!hasRelatedOrder && isPointsUsage">
        <div class="detail-row">
          <span class="label">兑换商品：</span>
          <span class="value">{{ detailData.exchangeProduct || '-' }}</span>
        </div>
        
        <div class="detail-row">
          <span class="label">兑换数量：</span>
          <span class="value">{{ detailData.exchangeQuantity || '-' }}</span>
        </div>
        
        <div class="description-section">
          <div class="description-text">
            {{ detailData.usageDescription || '使用积分兑换商品，兑换成功后积分将被扣除且不可退还。' }}
          </div>
        </div>
      </template>

      <!-- 无关联订单且是获得积分时显示的描述 -->
      <template v-if="!hasRelatedOrder && !isPointsUsage && detailData.description">
        <div class="description-section">
          <div class="description-text">
            {{ detailData.description }}
          </div>
        </div>
      </template>
    </div>

    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">关闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: "PointsDetail",
  data() {
    return {
      visible: false,
      detailData: {}
    };
  },
  computed: {
    hasRelatedOrder() {
      return this.detailData.relatedOrder && this.detailData.relatedOrder !== '-';
    },
    isPointsUsage() {
      return this.detailData.pointsChange < 0;
    }
  },
  methods: {
    show(data) {
      this.detailData = data;
      this.visible = true;
    },
    handleClose() {
      this.visible = false;
      this.detailData = {};
    },
    getPointsColor(value) {
      return value > 0 ? '#67C23A' : '#F56C6C';
    }
  }
};
</script>

<style lang="scss" scoped>
.detail-content {
  padding: 10px 0;
}

.detail-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #f0f0f0;
  
  &:last-child {
    border-bottom: none;
  }
}

.label {
  color: #606266;
  font-weight: 500;
  min-width: 100px;
}

.value {
  color: #303133;
  flex: 1;
  text-align: right;
}

.points-value {
  font-weight: bold;
  font-size: 16px;
}

.description-section {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px dashed #e4e7ed;
}

.description-text {
  color: #909399;
  font-size: 14px;
  line-height: 1.5;
  background: #f5f7fa;
  padding: 15px;
  border-radius: 4px;
  border-left: 4px solid #409eff;
}
</style>
