<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">
            <text class="item-label">负责人</text>
            <input 
              type="text" 
              v-model="form.manager" 
              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>
      </view>

      <!-- 状态设置 -->
      <view class="form-section">
        <view class="section-title">状态设置</view>
        <view class="form-card">
          <view class="form-item">
            <text class="item-label">运营状态</text>
            <view class="status-options">
              <view 
                v-for="(option, index) in statusOptions" 
                :key="index"
                class="status-option"
                :class="{ active: form.status === option.value }"
                @click="form.status = option.value"
              >
                <text>{{option.label}}</text>
              </view>
            </view>
          </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 { apartmentApi } from '../../api/index.js'

  export default {
    data() {
      return {
        isEdit: false,
        apartmentId: null,
        form: {
          name: '',
          address: '',
          manager: '',
          phone: '',
          status: 'active',
          remark: ''
        },
        statusOptions: [
          { label: '运营中', value: 'active' },
          { label: '已停用', value: 'inactive' },
          { label: '装修中', value: 'renovating' }
        ]
      }
    },
    onLoad(options) {
      if (options.id) {
        this.isEdit = true
        this.apartmentId = options.id
        this.loadApartmentDetail(options.id)
      }
    },
    methods: {
      async loadApartmentDetail(id) {
        try {
          uni.showLoading({ title: '加载中...' })
          const res = await apartmentApi.getDetail(id)
          const data = res.data || {}
          this.form = {
            name: data.name || '',
            address: data.address || '',
            manager: data.manager || '',
            phone: data.phone || '',
            status: data.status || 'active',
            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
        }

        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) {
          uni.showToast({ title: '保存失败', icon: 'none' })
        } finally {
          uni.hideLoading()
        }
      },

      confirmDelete() {
        uni.showModal({
          title: '确认删除',
          content: '删除后无法恢复，确定要删除该公寓吗？',
          confirmColor: '#EF4444',
          success: (res) => {
            if (res.confirm) {
              this.deleteApartment()
            }
          }
        })
      },

      async deleteApartment() {
        try {
          uni.showLoading({ title: '删除中...' })
          await apartmentApi.delete(this.apartmentId)
          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>
  .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;
  }

  .item-label {
    width: 160rpx;
    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;
  }

  .status-options {
    flex: 1;
    display: flex;
    justify-content: flex-end;
    gap: 16rpx;
  }

  .status-option {
    padding: 12rpx 24rpx;
    border-radius: 8rpx;
    background: #F1F5F9;
    font-size: 26rpx;
    color: #64748B;
  }

  .status-option.active {
    background: #DBEAFE;
    color: #2563EB;
  }

  .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>
