<template>
  <view class="apartment-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="text" 
              v-model="form.address" 
              placeholder="请输入地址"
              class="item-input"
            />
          </view>
          <view class="form-item textarea-item">
            <text class="item-label">描述</text>
            <textarea 
              v-model="form.description" 
              placeholder="请输入描述"
              class="remark-input"
              maxlength="500"
            />
          </view>
        </view>
      </view>

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

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

  export default {
    data() {
      return {
        isEdit: false,
        apartmentId: null,
        form: {
          name: '',
          address: '',
          description: ''
        }
      }
    },
    onLoad(options) {
      // 判断是否为编辑模式
      if (options.mode === 'edit' && options.id) {
        this.isEdit = true
        this.apartmentId = options.id
        this.loadApartmentDetail()
      }
    },
    methods: {
      // 加载公寓详情（编辑模式）
      async loadApartmentDetail() {
        try {
          uni.showLoading({ title: '加载中...' })
          const res = await apartmentApi.getDetail(this.apartmentId)
          const data = res.data
          
          // 填充表单数据
          this.form = {
            name: data.name || '',
            address: data.address || '',
            description: data.description || ''
          }
        } catch (error) {
          uni.showToast({ title: '加载失败', icon: 'none' })
        } finally {
          uni.hideLoading()
        }
      },

      async save() {
        if (!this.form.name.trim()) {
          uni.showToast({ title: '请输入公寓名称', icon: 'none' })
          return
        }

        try {
          uni.showLoading({ title: '保存中...' })
          
          if (this.isEdit) {
            // 编辑模式
            await apartmentApi.update(this.apartmentId, this.form)
            uni.showToast({ title: '修改成功', icon: 'success' })
          } else {
            // 添加模式
            await apartmentApi.create(this.form)
            uni.showToast({ title: '添加成功', icon: 'success' })
          }

          setTimeout(() => {
            uni.navigateBack()
          }, 1500)
        } catch (error) {
          const msg = error.data && error.data.message
          uni.showToast({ title: msg || (this.isEdit ? '修改失败' : '添加失败'), icon: 'none' })
        } finally {
          uni.hideLoading()
        }
      },

      goBack() {
        uni.navigateBack()
      }
    }
  }
</script>

<style scoped>
  .apartment-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;
  }

  .form-item:last-child {
    border-bottom: none;
  }

  .textarea-item {
    flex-direction: column;
    align-items: flex-start;
  }

  .item-label {
    width: 160rpx;
    font-size: 30rpx;
    color: #1E293B;
    font-weight: 500;
    flex-shrink: 0;
  }

  .textarea-item .item-label {
    margin-bottom: 16rpx;
  }

  .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;
  }
</style>
