<script setup>
import { ref, watch, nextTick } from 'vue'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  hasSubmitted: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['close', 'submit'])

// 表单数据
const formData = ref({
  name: '',
  phone: '',
  address: '',
  msg: ''
})

// 监听 visible 变化，打开时重置表单
watch(() => props.visible, (newVal) => {
  if (newVal) {
    formData.value = {
      name: '',
      phone: '',
      address: '',
      msg: ''
    }
  }
})

// 关闭页面
const handleClose = () => {
  emit('close')
}

// 显示提示信息
const showToast = (title) => {
  nextTick(() => {
    uni.showToast({
      title,
      icon: 'none',
      duration: 2000
    })
  })
}

// 提交表单
const handleSubmit = () => {
  // 如果已经提交过，直接提示
  if (props.hasSubmitted) {
    showToast('您已经提交过信息了，请勿重复提交')
    return
  }
  
  console.log('提交表单', formData.value)
  
  // 验证表单
  console.log('表单数据:', formData.value)
  if (!formData.value.name || !formData.value.phone || !formData.value.address || !formData.value.msg) {
    showToast('请填写完整信息')
    return
  }
  
  // 验证手机号
  const phoneRegex = /^1[3-9]\d{9}$/
  const phone = formData.value.phone.trim()
  console.log('手机号验证:', phone, '长度:', phone.length, '匹配结果:', phoneRegex.test(phone))
  if (!phoneRegex.test(phone)) {
    showToast('请输入正确的手机号')
    return
  }
  
  // 验证留言
  if (!formData.value.msg || formData.value.msg.trim() === '') {
    showToast('请输入留言')
    return
  }
  
  // 验证留言长度
  const msgLength = formData.value.msg.length
  if (msgLength > 200) {
    showToast('留言最多200个汉字')
    return
  }
  
  // 提交前清理数据
  const submitData = {
    name: formData.value.name.trim(),
    phone: formData.value.phone.trim(),
    address: formData.value.address.trim(),
    msg: formData.value.msg.trim()
  }
  
  emit('submit', submitData)
}

// 阻止 iOS 滚动穿透
const handleTouchMove = (e) => {
  e.preventDefault()
}
</script>

<template>
  <div class="info-page" v-if="visible" @touchmove="handleTouchMove">
    <!-- 背景图片 -->
    <div class="bg-container">
      <img src="/static/info/info_bg.jpg" class="bg-image" mode="aspectFill" />
    </div>
    
    <!-- 返回按钮 -->
    <div class="back-btn" @click="handleClose">
      <img src="/static/images/btn_back.png" class="back-icon" />
    </div>
    
    <!-- 标题 -->
    <div class="title-container">
      <img src="/static/info/info_title.png" class="title-image" />
    </div>
    
    <!-- 信息提示 -->
    <div class="tips-container">
      <img src="/static/info/info_tips.png" class="tips-image" />
    </div>
    
    <!-- 表单区域 -->
    <div class="form-container">
      <div class="form-item">
        <input 
          v-model="formData.name" 
          type="text" 
          placeholder="请输入您的姓名" 
          class="form-input"
          maxlength="30"
          required
        />
      </div>
      
      <div class="form-item">
        <input 
          v-model="formData.phone" 
          type="tel" 
          placeholder="请输入您的联系电话" 
          class="form-input"
          maxlength="11"
          required
        />
      </div>
      
      <div class="form-item">
        <input 
          v-model="formData.address" 
          type="text" 
          placeholder="请输入您的邮寄地址" 
          class="form-input"
          maxlength="100"
          required
        />
      </div>
      
      <div class="form-item">
        <textarea 
          v-model="formData.msg" 
          placeholder="请输入您的留言" 
          class="form-textarea"
          maxlength="200"
          required
        ></textarea>
        <span class="char-count">{{ formData.msg.length }}/200</span>
      </div>
    </div>
    
    <!-- 提交按钮 -->
    <div class="submit-container">
      <img 
        src="/static/images/btn_submit.png" 
        class="submit-btn-image" 
        @click="handleSubmit"
      />
    </div>
  </div>
</template>

<style scoped>
.info-page {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: 100;
  overflow: hidden;
}

/* 背景图片 */
.bg-container {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: -1;
}

.bg-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* 返回按钮 */
.back-btn {
  position: absolute;
  top: 30rpx;
  left: 30rpx;
  z-index: 10;
}

.back-icon {
  width: 68rpx;
  height: 68rpx;
}

/* 标题 */
.title-container {
  display: flex;
  justify-content: center;
  padding-top: 100rpx;
  margin-bottom: 20rpx;
}

.title-image {
  width: 559rpx;
  height: 213rpx;
}

/* 信息提示 */
.tips-container {
  display: flex;
  justify-content: center;
  margin-bottom: 30rpx;
}

.tips-image {
  width: 697rpx;
  height: 258rpx;
}

/* 表单区域 */
.form-container {
  padding: 0 50rpx;
}

.form-item {
  margin-bottom: 16rpx;
  position: relative;
}

.form-input,
.form-textarea {
  width: 100%;
  padding: 20rpx;
  border: none;
  border-radius: 10rpx;
  font-size: 28rpx;
  background-color: #fff;
  box-sizing: border-box;
}

.form-input {
  height: 70rpx;
  padding: 16rpx 20rpx;
}

.form-textarea {
  height: 150rpx;
  padding: 16rpx 20rpx;
  resize: none;
}

.char-count {
  position: absolute;
  right: 20rpx;
  bottom: 20rpx;
  color: #999;
  font-size: 24rpx;
}

/* 提交按钮 */
.submit-container {
  display: flex;
  justify-content: center;
  margin-top: 30rpx;
}

.submit-btn-image {
  width: 306rpx;
  height: 97rpx;
  cursor: pointer;
}

.submit-btn-image:active {
  transform: scale(0.98);
}
</style>
