<template>
  <view class="tenant-add-page">
    <view class="custom-nav safe-area-top">
      <view class="nav-content">
        <view class="nav-btn" @click="goBack">
          <uni-icons type="left" size="22" color="#1E293B"></uni-icons>
        </view>
        <text class="nav-title">{{isEdit ? '编辑租客' : '添加租客'}}</text>
        <view class="nav-btn" @click="saveTenant">
          <text class="save-text">保存</text>
        </view>
      </view>
    </view>

    <scroll-view scroll-y class="form-content">
      <view class="form-card">
        <view class="form-item">
          <text class="form-label">租客姓名 <text class="required">*</text></text>
          <input 
            type="text" 
            v-model="form.name" 
            placeholder="请输入租客姓名"
            class="form-input"
            maxlength="20"
          />
        </view>

        <view class="form-item">
          <text class="form-label">联系电话 <text class="required">*</text></text>
          <input 
            type="number" 
            v-model="form.phone" 
            placeholder="请输入联系电话"
            class="form-input"
            maxlength="11"
          />
        </view>

        <view class="form-item">
          <text class="form-label">身份证号</text>
          <input 
            type="idcard" 
            v-model="form.idCard" 
            placeholder="请输入身份证号（选填）"
            class="form-input"
            maxlength="18"
          />
        </view>

        <view class="form-item">
          <text class="form-label">备注</text>
          <textarea 
            v-model="form.remark" 
            placeholder="请输入备注（选填）"
            class="form-textarea"
            maxlength="200"
          />
        </view>
      </view>

      <button class="btn-submit" :loading="submitLoading" @click="saveTenant">{{isEdit ? '保存修改' : '确认添加'}}</button>
      <view class="safe-area-bottom" style="height: 40rpx;"></view>
    </scroll-view>
  </view>
</template>

<script>
  import renterApi from '@/api/renter.js'
  
  export default {
    data() {
      return {
        isEdit: false,
        tenantId: null,
        submitLoading: false,
        form: {
          name: '',
          phone: '',
          idCard: '',
          remark: ''
        }
      }
    },
    onLoad(options) {
      if (options.id) {
        this.isEdit = true
        this.tenantId = options.id
        this.loadTenantDetail(options.id)
      }
    },
    methods: {
      async loadTenantDetail(id) {
        try {
          const res = await renterApi.getDetail(id)
          if (res.data) {
            this.form = {
              name: res.data.name,
              phone: res.data.phone,
              idCard: res.data.idCard || '',
              remark: res.data.remark || ''
            }
          }
        } catch (error) {
          console.error('加载租客详情失败:', error)
          uni.showToast({ title: '加载失败', icon: 'none' })
        }
      },
      async saveTenant() {
        if (!this.form.name.trim()) {
          uni.showToast({ title: '请输入租客姓名', icon: 'none' })
          return
        }
        if (!this.form.phone.trim()) {
          uni.showToast({ title: '请输入联系电话', icon: 'none' })
          return
        }
        if (!/^1[3-9]\d{9}$/.test(this.form.phone)) {
          uni.showToast({ title: '请输入正确的手机号', icon: 'none' })
          return
        }

        this.submitLoading = true
        try {
          const data = {
            name: this.form.name.trim(),
            phone: this.form.phone.trim(),
            idCard: this.form.idCard.trim() || null,
            remark: this.form.remark.trim() || null
          }

          if (this.isEdit) {
            await renterApi.update(this.tenantId, data)
            uni.showToast({ title: '修改成功', icon: 'success' })
          } else {
            await renterApi.create(data)
            uni.showToast({ title: '添加成功', icon: 'success' })
          }

          setTimeout(() => uni.navigateBack(), 1500)
        } catch (error) {
          console.error('保存失败:', error)
          uni.showToast({ title: this.isEdit ? '修改失败' : '添加失败', icon: 'none' })
        } finally {
          this.submitLoading = false
        }
      },
      goBack() {
        uni.navigateBack()
      }
    }
  }
</script>

<style scoped>
  .tenant-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-btn {
    width: 72rpx;
    height: 72rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
  }
  .nav-title {
    font-size: 34rpx;
    font-weight: 600;
    color: #1E293B;
  }
  .save-text {
    font-size: 28rpx;
    color: #2563EB;
    font-weight: 600;
  }
  .form-content {
    flex: 1;
    padding: 24rpx 32rpx;
  }
  .form-card {
    background: #FFFFFF;
    border-radius: 20rpx;
    padding: 0 24rpx;
    margin-bottom: 40rpx;
  }
  .form-item {
    padding: 24rpx 0;
    border-bottom: 2rpx solid #F1F5F9;
  }
  .form-item:last-child {
    border-bottom: none;
  }
  .form-label {
    display: block;
    font-size: 26rpx;
    color: #64748B;
    margin-bottom: 16rpx;
  }
  .required {
    color: #EF4444;
  }
  .form-input {
    width: 100%;
    height: 60rpx;
    font-size: 30rpx;
    color: #1E293B;
  }
  .form-textarea {
    width: 100%;
    height: 160rpx;
    font-size: 30rpx;
    color: #1E293B;
  }
  .btn-submit {
    background: linear-gradient(135deg, #2563EB 0%, #1D4ED8 100%);
    color: #FFFFFF;
    font-size: 30rpx;
    font-weight: 600;
    padding: 28rpx;
    border-radius: 16rpx;
    border: none;
  }
</style>
