<template>
  <view class="page">
    <!-- 搜索和筛选区域 -->
    <view class="search-filter-section">
      <view class="search-box">
        <uni-icons color="#7F7F7F" size="20" type="search"></uni-icons>
        <input v-model="searchKeyword" class="search-input" placeholder="搜索" @input="onSearch" />
      </view>
      <view class="filter-btn" @click="showFilter">
        <text class="filter-text">筛选</text>
        <text class="filter-arrow">▼</text>
      </view>
    </view>

    <!-- 用户列表 -->
    <view class="user-list">
      <view v-for="(user, index) in filteredUsers" :key="user.id" class="user-card">
        <!-- 用户基本信息 -->
        <view class="user-info">
          <view class="avatar-placeholder"></view>
          <view class="user-details">
            <text class="username">{{ user.username }}</text>
            <view class="user-stats">
              <text class="amount">¥ {{ user.totalAmount }}</text>
              <text class="device-count">{{ user.deviceCount }}台</text>
            </view>
          </view>
        </view>

        <!-- 设备信息表格 -->
        <view class="device-table">
          <view class="table-header">
            <text class="header-cell">类型</text>
            <text class="header-cell">金额</text>
            <text class="header-cell">租赁</text>
            <text class="header-cell">周期</text>
            <text class="header-cell">到期</text>
          </view>

          <view
            v-for="(device, deviceIndex) in user.devices.slice(
              0,
              user.isExpanded ? user.devices.length : 1
            )"
            :key="deviceIndex"
            class="table-row"
          >
            <text class="table-cell">{{ device.type }}</text>
            <text class="table-cell">¥ {{ device.amount }}</text>
            <text class="table-cell">{{ device.rentDate }}</text>
            <text class="table-cell">{{ device.period }}</text>
            <text class="table-cell">{{ device.expiryDate }}</text>
          </view>
        </view>

        <!-- 展开/收起按钮 -->
        <view class="expand-btn" @click="toggleExpand(index)">
          <text class="expand-text">
            {{ user.isExpanded ? '点击收起' : '点击展开' }}
          </text>
          <text class="expand-arrow">
            {{ user.isExpanded ? '▲' : '▼' }}
          </text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { commonEnum } from '@/enum/commonEnum.js'
import UniIcons from '../../uni_modules/uni-icons/components/uni-icons/uni-icons.vue'

export default {
  components: { UniIcons },
  data() {
    return {
      searchKeyword: '',
      users: [
        {
          id: 1,
          username: '用户名',
          totalAmount: '5000',
          deviceCount: 2,
          isExpanded: true,
          devices: [
            {
              type: '密密麻麻',
              amount: '1000',
              rentDate: '2025.07.15 12:56:08',
              period: '1年',
              expiryDate: '2025.07.15 12:56:08',
            },
            {
              type: '密密麻麻',
              amount: '1000',
              rentDate: '2025.07.15 12:56:08',
              period: '1年',
              expiryDate: '2025.07.15 12:56:08',
            },
          ],
        },
        {
          id: 2,
          username: '用户名',
          totalAmount: '5000',
          deviceCount: 5,
          isExpanded: false,
          devices: [
            {
              type: '密密麻麻',
              amount: '1000',
              rentDate: '2025.07.15 12:56:08',
              period: '1年',
              expiryDate: '2025.07.15 12:56:08',
            },
            {
              type: '密密麻麻',
              amount: '1000',
              rentDate: '2025.07.15 12:56:08',
              period: '1年',
              expiryDate: '2025.07.15 12:56:08',
            },
            {
              type: '密密麻麻',
              amount: '1000',
              rentDate: '2025.07.15 12:56:08',
              period: '1年',
              expiryDate: '2025.07.15 12:56:08',
            },
            {
              type: '密密麻麻',
              amount: '1000',
              rentDate: '2025.07.15 12:56:08',
              period: '1年',
              expiryDate: '2025.07.15 12:56:08',
            },
            {
              type: '密密麻麻',
              amount: '1000',
              rentDate: '2025.07.15 12:56:08',
              period: '1年',
              expiryDate: '2025.07.15 12:56:08',
            },
          ],
        },
      ],
    }
  },
  computed: {
    filteredUsers() {
      if (!this.searchKeyword) {
        return this.users
      }
      return this.users.filter(user =>
        user.username.toLowerCase().includes(this.searchKeyword.toLowerCase())
      )
    },
  },
  methods: {
    onSearch() {
      // 搜索逻辑已在computed中处理
    },
    showFilter() {
      // 显示筛选选项
      uni.showToast({
        title: '筛选功能开发中',
        icon: 'none',
      })
    },
    toggleExpand(index) {
      this.users[index].isExpanded = !this.users[index].isExpanded
    },
  },
}
</script>

<style lang="scss" scoped>
.page {
  min-height: 90vh;
  background: #f7f7f7;

  padding-bottom: 40rpx;
}

.search-filter-section {
  display: flex;
  align-items: center;
  padding: 20rpx 20rpx 20rpx 20rpx;
  gap: 20rpx;
  background: white;
  margin-bottom: 20rpx;
}

.search-box {
  flex: 1;
  display: flex;
  align-items: center;
  background: #f3f5f6;
  border-radius: 999px;
  padding: 20rpx;

  .search-input {
    padding: 0 20rpx;
    flex: 1;
    font-size: 28rpx;
  }
}

.filter-btn {
  display: flex;
  align-items: center;
  background: white;
  border-radius: 10rpx;
  padding: 20rpx;

  .filter-text {
    font-size: 28rpx;
    margin-right: 10rpx;
  }

  .filter-arrow {
    font-size: 24rpx;
  }
}

.user-list {
  padding: 0 20rpx;
}

.user-card {
  background: white;
  border-radius: 20rpx;
  margin-bottom: 20rpx;
  padding: 30rpx;
}

.user-info {
  display: flex;
  align-items: center;
  margin-bottom: 30rpx;
}

.avatar-placeholder {
  width: 80rpx;
  height: 80rpx;
  background: #e0e0e0;
  border-radius: 50%;
  margin-right: 20rpx;
}

.user-details {
  display: flex;
  justify-content: space-between;
  flex: 1;
}

.username {
  font-size: 32rpx;
  font-weight: 500;
  color: #333;
  display: block;
  margin-bottom: 10rpx;
}

.user-stats {
  display: flex;
  align-items: center;
  gap: 20rpx;
}

.amount {
  font-size: 28rpx;

  font-weight: 500;
  padding-right: 78rpx;
}

.device-count {
  font-size: 24rpx;
  color: #666;
}

.device-table {
  margin-bottom: 20rpx;
}

.table-header {
  display: flex;
  background: #f8f8f8;
  border-radius: 10rpx;
  padding: 20rpx 0;
  margin-bottom: 10rpx;
}

.header-cell {
  flex: 1;
  text-align: center;
  font-size: 24rpx;
  color: #666;
  font-weight: 500;
}

.table-row {
  display: flex;
  padding: 15rpx 0;
  border-bottom: 1rpx solid #f0f0f0;
}

.table-cell {
  flex: 1;
  text-align: center;
  font-size: 24rpx;
  color: #333;
}

.expand-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20rpx 0;
  color: #007aff;
  font-size: 28rpx;

  .expand-text {
    margin-right: 10rpx;
  }

  .expand-arrow {
    font-size: 24rpx;
  }
}

view {
  border: red solid 1px;
}
</style>
