<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 required">手机号</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">
            <text class="item-label">紧急联系人</text>
            <input type="text" v-model="form.emergencyContact" placeholder="请输入紧急联系人" class="item-input"/>
          </view>
          <view class="form-item">
            <text class="item-label">紧急电话</text>
            <input type="number" v-model="form.emergencyPhone" placeholder="请输入紧急联系电话" class="item-input" maxlength="11"/>
          </view>
        </view>
      </view>

      <view class="form-section">
        <view class="section-title">备注信息</view>
        <view class="form-card">
          <view class="form-item">
            <textarea v-model="form.remark" placeholder="请输入备注信息（选填）" class="remark-input" maxlength="200"/>
            <text class="word-count">{{form.remark.length}}/200</text>
          </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: '',
          emergencyContact: '',
          emergencyPhone: '',
          remark: ''
        }
      }
    },
    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 || '',
            emergencyContact: data.emergencyContact || '',
            emergencyPhone: data.emergencyPhone || '',
            remark: data.remark || ''
          }
        } catch (error) {
          uni.showToast({ title: '加载失败', icon: 'none' })
        } finally {
          uni.hideLoading()
        }
      },
      async save() {
        if (!this.form.name.trim()) {
          uni.showToast({ title: '请输入姓名', icon: 'none' })
          return
        }
        if (!this.form.phone.trim()) {
          uni.showToast({ title: '请输入手机号', icon: 'none' })
          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; }
  .remark-input { width: 100%; height: 200rpx; font-size: 30rpx; color: #1E293B; line-height: 1.6; }
  .word-count { position: absolute; right: 32rpx; bottom: 24rpx; font-size: 24rpx; color: #94A3B8; }
  .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; }
</style>
