<template>
  <view class="container">
    <!-- 顶部导航 -->
    <view class="nav-bar">
      <view class="back" @tap="goBack">
        <uni-icons type="left" size="20"></uni-icons>
      </view>
      <text class="title">修改地址</text>
    </view>

    <!-- 表单内容 -->
    <view class="form-content">
      <!-- 寄件人信息卡片 -->
      <view class="form-card">
        <view class="card-title">寄件人信息</view>
        <view class="form-item">
          <text class="label">寄件人姓名</text>
          <input 
            class="input" 
            type="text" 
            v-model="formData.name"
            placeholder="请输入姓名"
          />
        </view>
        <view class="form-item">
          <text class="label">寄件人电话</text>
          <input 
            class="input" 
            type="number" 
            v-model="formData.phone"
            placeholder="请输入手机号"
          />
        </view>
      </view>

      <!-- 寄件人地址卡片 -->
      <view class="form-card">
        <view class="card-title">寄件地址</view>
        <view class="form-item">
          <text class="label">寄件地址</text>
          <input 
            class="input" 
            type="text" 
            v-model="formData.address"
            placeholder="请输入详细地址"
          />
        </view>
      </view>
    </view>

    <!-- 底部确认按钮 -->
    <view class="bottom-btn" @tap="confirmEdit">确认</view>
  </view>
</template>

<script>
import pullRefreshMixin from '@/pages/mixins/pullRefreshMixin.js'

export default {
  mixins: [pullRefreshMixin],
  data() {
    return {
      formData: {
        name: '',
        phone: '',
        address: ''
      }
    }
  },
  onLoad(options) {
    // 如果是编辑模式，填充表单数据
    if (options.mode === 'edit') {
      this.formData = {
        name: decodeURIComponent(options.name || ''),
        phone: options.phone || '',
        address: decodeURIComponent(options.address || '')
      }
    }
  },
  methods: {
    async onRefresh() {
      // 模拟刷新数据
      await new Promise(resolve => setTimeout(resolve, 1000))
      this.stopPullRefresh()
    },
    goBack() {
      uni.navigateBack()
    },
    confirmEdit() {
      // 表单验证
      if (!this.formData.name.trim()) {
        return uni.showToast({
          title: '请输入姓名',
          icon: 'none'
        })
      }
      if (!/^1\d{10}$/.test(this.formData.phone)) {
        return uni.showToast({
          title: '请输入正确的手机号',
          icon: 'none'
        })
      }
      if (!this.formData.address.trim()) {
        return uni.showToast({
          title: '请输入地址',
          icon: 'none'
        })
      }

      // 获取上一页实例并更新数据
      const pages = getCurrentPages()
      const prevPage = pages[pages.length - 2]
      if (prevPage) {
        // 更新地址列表中的对应项
        const addressList = prevPage.$vm.addressList
        const index = addressList.findIndex(item => 
          item.phone === this.formData.phone && 
          item.address === this.formData.address
        )
        if (index > -1) {
          addressList[index] = { ...this.formData }
        }
      }

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

<style lang="scss" scoped>
.container {
  min-height: 100vh;
  background: #f5f5f5;
  padding-bottom: calc(140rpx + env(safe-area-inset-bottom));
}

.nav-bar {
  display: flex;
  align-items: center;
  height: 88rpx;
  background: #fff;
  padding: 0 30rpx;

  .back {
    padding: 20rpx;
    margin-left: -20rpx;
  }

  .title {
    flex: 1;
    text-align: center;
    font-size: 34rpx;
    font-weight: 500;
  }

  .right-btns {
    display: flex;
    align-items: center;
    gap: 30rpx;

    .more, .target {
      font-size: 40rpx;
      color: #333;
    }
  }
}

.form-content {
  padding: 20rpx;
}

.form-card {
  background: #fff;
  border-radius: 20rpx;
  margin-bottom: 20rpx;
  overflow: hidden;

  .card-title {
    font-size: 32rpx;
    font-weight: bold;
    color: #333;
    padding: 30rpx;
  }

  .form-item {
    padding: 20rpx 30rpx;
    border-top: 1rpx solid #f5f5f5;

    .label {
      font-size: 28rpx;
      color: #333;
      margin-bottom: 20rpx;
      display: block;
    }

    .input {
      font-size: 28rpx;
      color: #333;
      width: 100%;
    }
  }
}

.bottom-btn {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 100rpx;
  background: #FFB74D;
  color: #fff;
  font-size: 32rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  padding-bottom: env(safe-area-inset-bottom);
}
</style> 