<template>
  <view class="renter-detail-page">
    <view class="custom-nav safe-area-top">
      <view class="nav-content">
        <view class="nav-back" @click="goBack">
          <uni-icons type="left" size="20" color="#1E293B"></uni-icons>
        </view>
        <text class="nav-title">租客详情</text>
        <view class="nav-actions">
          <view class="nav-btn" @click="editRenter">
            <uni-icons type="compose" size="20" color="#2563EB"></uni-icons>
          </view>
        </view>
      </view>
    </view>

    <scroll-view scroll-y class="page-content" v-if="renter">
      <view class="renter-header">
        <view class="renter-avatar">
          <text class="avatar-text">{{renter.name ? renter.name.charAt(0) : '租'}}</text>
        </view>
        <text class="renter-name">{{renter.name}}</text>
        <view class="renter-status" :class="renter.status">
          <text>{{getStatusText(renter.status)}}</text>
        </view>
      </view>

      <view class="info-section">
        <view class="section-title">联系信息</view>
        <view class="info-card">
          <view class="info-item">
            <text class="item-label">手机号</text>
            <view class="item-value">
              <text>{{renter.phone || '-'}}</text>
              <uni-icons v-if="renter.phone" type="phone-filled" size="18" color="#2563EB" @click="callPhone(renter.phone)"></uni-icons>
            </view>
          </view>
          <view class="info-item">
            <text class="item-label">身份证号</text>
            <text class="item-value">{{renter.idCard || '-'}}</text>
          </view>
        </view>
      </view>

      <view class="action-section">
        <button class="action-btn primary" @click="viewRentals">查看租约</button>
        <button class="action-btn" @click="addRental">新建租约</button>
      </view>

      <view class="safe-area-bottom" style="height: 40rpx;"></view>
    </scroll-view>
  </view>
</template>

<script>
  import { renterApi } from '../../api/index.js'

  export default {
    data() {
      return {
        renterId: null,
        renter: null
      }
    },
    onLoad(options) {
      if (options.id) {
        this.renterId = options.id
        this.loadData()
      }
    },
    onShow() {
      if (this.renterId) {
        this.loadData()
      }
    },
    methods: {
      async loadData() {
        try {
          uni.showLoading({ title: '加载中...' })
          const res = await renterApi.getDetail(this.renterId)
          this.renter = res.data
        } catch (error) {
          uni.showToast({ title: '加载失败', icon: 'none' })
        } finally {
          uni.hideLoading()
        }
      },
      editRenter() {
        uni.navigateTo({ url: `/pages/renter-add/renter-add?id=${this.renterId}&mode=edit` })
      },
      viewRentals() {
        uni.navigateTo({ url: `/pages/rentals/rentals?renterId=${this.renterId}` })
      },
      addRental() {
        uni.switchTab({ url: `/pages/add-record/add-record?renterId=${this.renterId}` })
      },
      callPhone(phone) {
        uni.makePhoneCall({ phoneNumber: phone })
      },
      goBack() { uni.navigateBack() },
      getStatusText(status) {
        const texts = { 'active': '正常', 'inactive': '停用' }
        return texts[status] || '未知'
      }
    }
  }
</script>

<style scoped>
  .renter-detail-page { min-height: 100vh; background: #F8FAFC; display: flex; flex-direction: column; }
  .custom-nav { background: #FFFFFF; border-bottom: 2rpx solid #F1F5F9; }
  .nav-content { display: flex; align-items: center; justify-content: space-between; padding: 20rpx 32rpx; }
  .nav-back { width: 60rpx; height: 60rpx; display: flex; align-items: center; justify-content: center; }
  .nav-title { font-size: 36rpx; font-weight: 700; color: #1E293B; }
  .nav-actions { width: 60rpx; height: 60rpx; display: flex; align-items: center; justify-content: center; }
  .page-content { flex: 1; padding: 24rpx 32rpx; }
  .renter-header { display: flex; flex-direction: column; align-items: center; padding: 48rpx 0; }
  .renter-avatar { width: 160rpx; height: 160rpx; border-radius: 50%; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); display: flex; align-items: center; justify-content: center; margin-bottom: 24rpx; }
  .avatar-text { font-size: 64rpx; font-weight: 700; color: #FFFFFF; }
  .renter-name { font-size: 40rpx; font-weight: 700; color: #1E293B; margin-bottom: 16rpx; }
  .renter-status { padding: 8rpx 24rpx; border-radius: 8rpx; font-size: 26rpx; }
  .renter-status.active { background: #DCFCE7; color: #16A34A; }
  .renter-status.inactive { background: #F1F5F9; color: #64748B; }
  .info-section { margin-bottom: 32rpx; }
  .section-title { font-size: 28rpx; font-weight: 600; color: #94A3B8; margin-bottom: 16rpx; padding-left: 16rpx; }
  .info-card { background: #FFFFFF; border-radius: 24rpx; padding: 0 32rpx; box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.04); }
  .info-item { display: flex; align-items: center; justify-content: space-between; padding: 28rpx 0; border-bottom: 2rpx solid #F8FAFC; }
  .info-item:last-child { border-bottom: none; }
  .item-label { font-size: 30rpx; color: #64748B; }
  .item-value { display: flex; align-items: center; gap: 16rpx; font-size: 30rpx; color: #1E293B; font-weight: 500; }
  .action-section { display: flex; gap: 24rpx; margin-top: 48rpx; }
  .action-btn { flex: 1; height: 96rpx; background: #F1F5F9; color: #64748B; font-size: 32rpx; font-weight: 600; border-radius: 16rpx; border: none; display: flex; align-items: center; justify-content: center; }
  .action-btn.primary { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: #FFFFFF; }
</style>
