<template>
  <view class="user-detail-container">
    <!-- 顶部导航栏 -->
    <view class="nav-bar">
      <view class="back" @tap="goBack">
        <uni-icons type="left" size="20"></uni-icons>
      </view>
      <text class="title">用户管理详情</text>
    </view>
    <view class="main-content">
      <!-- 个人信息卡片 -->
      <view class="info-card info-card-personal">
        <view class="card-title-row">
          <view class="card-title">个人信息</view>
          <text v-if="user.role==='推广官'" class="role-tag">推广官</text>
        </view>
        <view v-if="user.blocked" class="blocked-tag">已拉黑</view>
        <view class="info-row">
          <text class="label">姓名</text>
          <text class="value">{{ user.name }}</text>
        </view>
        <view class="divider"></view>
        <view class="info-row">
          <text class="label">电话</text>
          <text class="value">{{ user.phone }}</text>
        </view>
        <view class="divider"></view>
        <view class="info-row avatar-row">
          <text class="label">头像</text>
          <image class="avatar" :src="user.avatar" mode="aspectFill" />
        </view>
      </view>
      <!-- 平台信息卡片 -->
      <view class="info-card">
        <view class="card-title">平台信息</view>
        <view class="info-row">
          <text class="label">回收总次数</text>
          <text class="value">{{ user.recycleCount }}</text>
        </view>
        <view class="divider"></view>
        <view class="info-row">
          <text class="label">总件数</text>
          <text class="value">{{ user.totalItems }}</text>
        </view>
        <view class="divider"></view>
        <view class="info-row">
          <text class="label">回收总额</text>
          <text class="value">{{ user.totalAmount }}元</text>
        </view>
        <view class="divider"></view>
        <view class="info-row address-row">
          <text class="label">用户下单地址</text>
          <text class="value address">{{ user.address }}</text>
        </view>
        <view class="divider"></view>
        <view class="info-row order-row">
          <text class="label">历史订单</text>
          <text class="value link" @tap="viewOrders">点击查看</text>
        </view>
      </view>
    </view>
    <!-- 底部操作栏 -->
    <view class="bottom-bar">
      <button v-if="user.blocked" class="action-btn danger" @tap="blockUser">解除拉黑</button>
      <template v-else>
        <button class="action-btn danger" @tap="blockUser">拉黑</button>
        <button v-if="user.role!=='推广官'" class="action-btn primary" @tap="upgradeUser">升级推广官</button>
      </template>
    </view>
  </view>
</template>

<script>
import pullRefreshMixin from '../mixins/pullRefreshMixin.js'
export default {
  mixins: [pullRefreshMixin],
  data() {
    return {
      user: {
        name: '赵莫艳',
        phone: '15888977617',
        avatar: 'https://img1.baidu.com/it/u=1234567890,1234567890&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=400',
        recycleCount: 2412,
        totalItems: 32,
        totalAmount: '8273.99',
        address: '海南省海口市秀英区秀英街道5单元183室',
      },
      statusBarHeight: 0,
    }
  },
  computed: {
    navbarStyle() {
      return `padding-top: ${this.statusBarHeight}px;`;
    }
  },
  onLoad(options) {
    // 这里可以通过 options 传递用户id并请求详情
    uni.getSystemInfo({
      success: (res) => {
        this.statusBarHeight = res.statusBarHeight || 20;
      }
    });
    // 获取上个页面传递的用户信息
    const eventChannel = this.getOpenerEventChannel && this.getOpenerEventChannel();
    if (eventChannel) {
      eventChannel.on('userDetail', (user) => {
        this.user = Object.assign({}, this.user, user);
        // 获取平台统计信息
        if (user.id) {
          this.$api && this.$api('getUserOrderNum', { userId: user.id }, res => {
            if (res && res.code === 200 && res.result) {
              this.user.recycleCount = res.result.unit_num;
              this.user.totalItems = res.result.sum;
              this.user.totalAmount = res.result.order_money;
            }
          });
          // 获取默认地址
          this.$api && this.$api('getAddressList', { userId: user.id }, res => {
            if (res && res.code === 200 && Array.isArray(res.result)) {
              const defaultAddr = res.result.find(addr => addr.defaultFlag === 'Y');
              if (defaultAddr) {
                this.user.address = defaultAddr.address;
              }
            }
          });
        }
      });
    }
  },
  methods: {
    goBack() {
      uni.navigateBack();
    },
    blockUser() {
      if (!this.user.id) return;
      this.$api('blackUser', { userId: this.user.id }, res => {
        if (res && res.code === 200) {
			this.user.blocked = !this.user.blocked;
          uni.showToast({ title: this.user.blocked ? '已拉黑' : '已解除', icon: 'success' });
        } else {
          uni.showToast({ title: res.msg || '拉黑失败', icon: 'none' });
        }
      })
    },
    upgradeUser() {
      if (!this.user.id) return;
      this.$api('upgrade', { userId: this.user.id }, res => {
        if (res && res.code === 200) {
          uni.showToast({ title: '已升级为推广官', icon: 'success' });
          this.user.role = '推广官';
        } else {
          uni.showToast({ title: res.msg || '升级失败', icon: 'none' });
        }
      })
    },
    
    viewOrders() {
      uni.navigateTo({
        url: '/pages/manager/order?historyOrder=true&userId=' + this.user.id
      });
    },
    unblockUser() {
      uni.showToast({ title: '已解除拉黑', icon: 'none' });
      this.user.blocked = false;
    },
    refreshData() {
      // TODO: 实现用户详情刷新逻辑，如重新请求接口
    },
    async onRefresh() {
      await this.refreshData && this.refreshData()
    }
  },
  onPullDownRefresh() {
    this.refreshData && this.refreshData()
    uni.stopPullDownRefresh()
  }
}
</script>

<style lang="scss" scoped>
.user-detail-container {
  min-height: 100vh;
  background: #f7f7f7;
  padding-bottom: 160rpx;
}

// 新增主流小程序风格导航栏
.nav-bar {
  display: flex;
  align-items: center;
  height: calc(150rpx + var(--status-bar-height));
  padding: 0 32rpx;
  padding-top: var(--status-bar-height);
  background: #fff;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 999;
  box-sizing: border-box;
  box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.03);
  .back {
    padding: 20rpx;
    margin-left: -20rpx;
  }
  .title {
    flex: 1;
    text-align: center;
    font-size: 34rpx;
    font-weight: 500;
    color: #222;
  }
}

.main-content {
  margin-top: calc(150rpx + var(--status-bar-height));
  margin-bottom: 40rpx;
}
.info-card {
  background: #fff;
  border-radius: 40rpx;
  margin: 0 32rpx 32rpx 32rpx;
  padding: 40rpx 36rpx 36rpx 36rpx;
  box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.03);
  .card-title {
    font-size: 36rpx;
    font-weight: bold;
    color: #222;
    margin-bottom: 32rpx;
  }
  .info-row {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    min-height: 60rpx;
    margin-bottom: 0;
    padding: 18rpx 0 10rpx 0;
    .label {
      font-size: 26rpx;
      color: #b3b3b3;
      font-weight: 400;
      margin-bottom: 6rpx;
    }
    .value {
      font-size: 32rpx;
      color: #222;
      font-weight: 500;
      flex: 1;
      word-break: break-all;
      margin-bottom: 0;
    }
    .address {
      font-size: 30rpx;
      color: #222;
      font-weight: 400;
      margin-top: 0;
      line-height: 1.5;
    }
    .link {
      color: #ff8917;
      font-size: 32rpx;
      font-weight: 500;
      text-decoration: underline;
      margin-left: 0;
    }
  }
  .divider {
    height: 2rpx;
    background: #f3f3f3;
    margin: 10rpx 0 10rpx 0;
    border: none;
  }
}
.card-title-row {
  display: flex;
  align-items: center;
  margin-bottom: 32rpx;
}
.role-tag {
  font-size: 26rpx;
  color: #ff8917;
  border: 2rpx solid #ff8917;
  border-radius: 12rpx;
  padding: 2rpx 18rpx;
  font-weight: 400;
  background: #fff7f0;
  display: inline-block;
  vertical-align: middle;
}
.avatar-row {
  align-items: flex-start;
  .avatar {
    width: 120rpx;
    height: 120rpx;
    border-radius: 20rpx;
    margin-top: 8rpx;
    margin-left: 0;
    object-fit: cover;
  }
}
.order-row {
  .link {
    color: #ff8917;
    font-size: 30rpx;
    font-weight: 500;
    text-decoration: underline;
    margin-left: 0;
  }
}
.bottom-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: space-around;
  align-items: center;
  background: #f7f7f7;
  padding-bottom: env(safe-area-inset-bottom);
  padding-top: 24rpx;
  padding-bottom: 24rpx;
  z-index: 20;
}
.action-btn {
  flex: 1;
  margin: 0 32rpx;
  height: 80rpx;
  border-radius: 40rpx;
  font-size: 30rpx;
  font-weight: 500;
  border: 2rpx solid #ffd36d;
  background: #fffbe6;
  color: #ffb800;
  box-shadow: 0 2rpx 8rpx rgba(255, 156, 0, 0.03);
}
.action-btn.primary {
  color: #ffb800;
  border: 2rpx solid #ffd36d;
  background: #fffbe6;
}
.action-btn.danger {
  color: #ffb800;
  border: 2rpx solid #ffd36d;
  background: #fffbe6;
}
.info-card-personal {
  position: relative;
  .card-title-row {
    margin-bottom: 32rpx;
    .card-title {
      font-size: 36rpx;
      font-weight: bold;
      color: #222;
      margin-right: 18rpx;
    }
  }
  .info-row {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    min-height: 60rpx;
    margin-bottom: 0;
    padding: 18rpx 0 10rpx 0;
    .label {
      font-size: 26rpx;
      color: #b3b3b3;
      font-weight: 400;
      margin-bottom: 6rpx;
    }
    .value {
      font-size: 32rpx;
      color: #222;
      font-weight: 500;
      flex: 1;
      word-break: break-all;
      margin-bottom: 0;
    }
  }
  .avatar-row {
    align-items: flex-start;
    .avatar {
      width: 120rpx;
      height: 120rpx;
      border-radius: 24rpx;
      margin-top: 8rpx;
      margin-left: 0;
      object-fit: cover;
      display: block;
    }
  }
  .divider {
    height: 2rpx;
    background: #f3f3f3;
    margin: 10rpx 0 10rpx 0;
    border: none;
  }
}
.blocked-tag {
  position: absolute;
  right: 0;
  top: 0;
  background: #ffeaea;
  color: #ff5b5b;
  font-size: 28rpx;
  border-radius: 0 0 0 20rpx;
  padding: 12rpx 32rpx 12rpx 32rpx;
  font-weight: 400;
  z-index: 2;
}
</style> 