<template>
  <div class="table-container">
    <table class="data-table">
      <thead>
        <tr>
          <th v-for="column in columns" :key="column.props">{{ column.title }}</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(row, index) in data" :key="index"
          :class="{ 'odd-row': (index + 1) % 2 !== 0, 'even-row': (index + 1) % 2 === 0 }">
          
          <td v-for="column in columns" :key="`${column.props}-${index}`">
            <!-- 如果列配置了slot属性，则使用插槽 -->
            <slot v-if="column.slot" :name="`column-${column.props}`" :row="row" :index="index">
              <!-- 默认插槽内容 -->
              {{ row[column.props] }}
            </slot>
            <!-- 如果没有配置slot属性，则显示普通文本 -->
            <template v-else>
              {{ row[column.props] || '-' }}
            </template>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
export default {
  name: 'TableList',
  props: {
    columns: {
      type: Array,
      default: () => [],
      validator: (value) => {
        return value.every(column => {
          return column.props && column.title;
        });
      }
    },
    data: {
      type: Array,
      default: () => []
    }
  },
  created() {
    console.log(this.columns,this.data);
  }
};
</script>

<style lang="scss" scoped>
.table-container {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  
  /* 强制显示滚动条 */
  scrollbar-width: thin; /* Firefox */
  scrollbar-color: #525861 #f1f1f1; /* Firefox */
  
  /* 自定义滚动条样式 - 更明显的滚动条 */
  &::-webkit-scrollbar {
    height: 12px; /* 增加滚动条高度 */
    width: 12px;
  }
  
  &::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 6px;
    box-shadow: inset 0 0 5px rgba(0,0,0,0.1);
  }
  
  &::-webkit-scrollbar-thumb {
    background: #525861; /* 使用主题色 */
    border-radius: 6px;
    border: 2px solid #f1f1f1;
  }
  
  &::-webkit-scrollbar-thumb:hover {
    background: #525861; /* 悬停时加深颜色 */
  }
  
  &::-webkit-scrollbar-corner {
    background: #f1f1f1;
  }
}

.data-table {
  width: auto;
  min-width: 100%;
  border-collapse: collapse;
  border: 2rpx solid #ccc;
  border-radius: 16rpx;
  table-layout: auto;

  th,
  td {
    padding: 12rpx;
    height: 48rpx;
    text-align: left;
    white-space: nowrap;
    overflow: visible;
    text-overflow: clip;
    // border-bottom: 1rpx solid #ccc;
  }

  th {
    background-color: #f5f5f5;
    font-weight: bold;
    color: #666;
    position: sticky;
    top: 0;
    z-index: 1;
    
    /* 确保表头内容完全展示 */
    white-space: nowrap;
    overflow: visible;
    text-overflow: clip;
  }

  .odd-row {
    background-color: #fff !important;
  }

  .even-row {
    background-color: #f5f5f5 !important;
  }

  tr:hover {
    background-color: #e0e0e0;
  }
}
</style>