<template>
  <view class="renter-add-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">{{isEdit ? '编辑租客' : '添加租客'}}</text>
        <view class="nav-actions">
          <view class="nav-btn" @click="save">
            <text class="save-text">保存</text>
          </view>
        </view>
      </view>
    </view>

    <scroll-view scroll-y class="page-content">
      <view class="form-section">
        <view class="section-title">基本信息</view>
        <view class="form-card">
          <view class="form-item">
            <text class="item-label required">姓名</text>
            <input type="text" v-model="form.name" placeholder="请输入租客姓名" class="item-input"/>
          </view>
          <view class="form-item">
            <text class="item-label">手机号</text>
            <input type="number" v-model="form.phone" placeholder="请输入手机号" class="item-input" maxlength="11"/>
          </view>
          <view class="form-item">
            <text class="item-label">身份证号</text>
            <input type="text" v-model="form.idCard" placeholder="请输入身份证号" class="item-input" maxlength="18"/>
          </view>
          <view class="form-item" v-if="isEdit">
            <text class="item-label">状态</text>
            <view class="status-options">
              <view class="status-option" :class="{ active: form.status === 'active' }" @click="form.status = 'active'">
                <text>正常</text>
              </view>
              <view class="status-option" :class="{ active: form.status === 'inactive' }" @click="form.status = 'inactive'">
                <text>停用</text>
              </view>
            </view>
          </view>
        </view>
      </view>

      <view v-if="isEdit" class="delete-section">
        <button class="delete-btn" @click="confirmDelete">删除租客</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 {
        isEdit: false,
        renterId: null,
        form: {
          name: '',
          phone: '',
          idCard: '',
          status: 'active'
        }
      }
    },
    onLoad(options) {
      if (options.id) {
        this.isEdit = true
        this.renterId = options.id
        this.loadRenterDetail(options.id)
      }
    },
    methods: {
      async loadRenterDetail(id) {
        try {
          uni.showLoading({ title: '加载中...' })
          const res = await renterApi.getDetail(id)
          const data = res.data || {}
          this.form = {
            name: data.name || '',
            phone: data.phone || '',
            idCard: data.idCard || '',
            status: data.status || 'active'
          }
        } catch (error) {
          uni.showToast({ title: '加载失败', icon: 'none' })
        } finally {
          uni.hideLoading()
        }
      },
      validateForm() {
        const { name, phone, idCard } = this.form
        if (!name || !name.trim()) {
          uni.showToast({ title: '请输入租客姓名', icon: 'none' })
          return false
        }
        if (name.trim().length < 2 || name.trim().length > 50) {
          uni.showToast({ title: '姓名长度应在2-50个字符', icon: 'none' })
          return false
        }
        if (phone && phone.trim()) {
          const phonePattern = /^1[3-9]\d{9}$/
          if (!phonePattern.test(phone.trim())) {
            uni.showToast({ title: '请输入正确的手机号', icon: 'none' })
            return false
          }
        }
        if (idCard && idCard.trim()) {
          const idCardPattern = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
          if (!idCardPattern.test(idCard.trim())) {
            uni.showToast({ title: '请输入正确的身份证号', icon: 'none' })
            return false
          }
        }
        return true
      },
      async save() {
        if (!this.validateForm()) return
        try {
          uni.showLoading({ title: '保存中...' })
          if (this.isEdit) {
            await renterApi.update(this.renterId, this.form)
            uni.showToast({ title: '更新成功', icon: 'success' })
          } else {
            await renterApi.create(this.form)
            uni.showToast({ title: '添加成功', icon: 'success' })
          }
          setTimeout(() => { uni.navigateBack() }, 1500)
        } catch (error) {
          uni.showToast({ title: '保存失败', icon: 'none' })
        } finally {
          uni.hideLoading()
        }
      },
      confirmDelete() {
        uni.showModal({
          title: '确认删除',
          content: '删除后无法恢复，确定要删除该租客吗？',
          confirmColor: '#EF4444',
          success: (res) => { if (res.confirm) this.deleteRenter() }
        })
      },
      async deleteRenter() {
        try {
          uni.showLoading({ title: '删除中...' })
          await renterApi.delete(this.renterId)
          uni.showToast({ title: '删除成功', icon: 'success' })
          setTimeout(() => { uni.navigateBack() }, 1500)
        } catch (error) {
          uni.showToast({ title: '删除失败', icon: 'none' })
        } finally {
          uni.hideLoading()
        }
      },
      goBack() { uni.navigateBack() }
    }
  }
</script>

<style scoped>
  .renter-add-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: 80rpx; display: flex; align-items: center; justify-content: flex-end; }
  .save-text { font-size: 30rpx; color: #2563EB; font-weight: 600; }
  .page-content { flex: 1; padding: 24rpx 32rpx; }
  .form-section { margin-bottom: 32rpx; }
  .section-title { font-size: 28rpx; font-weight: 600; color: #94A3B8; margin-bottom: 16rpx; padding-left: 16rpx; }
  .form-card { background: #FFFFFF; border-radius: 24rpx; overflow: hidden; box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.04); }
  .form-item { display: flex; align-items: center; padding: 28rpx 32rpx; border-bottom: 2rpx solid #F8FAFC; position: relative; }
  .form-item:last-child { border-bottom: none; }
  .item-label { width: 180rpx; font-size: 30rpx; color: #1E293B; font-weight: 500; }
  .item-label.required::after { content: '*'; color: #EF4444; margin-left: 8rpx; }
  .item-input { flex: 1; font-size: 30rpx; color: #1E293B; text-align: right; }
  .delete-section { margin-top: 48rpx; }
  .delete-btn { width: 100%; height: 96rpx; background: #FEE2E2; color: #EF4444; font-size: 32rpx; font-weight: 600; border-radius: 16rpx; border: none; display: flex; align-items: center; justify-content: center; }
  .status-options { display: flex; gap: 24rpx; }
  .status-option { padding: 16rpx 32rpx; border-radius: 12rpx; background: #F1F5F9; font-size: 28rpx; color: #64748B; }
  .status-option.active { background: #DBEAFE; color: #2563EB; }
</style>
