<template>
  <div class="NCC-common-layout">
    <div class="NCC-common-layout-center">
      <!-- 筛选条件 -->
      <!-- <el-row class="NCC-common-search-box" :gutter="16">
        <el-form @submit.native.prevent>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()">查询</el-button>
              <el-button icon="el-icon-refresh-right" @click="reset()">重置</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row> -->

      <!-- 数据表格 -->
      <div class="NCC-common-layout-main NCC-flex-main table-wrapper">
        <el-table 
          v-loading="listLoading" 
          :data="list" 
          border 
          stripe
          :max-height="tableHeight">
          <!-- 展开行：显示账单列表 -->
          <el-table-column type="expand" width="50">
            <template slot-scope="props">
              <div class="expand-container" v-if="props.row.billingList && props.row.billingList.length > 0">
                <div class="expand-title">
                  <i class="el-icon-document"></i>
                  账单明细
                </div>
                <div v-for="(billing, billingIndex) in props.row.billingList" :key="billing.billingId || billingIndex" class="billing-item">
                  <!-- 账单信息 -->
                  <div class="billing-header">
                    <div class="billing-info">
                      <span class="billing-label">
                        <i class="el-icon-document item-icon"></i>
                        账单ID：
                      </span>
                      <span class="billing-value">{{ billing.billingId || '无' }}</span>
                    </div>
                    <div class="billing-info">
                      <span class="billing-label">
                        <i class="el-icon-date item-icon"></i>
                        账单日期：
                      </span>
                      <span class="billing-value">{{ formatTime(billing.billingDate) }}</span>
                    </div>
                    <div class="billing-info">
                      <span class="billing-label">
                        <i class="el-icon-shop item-icon"></i>
                        门店名称：
                      </span>
                      <span class="billing-value">{{ billing.storeName || '无' }}</span>
                    </div>
                    <div class="billing-info">
                      <span class="billing-label">
                        <i class="el-icon-money item-icon"></i>
                        实际业绩：
                      </span>
                      <span class="billing-value amount-value">¥{{ formatMoney(billing.actualPerformance) }}</span>
                    </div>
                  </div>
                  
                  <!-- 项目列表 -->
                  <div v-if="billing.itemList && billing.itemList.length > 0" class="item-list-container">
                    <div class="item-list-title">
                      <i class="el-icon-goods"></i>
                      项目明细（{{ billing.itemList.length }}项）
                    </div>
                    <el-table 
                      :data="billing.itemList" 
                      border 
                      size="small" 
                      :header-cell-style="{ background: '#f0f2f5', color: '#606266' }"
                      style="margin-top: 8px;">
                      <el-table-column prop="itemName" label="项目名称"  min-width="150">
                        <template slot-scope="itemScope">
                          <div class="item-name">
                            <i class="el-icon-goods item-icon"></i>
                            <span>{{ itemScope.row.itemName || '无' }}</span>
                          </div>
                        </template>
                      </el-table-column>
                      <el-table-column prop="itemCode" label="项目编号"  width="120">
                        <template slot-scope="itemScope">
                          <span>{{ itemScope.row.itemCode || '无' }}</span>
                        </template>
                      </el-table-column>
                      <el-table-column prop="itemPrice" label="项目价格"  width="120">
                        <template slot-scope="itemScope">
                          <span class="amount-value">¥{{ formatMoney(itemScope.row.itemPrice) }}</span>
                        </template>
                      </el-table-column>
                      <el-table-column prop="sourceType" label="来源类型"  width="100">
                        <template slot-scope="itemScope">
                          <el-tag 
                            :type="getSourceTypeTagType(itemScope.row.sourceType)" 
                            size="small">
                            {{ itemScope.row.sourceType || '无' }}
                          </el-tag>
                        </template>
                      </el-table-column>
                      <el-table-column prop="projectNumber" label="项目次数"  width="100">
                        <template slot-scope="itemScope">
                          <span>{{ itemScope.row.projectNumber || 0 }}</span>
                        </template>
                      </el-table-column>
                      <el-table-column prop="totalPrice" label="总价"  width="120">
                        <template slot-scope="itemScope">
                          <span class="amount-value">¥{{ formatMoney(itemScope.row.totalPrice) }}</span>
                        </template>
                      </el-table-column>
                      <el-table-column prop="actualPrice" label="实付金额"  width="120">
                        <template slot-scope="itemScope">
                          <span class="amount-paid">¥{{ formatMoney(itemScope.row.actualPrice) }}</span>
                        </template>
                      </el-table-column>
                      <el-table-column prop="performanceTime" label="业绩时间"  width="160">
                        <template slot-scope="itemScope">
                          <span>{{ formatTime(itemScope.row.performanceTime) }}</span>
                        </template>
                      </el-table-column>
                    </el-table>
                  </div>
                  <div v-else class="item-list-empty">
                    暂无项目明细
                  </div>
                </div>
              </div>
              <div v-else class="expand-empty">
                暂无账单明细
              </div>
            </template>
          </el-table-column>

          <el-table-column prop="memberId" label="会员ID"  width="180">
            <template slot-scope="scope">
              <i class="el-icon-user-solid" style="margin-right: 4px; color: #409EFF;"></i>
              <span>{{ scope.row.memberId || '无' }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="memberName" label="会员姓名"  min-width="120">
            <template slot-scope="scope">
              <i class="el-icon-user" style="margin-right: 4px; color: #409EFF;"></i>
              <span>{{ scope.row.memberName || '无' }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="phone" label="手机号"  min-width="130">
            <template slot-scope="scope">
              <i class="el-icon-phone" style="margin-right: 4px; color: #67C23A;"></i>
              <span>{{ scope.row.phone || '无' }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="storeName" label="门店名称"  min-width="150">
            <template slot-scope="scope">
              <i class="el-icon-shop" style="margin-right: 4px; color: #909399;"></i>
              <span>{{ scope.row.storeName || '无' }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="billingList" label="账单数量"  width="100">
            <template slot-scope="scope">
              <span>{{ (scope.row.billingList && scope.row.billingList.length) || 0 }}</span>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页组件 -->
        <pagination 
          v-show="total > 0" 
          :total="total" 
          :page.sync="listQuery.pageIndex"
          :limit.sync="listQuery.pageSize" 
          @pagination="handlePagination" 
        />
      </div>
    </div>
  </div>
</template>

<script>
import request from '@/utils/request'
import Pagination from '@/components/Pagination'

export default {
  name: 'GoddessCardMembers',
  components: {
    Pagination
  },
  data() {
    return {
      list: [],
      listLoading: false,
      total: 0,
      listQuery: {
        pageIndex: 1,
        pageSize: 10
      }
    }
  },
  computed: {
    // 计算表格高度
    tableHeight() {
      // 减去头部、分页等高度，大约 200px
      return window.innerHeight - 200
    }
  },
  created() {
    this.search()
  },
  methods: {
    // 查询数据
    search() {
      this.listLoading = true

      const params = {
        pageIndex: this.listQuery.pageIndex,
        pageSize: this.listQuery.pageSize
      }

      request({
        url: '/api/Extend/lqstatistics/GetGoddessCardMembers',
        method: 'POST',
        data: params
      }).then(res => {
        if (res.data && res.data.list) {
          this.list = res.data.list
          this.total = (res.data.pagination && res.data.pagination.total) || res.data.list.length || 0
        } else {
          this.list = []
          this.total = 0
        }
        this.listLoading = false
      }).catch(err => {
        console.error('查询失败:', err)
        this.$message({
          type: 'error',
          message: '查询失败，请重试',
          duration: 1500
        })
        this.listLoading = false
        this.list = []
        this.total = 0
      })
    },

    // 处理分页变化
    handlePagination({ page, limit }) {
      this.listQuery.pageIndex = page
      this.listQuery.pageSize = limit
      this.search()
    },

    // 重置查询条件
    reset() {
      this.listQuery.pageIndex = 1
      this.listQuery.pageSize = 10
      this.list = []
      this.total = 0
      this.search()
    },

    // 格式化金额
    formatMoney(amount) {
      if (amount === null || amount === undefined || amount === '') {
        return '0.00'
      }
      const num = parseFloat(amount)
      if (isNaN(num)) {
        return '0.00'
      }
      return num.toFixed(2)
    },

    // 格式化时间
    formatTime(timestamp) {
      if (!timestamp) {
        return '无'
      }
      const date = new Date(timestamp)
      if (isNaN(date.getTime())) {
        return '无'
      }
      const year = date.getFullYear()
      const month = String(date.getMonth() + 1).padStart(2, '0')
      const day = String(date.getDate()).padStart(2, '0')
      const hours = String(date.getHours()).padStart(2, '0')
      const minutes = String(date.getMinutes()).padStart(2, '0')
      const seconds = String(date.getSeconds()).padStart(2, '0')
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
    },

    // 获取来源类型标签类型
    getSourceTypeTagType(sourceType) {
      if (!sourceType) {
        return 'info'
      }
      if (sourceType === '购买') {
        return 'success'
      } else if (sourceType === '赠送') {
        return 'warning'
      } else if (sourceType === '体验') {
        return 'info'
      }
      return 'info'
    }
  }
}
</script>

<style lang="scss" scoped>
.table-wrapper {
  height: 100%;
  
  ::v-deep .el-table__body-wrapper {
    overflow-y: scroll !important;
  }
}

.expand-container {
  padding: 16px;
  background: #fafafa;
  
  .expand-title {
    font-size: 14px;
    font-weight: 500;
    color: #303133;
    margin-bottom: 16px;
    display: flex;
    align-items: center;
    
    i {
      margin-right: 6px;
      color: #409EFF;
    }
  }
  
  .billing-item {
    background: #fff;
    border: 1px solid #e4e7ed;
    border-radius: 4px;
    padding: 12px;
    margin-bottom: 12px;
    
    &:last-child {
      margin-bottom: 0;
    }
    
    .billing-header {
      display: flex;
      flex-wrap: wrap;
      gap: 16px;
      margin-bottom: 12px;
      padding-bottom: 12px;
      border-bottom: 1px solid #e4e7ed;
      
      .billing-info {
        display: flex;
        align-items: center;
        
        .billing-label {
          font-size: 13px;
          color: #606266;
          margin-right: 4px;
          
          .item-icon {
            margin-right: 4px;
            color: #909399;
          }
        }
        
        .billing-value {
          font-size: 13px;
          color: #303133;
          font-weight: 500;
        }
      }
    }
    
    .item-list-container {
      .item-list-title {
        font-size: 13px;
        font-weight: 500;
        color: #606266;
        margin-bottom: 8px;
        display: flex;
        align-items: center;
        
        i {
          margin-right: 6px;
          color: #67C23A;
        }
      }
    }
    
    .item-list-empty {
      padding: 12px;
      text-align: center;
      color: #909399;
      background: #f5f7fa;
      border-radius: 4px;
    }
  }
}

.expand-empty {
  padding: 16px;
  text-align: center;
  color: #909399;
  background: #fafafa;
}

.item-name {
  display: flex;
  align-items: center;
  
  .item-icon {
    margin-right: 6px;
    color: #409EFF;
  }
}

.amount-value {
  color: #409EFF;
  font-weight: 500;
}

.amount-paid {
  color: #67C23A;
  font-weight: 500;
}
</style>

