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

const props = defineProps({
  visible: {
    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 handleSubmit = () => {
  // 验证表单
  if (!formData.value.name || !formData.value.phone || !formData.value.address) {
    uni.showToast({
      title: '请填写完整信息',
      icon: 'none',
      duration: 2000
    })
    return
  }
  
  // 验证手机号
  const phoneRegex = /^1[3-9]\d{9}$/
  if (!phoneRegex.test(formData.value.phone)) {
    uni.showToast({
      title: '请输入正确的手机号',
      icon: 'none',
      duration: 2000
    })
    return
  }
  
  // 验证留言
  if (!formData.value.msg || formData.value.msg.trim() === '') {
    uni.showToast({
      title: '请输入留言',
      icon: 'none',
      duration: 2000
    })
    return
  }
  
  // 验证留言长度
  const msgLength = formData.value.msg.length
  if (msgLength > 200) {
    uni.showToast({
      title: '留言最多200个汉字',
      icon: 'none',
      duration: 2000
    })
    return
  }
  
  emit('submit', { ...formData.value })
}
</script>

<template>
  <div class="info-page" v-if="visible">
    <!-- 背景图片 -->
    <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">
        <label class="form-label">姓名：</label>
        <input 
          v-model="formData.name" 
          type="text" 
          placeholder="请输入您的姓名" 
          class="form-input"
        />
      </div>
      
      <div class="form-item">
        <label class="form-label">联系电话：</label>
        <input 
          v-model="formData.phone" 
          type="tel" 
          placeholder="请输入您的手机号" 
          class="form-input"
        />
      </div>
      
      <div class="form-item">
        <label class="form-label">联系地址：</label>
        <input 
          v-model="formData.address" 
          type="text" 
          placeholder="请输入您的邮寄地址" 
          class="form-input"
          maxlength="100"
        />
      </div>
      
      <div class="form-item">
        <label class="form-label">留言：</label>
        <textarea 
          v-model="formData.msg" 
          placeholder="请输入您的留言" 
          class="form-textarea"
          rows="3"
          maxlength="200"
        ></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: 1000;
  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: 60rpx;
  left: 30rpx;
  z-index: 10;
}

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

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

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

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

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

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

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

.form-label {
  display: block;
  color: #fff;
  font-size: 28rpx;
  margin-bottom: 10rpx;
  font-weight: 500;
}

.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: 80rpx;
}

.form-textarea {
  min-height: 120rpx;
  resize: none;
}

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

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

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

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