<template>
  <view class="order-container">
    <!-- 搜索栏 -->
    <view class="search-section">
      <uni-easyinput
        v-model="searchText"
        placeholder="搜索菜品..."
        prefixIcon="search"
        trim="both"
      ></uni-easyinput>
    </view>

    <!-- 菜品列表 -->
    <view class="menu-list">
      <view 
        v-for="(dish, index) in menuList" 
        :key="index" 
        class="menu-item"
      >
        <view class="dish-image">
          <image
            :src="dish.image"
            mode="aspectFill"
            class="image"
          ></image>
        </view>
        <view class="dish-info">
          <text class="dish-name">{{ dish.name }}</text>
          <text class="dish-description">{{ dish.description }}</text>
          <view class="dish-meta">
            <view class="meta-item">
              <uni-icons type="clock" size="14" color="#999"></uni-icons>
              <text class="meta-text">{{ dish.cookTime }}</text>
            </view>
            <view class="meta-item">
              <uni-icons type="star" size="14" color="#FFD700"></uni-icons>
              <text class="meta-text">{{ dish.rating }}</text>
            </view>
          </view>
          <view class="dish-price">
            <text class="price">¥{{ dish.price }}</text>
          </view>
        </view>
        <view class="dish-actions">
          <view class="quantity-control" v-if="dish.quantity > 0">
            <view class="btn-minus" @click="decreaseQuantity(dish)">
              <uni-icons type="minus" size="16" color="#FF6B9D"></uni-icons>
            </view>
            <text class="quantity">{{ dish.quantity }}</text>
            <view class="btn-plus" @click="increaseQuantity(dish)">
              <uni-icons type="plus" size="16" color="#fff"></uni-icons>
            </view>
          </view>
          <view v-else class="add-btn" @click="addToCart(dish)">
            <text class="add-text">添加</text>
          </view>
        </view>
      </view>
      
      <!-- 空状态 -->
      <view v-if="menuList.length === 0" class="empty-state">
        <uni-icons type="folder-add" size="80" color="#ccc"></uni-icons>
        <text class="empty-text">暂无菜品</text>
        <text class="empty-desc">菜品正在准备中</text>
      </view>
    </view>

    <!-- 购物车 -->
    <view v-if="cartItems.length > 0" class="cart-section">
      <view class="cart-header">
        <view class="cart-icon">
          <uni-icons type="cart" size="24" color="#FF6B9D"></uni-icons>
          <view class="cart-badge">{{ totalQuantity }}</view>
        </view>
        <text class="cart-total">合计：¥{{ totalPrice }}</text>
        <view class="cart-clear" @click="clearCart">
          <uni-icons type="trash" size="20" color="#999"></uni-icons>
        </view>
      </view>
      <view class="checkout-btn" @click="checkout">
        <text class="checkout-text">去结算</text>
        <uni-icons type="right" size="16" color="#fff"></uni-icons>
      </view>
    </view>
  </view>
</template>

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

// 响应式数据
const searchText = ref('')
const selectedCategory = ref('all')
const cartItems = ref([])

const categories = ref([
  { id: 'all', name: '全部' },
  { id: 'chinese', name: '中餐' },
  { id: 'western', name: '西餐' },
  { id: 'dessert', name: '甜点' },
  { id: 'drink', name: '饮品' },
  { id: 'snack', name: '小吃' }
])

const dishes = ref([
  {
    id: 1,
    name: '红烧肉',
    description: '经典美味的红烧肉，肥而不腻',
    image: '/static/images/dish1.jpg',
    category: 'chinese',
    cookTime: 60,
    difficulty: '中等',
    price: 28,
    ingredients: ['五花肉', '生抽', '老抽', '冰糖', '葱姜蒜']
  },
  {
    id: 2,
    name: '提拉米苏',
    description: '意大利经典咖啡甜点',
    image: '/static/images/dish2.jpg',
    category: 'dessert',
    cookTime: 30,
    difficulty: '简单',
    price: 18,
    ingredients: ['手指饼干', '马斯卡彭奶酪', '咖啡', '鸡蛋', '糖']
  },
  {
    id: 3,
    name: '意式咖啡',
    description: '浓郁香醇的意式浓缩咖啡',
    image: '/static/images/dish3.jpg',
    category: 'drink',
    cookTime: 5,
    difficulty: '简单',
    price: 12,
    ingredients: ['咖啡豆', '水']
  },
  {
    id: 4,
    name: '宫保鸡丁',
    description: '川菜经典，麻辣鲜香',
    image: '/static/images/dish4.jpg',
    category: 'chinese',
    cookTime: 25,
    difficulty: '中等',
    price: 22,
    ingredients: ['鸡胸肉', '花生', '干辣椒', '葱姜蒜', '生抽']
  }
])

// 计算属性
const menuList = computed(() => {
  let filtered = dishes.value
  
  // 按分类筛选
  if (selectedCategory.value !== 'all') {
    filtered = filtered.filter(dish => dish.category === selectedCategory.value)
  }
  
  // 按关键词搜索
  if (searchText.value) {
    filtered = filtered.filter(dish => 
      dish.name.includes(searchText.value) || 
      dish.description.includes(searchText.value)
    )
  }
  
  return filtered
})

const totalQuantity = computed(() => {
  return cartItems.value.reduce((total, item) => total + item.quantity, 0)
})

const totalPrice = computed(() => {
  return cartItems.value.reduce((total, item) => total + (item.price * item.quantity), 0)
})

// 方法
const onSearch = () => {
  console.log('搜索关键词:', searchText.value)
}

const selectCategory = (categoryId) => {
  selectedCategory.value = categoryId
}

const getCartQuantity = (dishId) => {
  const cartItem = cartItems.value.find(item => item.id === dishId)
  return cartItem ? cartItem.quantity : 0
}

const addToCart = (dish) => {
  const existingItem = cartItems.value.find(item => item.id === dish.id)
  
  if (existingItem) {
    existingItem.quantity++
  } else {
    cartItems.value.push({
      id: dish.id,
      name: dish.name,
      price: dish.price,
      image: dish.image,
      quantity: 1
    })
  }
  
  uni.showToast({
    title: '已添加到购物车',
    icon: 'success'
  })
}

const decreaseQuantity = (dish) => {
  const existingItem = cartItems.value.find(item => item.id === dish.id)
  
  if (existingItem) {
    if (existingItem.quantity > 1) {
      existingItem.quantity--
    } else {
      // 移除数量为0的商品
      const index = cartItems.value.findIndex(item => item.id === dish.id)
      cartItems.value.splice(index, 1)
    }
  }
}

const increaseQuantity = (dish) => {
  const existingItem = cartItems.value.find(item => item.id === dish.id)
  if (existingItem) {
    existingItem.quantity++
  } else {
    cartItems.value.push({
      id: dish.id,
      name: dish.name,
      price: dish.price,
      image: dish.image,
      quantity: 1
    })
  }
}

const clearCart = () => {
  uni.showModal({
    title: '提示',
    content: '确定要清空购物车吗？',
    success: (res) => {
      if (res.confirm) {
        cartItems.value = []
        uni.showToast({
          title: '购物车已清空',
          icon: 'success'
        })
      }
    }
  })
}

const checkout = () => {
  if (cartItems.value.length === 0) {
    uni.showToast({
      title: '购物车为空',
      icon: 'none'
    })
    return
  }
  
  // 这里可以跳转到订单确认页面
  uni.showToast({
    title: '下单功能开发中',
    icon: 'none'
  })
}

// 生命周期
onMounted(() => {
  // 可以在这里加载数据
})
</script>

<style lang="scss" scoped>
.order-container {
  min-height: 100vh;
  background: #f5f5f5;
  padding-bottom: 120rpx;
}

.search-section {
  background: #fff;
  padding: 20rpx 30rpx;
  
  .search-box {
    background: #f8f8f8;
    border-radius: 20rpx;
    overflow: hidden;
  }
}

.category-section {
  background: #fff;
  padding: 20rpx 0;
  margin-bottom: 20rpx;
  
  .category-scroll {
    white-space: nowrap;
    
    .category-list {
      display: flex;
      padding: 0 30rpx;
      
      .category-item {
        display: inline-block;
        padding: 16rpx 32rpx;
        margin-right: 20rpx;
        background: #f8f8f8;
        border-radius: 25rpx;
        transition: all 0.3s;
        
        .category-name {
          font-size: 28rpx;
          color: #666;
        }
        
        &.active {
          background: #FF6B9D;
          
          .category-name {
            color: #fff;
          }
        }
      }
    }
  }
}

.menu-list {
  padding: 0 30rpx;
  
  .menu-item {
    display: flex;
    align-items: center;
    background: #fff;
    padding: 30rpx;
    border-radius: 20rpx;
    margin-bottom: 20rpx;
    box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
    
    .dish-image {
      margin-right: 30rpx;
      
      .image {
        width: 120rpx;
        height: 120rpx;
        border-radius: 12rpx;
        object-fit: cover;
      }
    }
    
    .dish-info {
      flex: 1;
      
      .dish-name {
        display: block;
        font-size: 32rpx;
        font-weight: bold;
        color: #333;
        margin-bottom: 10rpx;
      }
      
      .dish-description {
        display: block;
        font-size: 26rpx;
        color: #999;
        margin-bottom: 20rpx;
        line-height: 1.4;
      }
      
      .dish-meta {
        display: flex;
        gap: 30rpx;
        
        .meta-item {
          display: flex;
          align-items: center;
          gap: 8rpx;
          
          .meta-text {
            font-size: 24rpx;
            color: #999;
          }
        }
      }
      
      .dish-price {
        margin-top: 15rpx;
        
        .price {
          font-size: 32rpx;
          font-weight: bold;
          color: #FF6B9D;
        }
      }
    }
    
    .dish-actions {
      display: flex;
      flex-direction: column;
      align-items: flex-end;
      gap: 20rpx;
      
      .quantity-control {
        display: flex;
        align-items: center;
        gap: 15rpx;
        
        .btn-minus, .btn-plus {
          width: 50rpx;
          height: 50rpx;
          border-radius: 50%;
          display: flex;
          align-items: center;
          justify-content: center;
          transition: all 0.3s;
          cursor: pointer;
          border: none;
        }
        
        .btn-minus {
          background: #f8f8f8;
          border: 2rpx solid #FF6B9D;
          
          &:active {
            background: #FF6B9D;
            color: #fff;
            transform: scale(0.9);
          }
        }
        
        .btn-plus {
          background: #FF6B9D;
          color: #fff;
          
          &:active {
            transform: scale(0.9);
            box-shadow: 0 2rpx 8rpx rgba(255, 107, 157, 0.4);
          }
        }
        
        .quantity {
          font-size: 28rpx;
          color: #333;
          min-width: 40rpx;
          text-align: center;
        }
      }
      
      .add-btn {
        padding: 15rpx 25rpx;
        background: #FF6B9D;
        color: #fff;
        border: none;
        border-radius: 25rpx;
        font-size: 24rpx;
        cursor: pointer;
        transition: all 0.3s ease;
        
        &:active {
          transform: scale(0.95);
          box-shadow: 0 2rpx 8rpx rgba(255, 107, 157, 0.4);
        }
      }
    }
  }
}

.empty-state {
  text-align: center;
  padding: 100rpx 0;
  
  .empty-text {
    display: block;
    font-size: 32rpx;
    color: #999;
    margin: 30rpx 0 20rpx;
  }
  
  .empty-desc {
    font-size: 26rpx;
    color: #ccc;
  }
}

.cart-section {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: #fff;
  border-radius: 20rpx 20rpx 0 0;
  box-shadow: 0 -4rpx 20rpx rgba(0, 0, 0, 0.1);
  z-index: 999;
  
  .cart-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 30rpx;
    border-bottom: 2rpx solid #f5f5f5;
    
    .cart-icon {
      position: relative;
      display: flex;
      align-items: center;
      
      .cart-badge {
        position: absolute;
        top: -8rpx;
        right: -8rpx;
        background: #FF6B9D;
        color: #fff;
        font-size: 20rpx;
        padding: 4rpx 8rpx;
        border-radius: 10rpx;
        min-width: 20rpx;
        text-align: center;
      }
    }
    
    .cart-total {
      font-size: 30rpx;
      font-weight: bold;
      color: #FF6B9D;
    }
    
    .cart-clear {
      padding: 10rpx;
      cursor: pointer;
      transition: all 0.3s ease;
      
      &:active {
        transform: scale(0.9);
      }
    }
  }
  
  .checkout-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10rpx;
    background: linear-gradient(135deg, #FF6B9D 0%, #FF8E9E 100%);
    color: #fff;
    padding: 25rpx;
    margin: 20rpx 30rpx 30rpx;
    border-radius: 25rpx;
    border: none;
    cursor: pointer;
    transition: all 0.3s ease;
    
    .checkout-text {
      font-size: 28rpx;
      font-weight: bold;
    }
    
    &:active {
      transform: scale(0.98);
      box-shadow: 0 4rpx 15rpx rgba(255, 107, 157, 0.4);
    }
  }
}
</style> 