<template>
  <view class="my-publish-container">
    <!-- 顶部Tab标签页 -->
    <view class="tab-header">
      <view class="tab-list">
        <view 
          class="tab-item"
          :class="{ active: activeTab === 'published' }"
          @click="switchTab('published')"
        >
          <text class="tab-text">销售信息</text>
        </view>
        <view 
          class="tab-item"
          :class="{ active: activeTab === 'purchase' }"
          @click="switchTab('purchase')"
        >
          <text class="tab-text">收购信息</text>
        </view>
      </view>
      <view class="tab-indicator" :style="{ left: activeTab === 'published' ? '0' : '50%' }"></view>
    </view>

    <!-- 我的发布页面 -->
    <view v-if="activeTab === 'published'" class="tab-content">
      <!-- 搜索筛选 -->
      <view class="search-section">
        <view class="search-box">
          <image class="search-icon" src="/static/icons/search.png" mode="aspectFit"></image>
          <input 
            class="search-input" 
            placeholder="搜索我的农产品" 
            placeholder-style="color: #999;"
            v-model="searchKeyword"
            @confirm="handleSearch"
          />
        </view>
      </view>
      
      <!-- 我的发布列表 -->
      <view class="products-container">
        <view class="products-list">
          <view 
            class="published-card"
            v-for="item in filteredPublishedProducts" 
            :key="item.id"
            @click="navigateToDetail(item)"
          >
            <view class="card-content">
              <view class="product-image">
                <image :src="item.image" mode="aspectFill"></image>
              </view>
              <view class="product-info">
                <view class="product-header">
                  <view class="product-name">{{ item.name }}</view>
                  <view class="price-info">¥{{ item.price }} 元/{{ item.unit }}</view>
                </view>
                <view class="product-desc">{{ item.description }}</view>
                <view class="product-footer">
                  <view class="publish-time">{{ item.publishTime }}</view>
                  <view class="status-tag" :class="item.status">
                    {{ getStatusText(item.status) }}
                  </view>
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 收购信息页面 -->
    <view v-if="activeTab === 'purchase'" class="tab-content">
      <!-- 搜索筛选 -->
      <view class="search-section">
        <view class="search-box">
          <image class="search-icon" src="/static/icons/search.png" mode="aspectFit"></image>
          <input 
            class="search-input" 
            placeholder="搜索收购信息" 
            placeholder-style="color: #999;"
            v-model="purchaseSearchKeyword"
            @confirm="handlePurchaseSearch"
          />
        </view>
      </view>
      
      <!-- 收购信息列表 -->
      <view class="products-container">
        <view class="products-list">
          <view 
            class="published-card"
            v-for="item in filteredPurchaseList" 
            :key="item.id"
            @click="navigateToPurchaseDetail(item)"
          >
            <view class="card-content">
              <view class="product-image">
                <image :src="getPurchaseImage(item)" mode="aspectFill"></image>
              </view>
              <view class="product-info">
                <view class="product-header">
                  <view class="product-name">{{ item.title }}</view>
                  <view class="price-info">¥{{ item.budgetPrice }} 元/{{ item.unit }}</view>
                </view>
                <view class="product-desc">需求数量：{{ item.quantity }}</view>
                <view class="product-footer">
                  <view class="publish-time">{{ item.publishTime }}</view>
                  <view class="status-tag" :class="item.status">
                    {{ getStatusText(item.status) }}
                  </view>
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 底部固定按钮 -->
    <view class="bottom-action-bar">
      <view class="action-btn" @click="showPublishModal = true">
        <text class="btn-text">发布</text>
      </view>
    </view>

    <!-- 发布选择弹窗 -->
    <view class="publish-modal" v-if="showPublishModal" @click="showPublishModal = false">
      <view class="publish-content" @click.stop>
        <view class="publish-header">
          <text class="publish-title">选择发布类型</text>
          <text class="publish-close" @click="showPublishModal = false">✕</text>
        </view>
        <view class="publish-options">
          <view class="option-item" @click="navigateToPublish">
            <view class="option-text">
              <text class="option-title">发布农产品</text>
              <text class="option-desc">出售我的农产品</text>
            </view>
          </view>
          <view class="option-item" @click="navigateToPurchasePublish">
            <view class="option-text">
              <text class="option-title">发布收购信息</text>
              <text class="option-desc">收购农产品需求</text>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      activeTab: 'published',
      searchKeyword: '',
      purchaseSearchKeyword: '',
      showPublishModal: false,
      
      // 我的发布列表
      publishedProductsList: [
        {
          id: 1,
          name: '有机苹果',
          description: '新鲜采摘，香甜可口',
          price: '8.5',
          unit: '斤',
          image: '/static/images/products/corn-seeds-new.jpg',
          publishTime: '今天',
          status: 'approved'
        },
        {
          id: 2,
          name: '优质大米',
          description: '当季新米，粒粒饱满',
          price: '6.8',
          unit: '斤',
          image: '/static/images/products/rice-seeds.jpg',
          publishTime: '昨天',
          status: 'pending'
        },
        {
          id: 3,
          name: '新鲜蔬菜包',
          description: '时令蔬菜，营养丰富',
          price: '25',
          unit: '份',
          image: '/static/images/products/organic-fertilizer-new.jpg',
          publishTime: '2天前',
          status: 'approved'
        },
        {
          id: 4,
          name: '土鸡蛋',
          description: '散养土鸡，营养价值高',
          price: '2.5',
          unit: '个',
          image: '/static/images/products/seeds-packets.jpg',
          publishTime: '3天前',
          status: 'rejected'
        }
      ],
      
      // 收购信息列表
      purchaseList: [
        {
          id: 1,
          title: '高价收购优质土豆',
          category: '蔬菜',
          quantity: '1000斤',
          budgetPrice: '3.5',
          unit: '斤',
          contact: '张**',
          location: '本地',
          publishTime: '今天',
          status: 'approved'
        },
        {
          id: 2,
          title: '大量收购新鲜玉米',
          category: '粮食',
          quantity: '5000斤',
          budgetPrice: '2.8',
          unit: '斤',
          contact: '李**',
          location: '周边',
          publishTime: '昨天',
          status: 'pending'
        },
        {
          id: 3,
          title: '收购有机蔬菜',
          category: '蔬菜',
          quantity: '500斤',
          budgetPrice: '8.0',
          unit: '斤',
          contact: '王**',
          location: '本地',
          publishTime: '2天前',
          status: 'rejected'
        }
      ]
    }
  },
  
  computed: {
    filteredPublishedProducts() {
      let products = this.publishedProductsList;
      
      if (this.searchKeyword.trim()) {
        const keyword = this.searchKeyword.trim().toLowerCase();
        products = products.filter(item => 
          item.name.toLowerCase().includes(keyword) || 
          item.description.toLowerCase().includes(keyword)
        );
      }
      
      return products;
    },
    
    filteredPurchaseList() {
      let list = this.purchaseList;
      
      if (this.purchaseSearchKeyword.trim()) {
        const keyword = this.purchaseSearchKeyword.trim().toLowerCase();
        list = list.filter(item => 
          item.title.toLowerCase().includes(keyword) || 
          item.category.toLowerCase().includes(keyword)
        );
      }
      
      return list;
    }
  },
  
  methods: {
    switchTab(tab) {
      this.activeTab = tab;
    },
    
    getStatusText(status) {
      const statusMap = {
        'pending': '审核中',
        'approved': '已上架',
        'rejected': '已下架'
      };
      return statusMap[status] || '未知';
    },
    
    handleSearch() {
      // 搜索逻辑已在computed中处理
    },
    
    handlePurchaseSearch() {
      // 搜索逻辑已在computed中处理
    },
    
    navigateToDetail(product) {
      uni.navigateTo({
        url: `/pages/service/sales-detail?id=${product.id}&type=sale&name=${encodeURIComponent(product.name)}&source=myPublish&status=${product.status}`
      });
    },
    
    navigateToPurchaseDetail(item) {
      uni.navigateTo({
        url: `/pages/service/sales-detail?id=${item.id}&type=purchase&title=${encodeURIComponent(item.title)}&source=myPublish&status=${item.status}`
      });
    },
    
    navigateToPublish() {
      this.showPublishModal = false;
      uni.navigateTo({
        url: '/pages/service/sales-publish'
      });
    },
    
    navigateToPurchasePublish() {
      this.showPublishModal = false;
      uni.navigateTo({
        url: '/pages/service/purchase-publish'
      });
    },
    
    // 获取收购信息对应的图片
    getPurchaseImage(item) {
      // 根据分类返回对应的默认图片
      const categoryImages = {
        '蔬菜': '/static/images/products/organic-fertilizer-new.jpg',
        '粮食': '/static/images/products/corn-seeds-new.jpg',
        '水果': '/static/images/products/rice-seeds.jpg',
        '其他': '/static/images/products/agriculture-tools.jpg'
      };
      return categoryImages[item.category] || categoryImages['其他'];
    }
  }
}
</script>

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

.tab-header {
  background-color: #fff;
  position: relative;
  border-bottom: 1rpx solid #f0f0f0;
}

.tab-list {
  display: flex;
  align-items: center;
}

.tab-item {
  flex: 1;
  text-align: center;
  padding: 32rpx 0;
  position: relative;
  
  .tab-text {
    font-size: 30rpx;
    color: #666;
    font-weight: 500;
    transition: color 0.3s ease;
  }
  
  &.active .tab-text {
    color: #4CAF50;
    font-weight: bold;
  }
}

.tab-indicator {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 50%;
  height: 4rpx;
  background-color: #4CAF50;
  transition: left 0.3s ease;
}

.tab-content {
  flex: 1;
}

.search-section {
  background-color: #fff;
  padding: 20rpx;
  border-bottom: 1rpx solid #f0f0f0;
}

.search-box {
  flex: 1;
  display: flex;
  align-items: center;
  background-color: #f8f8f8;
  border-radius: 32rpx;
  padding: 16rpx 24rpx;
}

.search-icon {
  width: 32rpx;
  height: 32rpx;
  margin-right: 16rpx;
}

.search-input {
  flex: 1;
  font-size: 28rpx;
  line-height: 1.5;
}

.products-container, .purchase-container {
  padding: 20rpx;
}

.products-list, .purchase-list {
  display: flex;
  flex-direction: column;
  gap: 16rpx;
}

.published-card {
  background-color: #fff;
  border-radius: 12rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
  transition: transform 0.2s ease;
  
  &:active {
    transform: scale(0.98);
  }
}

.card-content {
  display: flex;
  padding: 20rpx;
  gap: 20rpx;
}

.product-image {
  width: 160rpx;
  height: 160rpx;
  border-radius: 8rpx;
  overflow: hidden;
  flex-shrink: 0;
  
  image {
    width: 100%;
    height: 100%;
  }
}

.product-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.product-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 12rpx;
}

.product-name {
  font-size: 30rpx;
  font-weight: bold;
  color: #4CAF50;
  flex: 1;
  margin-right: 16rpx;
}

.price-info {
  font-size: 26rpx;
  color: #333;
  font-weight: bold;
  white-space: nowrap;
}

.product-desc {
  font-size: 26rpx;
  color: #666;
  margin-bottom: 16rpx;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.product-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.publish-time {
  font-size: 24rpx;
  color: #999;
}

.status-tag {
  padding: 8rpx 16rpx;
  border-radius: 16rpx;
  font-size: 22rpx;
  
  &.pending {
    background-color: #fff7e6;
    color: #fa8c16;
  }
  
  &.approved {
    background-color: #f6ffed;
    color: #52c41a;
  }
  
  &.rejected {
    background-color: #fff2f0;
    color: #ff4d4f;
  }
}

// 收购信息现在使用统一的 .published-card 样式

// 底部固定按钮
.bottom-action-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  width: 100%;
  background-color: #fff;
  border-top: 1rpx solid #f0f0f0;
  padding: 20rpx 30rpx;
  padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
  z-index: 1000;
  box-shadow: 0 -2rpx 8rpx rgba(0, 0, 0, 0.05);
  box-sizing: border-box;
}

.action-btn {
  width: 100%;
  height: 88rpx;
  background-color: #4CAF50;
  border-radius: 12rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.2s ease;
  box-sizing: border-box;
  
  &:active {
    background-color: #45a049;
  }
  
  .btn-text {
    font-size: 32rpx;
    color: #fff;
    font-weight: bold;
  }
}

.publish-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 2000;
  display: flex;
  align-items: flex-end;
}

.publish-content {
  width: 100%;
  background-color: #fff;
  border-radius: 24rpx 24rpx 0 0;
  padding: 0;
}

.publish-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30rpx;
  border-bottom: 1rpx solid #f0f0f0;
  
  .publish-title {
    font-size: 32rpx;
    font-weight: bold;
    color: #333;
  }
  
  .publish-close {
    font-size: 36rpx;
    color: #999;
  }
}

.publish-options {
  padding: 20rpx;
}

.option-item {
  display: flex;
  align-items: center;
  padding: 24rpx 20rpx;
  border-radius: 12rpx;
  margin-bottom: 16rpx;
  background-color: #f8f8f8;
  transition: background-color 0.2s ease;
  
  &:last-child {
    margin-bottom: 0;
  }
  
  &:active {
    background-color: #e8f5e8;
  }
}

.option-text {
  flex: 1;
  
  .option-title {
    font-size: 30rpx;
    font-weight: bold;
    color: #333;
    display: block;
    margin-bottom: 8rpx;
  }
  
  .option-desc {
    font-size: 26rpx;
    color: #666;
    display: block;
  }
}
</style> 