<template>
  <view class="publish-page">
    <!-- 头部导航 -->
    <view class="header">
      <view class="back-btn" @click="goBack">
        <uni-icons type="arrowleft" size="24" color="#333"></uni-icons>
      </view>
      <text class="title">发布旅行计划</text>
      <view class="submit-btn" @click="submitForm">发布</view>
    </view>

    <!-- 表单内容 -->
    <scroll-view class="form-content" scroll-y>
      <!-- 封面图片 -->
      <view class="form-section">
        <text class="section-title">封面图片</text>
        <view class="upload-area" @click="chooseImage">
          <image v-if="formData.coverImage" :src="formData.coverImage" class="cover-image" mode="aspectFill"></image>
          <view v-else class="upload-placeholder">
            <uni-icons type="plus" size="32" color="#ccc"></uni-icons>
            <text class="upload-text">添加封面图片</text>
          </view>
        </view>
      </view>

      <!-- 基本信息 -->
      <view class="form-section">
        <text class="section-title">基本信息</text>
        
        <view class="form-item">
          <text class="label">旅行标题</text>
          <input 
            v-model="formData.title" 
            class="input" 
            placeholder="请输入旅行标题"
            maxlength="30"
          />
        </view>

        <view class="form-item">
          <text class="label">目的地</text>
          <input 
            v-model="formData.destination" 
            class="input" 
            placeholder="请输入目的地"
            @focus="showLocationPicker = true"
          />
        </view>

        <view class="form-item">
          <text class="label">旅行时间</text>
          <view class="date-range">
            <view class="date-input" @click="showStartDatePicker = true">
              <text>{{ formData.startDate || '开始日期' }}</text>
              <uni-icons type="calendar" size="16" color="#999"></uni-icons>
            </view>
            <text class="date-separator">至</text>
            <view class="date-input" @click="showEndDatePicker = true">
              <text>{{ formData.endDate || '结束日期' }}</text>
              <uni-icons type="calendar" size="16" color="#999"></uni-icons>
            </view>
          </view>
        </view>

        <view class="form-item">
          <text class="label">预算</text>
          <view class="budget-input">
            <text class="currency">¥</text>
            <input 
              v-model="formData.budget" 
              class="input" 
              type="number"
              placeholder="0"
            />
          </view>
        </view>
      </view>

      <!-- 行程详情 -->
      <view class="form-section">
        <text class="section-title">行程详情</text>
        
        <view class="form-item">
          <text class="label">行程描述</text>
          <textarea 
            v-model="formData.description" 
            class="textarea" 
            placeholder="请详细描述您的旅行计划，包括行程安排、活动内容等"
            maxlength="500"
          />
          <text class="char-count">{{ formData.description?.length || 0 }}/500</text>
        </view>

        <view class="form-item">
          <text class="label">人数限制</text>
          <view class="member-limit">
            <text>最多</text>
            <input 
              v-model="formData.maxMembers" 
              class="number-input" 
              type="number"
              placeholder="2"
            />
            <text>人</text>
          </view>
        </view>

        <view class="form-item">
          <text class="label">兴趣标签</text>
          <view class="tags-container">
            <view 
              v-for="tag in interestTags" 
              :key="tag" 
              class="tag" 
              :class="{ active: formData.tags?.includes(tag) }"
              @click="toggleTag(tag)"
            >
              <text>{{ tag }}</text>
            </view>
          </view>
        </view>
      </view>

      <!-- 隐私设置 -->
      <view class="form-section">
        <text class="section-title">隐私设置</text>
        
        <view class="form-item">
          <view class="privacy-setting">
            <text class="label">公开可见</text>
            <switch :checked="formData.isPublic" @change="togglePrivacy" color="#007aff" />
          </view>
          <text class="privacy-tip">{{ formData.isPublic ? '所有人都可以看到您的旅行计划' : '仅自己可见' }}</text>
        </view>
      </view>
    </scroll-view>

    <!-- 日期选择器 -->
    <uni-datetime-picker 
      v-if="showStartDatePicker" 
      type="date" 
      :value="formData.startDate" 
      @maskClick="showStartDatePicker = false"
      @change="onStartDateChange"
    />
    
    <uni-datetime-picker 
      v-if="showEndDatePicker" 
      type="date" 
      :value="formData.endDate" 
      @maskClick="showEndDatePicker = false"
      @change="onEndDateChange"
    />

    <!-- 加载中遮罩 -->
    <view v-if="loading" class="loading-mask">
      <uni-loading size="32" color="#007aff"></uni-loading>
      <text>发布中...</text>
    </view>
  </view>
</template>

<script>
import { travelService } from '../../api/services.js'

export default {
  data() {
    return {
      formData: {
        title: '',
        destination: '',
        startDate: '',
        endDate: '',
        budget: '',
        description: '',
        maxMembers: 4,
        coverImage: '',
        tags: [],
        isPublic: true
      },
      interestTags: [
        '摄影', '美食', '徒步', '自驾', '露营', 
        '文化', '历史', '自然', '冒险', '休闲',
        '购物', '海滩', '登山', '滑雪', '潜水'
      ],
      showStartDatePicker: false,
      showEndDatePicker: false,
      loading: false
    }
  },

  methods: {
    // 选择封面图片
    async chooseImage() {
      try {
        const res = await uni.chooseImage({
          count: 1,
          sizeType: ['compressed'],
          sourceType: ['album', 'camera']
        })
        
        if (res.tempFilePaths.length > 0) {
          this.formData.coverImage = res.tempFilePaths[0]
        }
      } catch (error) {
        console.error('选择图片失败:', error)
        uni.showToast({
          title: '选择图片失败',
          icon: 'none'
        })
      }
    },

    // 切换兴趣标签
    toggleTag(tag) {
      const index = this.formData.tags.indexOf(tag)
      if (index === -1) {
        this.formData.tags.push(tag)
      } else {
        this.formData.tags.splice(index, 1)
      }
    },

    // 切换隐私设置
    togglePrivacy(e) {
      this.formData.isPublic = e.detail.value
    },

    // 日期选择
    onStartDateChange(e) {
      this.formData.startDate = e
      this.showStartDatePicker = false
    },

    onEndDateChange(e) {
      this.formData.endDate = e
      this.showEndDatePicker = false
    },

    // 表单验证
    validateForm() {
      if (!this.formData.title.trim()) {
        uni.showToast({ title: '请输入旅行标题', icon: 'none' })
        return false
      }
      
      if (!this.formData.destination.trim()) {
        uni.showToast({ title: '请输入目的地', icon: 'none' })
        return false
      }
      
      if (!this.formData.startDate) {
        uni.showToast({ title: '请选择开始日期', icon: 'none' })
        return false
      }
      
      if (!this.formData.endDate) {
        uni.showToast({ title: '请选择结束日期', icon: 'none' })
        return false
      }
      
      if (new Date(this.formData.startDate) > new Date(this.formData.endDate)) {
        uni.showToast({ title: '结束日期不能早于开始日期', icon: 'none' })
        return false
      }
      
      if (!this.formData.budget || Number(this.formData.budget) <= 0) {
        uni.showToast({ title: '请输入合理的预算', icon: 'none' })
        return false
      }
      
      if (!this.formData.description.trim()) {
        uni.showToast({ title: '请输入行程描述', icon: 'none' })
        return false
      }
      
      if (!this.formData.maxMembers || Number(this.formData.maxMembers) < 2) {
        uni.showToast({ title: '人数限制至少2人', icon: 'none' })
        return false
      }
      
      return true
    },

    // 提交表单
    async submitForm() {
      if (!this.validateForm()) return
      
      this.loading = true
      try {
        const submitData = {
          ...this.formData,
          budget: Number(this.formData.budget),
          maxMembers: Number(this.formData.maxMembers)
        }
        
        await travelService.create(submitData)
        
        uni.showToast({
          title: '发布成功',
          icon: 'success',
          duration: 2000
        })
        
        // 发布成功后返回上一页
        setTimeout(() => {
          uni.navigateBack()
        }, 1500)
        
      } catch (error) {
        console.error('发布失败:', error)
        uni.showToast({
          title: '发布失败，请重试',
          icon: 'none'
        })
      } finally {
        this.loading = false
      }
    },

    // 返回上一页
    goBack() {
      uni.navigateBack()
    }
  }
}
</script>

<style scoped>
.publish-page {
  background-color: #f8f9fa;
  min-height: 100vh;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20rpx 30rpx;
  background-color: #fff;
  border-bottom: 1rpx solid #eee;
  position: sticky;
  top: 0;
  z-index: 100;
}

.back-btn {
  padding: 10rpx;
}

.title {
  font-size: 32rpx;
  font-weight: 600;
  color: #333;
}

.submit-btn {
  font-size: 28rpx;
  color: #007aff;
  font-weight: 500;
  padding: 10rpx 20rpx;
}

.form-content {
  height: calc(100vh - 120rpx);
  padding: 20rpx 30rpx;
}

.form-section {
  background-color: #fff;
  border-radius: 16rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
}

.section-title {
  font-size: 32rpx;
  font-weight: 600;
  color: #333;
  margin-bottom: 30rpx;
  display: block;
}

.upload-area {
  height: 200rpx;
  border: 2rpx dashed #ddd;
  border-radius: 12rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #fafafa;
}

.cover-image {
  width: 100%;
  height: 100%;
  border-radius: 12rpx;
}

.upload-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.upload-text {
  font-size: 24rpx;
  color: #999;
  margin-top: 10rpx;
}

.form-item {
  margin-bottom: 30rpx;
}

.form-item:last-child {
  margin-bottom: 0;
}

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

.input, .textarea {
  background-color: #f8f9fa;
  border-radius: 8rpx;
  padding: 20rpx;
  font-size: 28rpx;
  border: 1rpx solid #eee;
}

.textarea {
  height: 200rpx;
  line-height: 1.5;
}

.char-count {
  font-size: 24rpx;
  color: #999;
  text-align: right;
  display: block;
  margin-top: 10rpx;
}

.date-range {
  display: flex;
  align-items: center;
  gap: 20rpx;
}

.date-input {
  flex: 1;
  background-color: #f8f9fa;
  border-radius: 8rpx;
  padding: 20rpx;
  border: 1rpx solid #eee;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 28rpx;
  color: #333;
}

.date-separator {
  color: #999;
  font-size: 28rpx;
}

.budget-input {
  display: flex;
  align-items: center;
  background-color: #f8f9fa;
  border-radius: 8rpx;
  border: 1rpx solid #eee;
  overflow: hidden;
}

.currency {
  padding: 20rpx;
  background-color: #f0f0f0;
  color: #333;
  font-size: 28rpx;
  font-weight: 500;
}

.budget-input .input {
  flex: 1;
  border: none;
  background: transparent;
}

.member-limit {
  display: flex;
  align-items: center;
  gap: 10rpx;
}

.number-input {
  width: 120rpx;
  background-color: #f8f9fa;
  border-radius: 8rpx;
  padding: 20rpx;
  border: 1rpx solid #eee;
  text-align: center;
  font-size: 28rpx;
}

.tags-container {
  display: flex;
  flex-wrap: wrap;
  gap: 15rpx;
  margin-top: 15rpx;
}

.tag {
  padding: 12rpx 24rpx;
  background-color: #f8f9fa;
  border-radius: 30rpx;
  border: 1rpx solid #eee;
  font-size: 24rpx;
  color: #666;
}

.tag.active {
  background-color: #007aff;
  color: #fff;
  border-color: #007aff;
}

.privacy-setting {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10rpx;
}

.privacy-tip {
  font-size: 24rpx;
  color: #999;
}

.loading-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.loading-mask text {
  color: #fff;
  margin-top: 20rpx;
  font-size: 28rpx;
}
</style>