<template>
  <view class="container">
    <!-- 搜索栏 -->
    <view class="search-container">
      <view class="search-box">
        <image src="/static/icons/search.png" mode="aspectFit" class="search-icon"></image>
        <input 
          type="text" 
          placeholder="请输入药品名称或症状" 
          v-model="searchKeyword" 
          @input="handleInput"
          @confirm="searchMedicine"
          class="search-input"
        >
        <image 
          src="/static/icons/voice.png" 
          mode="aspectFit" 
          class="voice-icon"
          @click="startVoiceSearch"
        ></image>
      </view>
      <view class="search-btn" @click="searchMedicine">搜索</view>
    </view>
    
    <!-- 热门搜索 -->
    <view class="hot-search">
      <text class="hot-title">热门搜索</text>
      <view class="hot-tags">
        <view class="tag" @click="selectHotTag('感冒')">感冒</view>
        <view class="tag" @click="selectHotTag('退烧药')">退烧药</view>
        <view class="tag" @click="selectHotTag('胃药')">胃药</view>
        <view class="tag" @click="selectHotTag('消炎药')">消炎药</view>
        <view class="tag" @click="selectHotTag('降压药')">降压药</view>
      </view>
    </view>
    
    <!-- 药品分类 -->
    <view class="category-container">
      <text class="category-title">药品分类</text>
      <scroll-view class="category-scroll" scroll-x="true">
        <view class="category-item" v-for="(category, index) in categories" :key="index" 
              :class="{active: activeCategory === index}" @click="changeCategory(index)">
          <view class="category-icon">
    
          </view>
          <text>{{category.name}}</text>
        </view>
      </scroll-view>
    </view>
    
    <!-- 药品列表 -->
    <view class="medicine-list">
      <view class="list-header">
        <text class="list-title">推荐药品</text>
        <text class="view-all" @click="viewAllMedicine">查看全部</text>
      </view>
      
      <view class="medicine-cards">
        <view class="medicine-card" v-for="(medicine, index) in medicineList" :key="index" @click="goToMedicineDetail(medicine.id)">
          <view class="card-image">
            <image :src="medicine.image" mode="aspectFit"></image>
            <view class="badge" v-if="medicine.badge">{{medicine.badge}}</view>
          </view>
          <view class="card-content">
            <text class="card-title">{{medicine.name}}</text>
            <text class="card-subtitle">{{medicine.brand}}</text>
            <view class="card-info">
              <text class="card-price">¥{{medicine.price}}</text>
              <text class="card-rating">
                <image src="/static/icons/star.png" mode="aspectFit"></image>
                {{medicine.rating}}
              </text>
            </view>
          </view>
        </view>
      </view>
    </view>
    
    <!-- 搜索结果 -->
    <view class="search-results" v-show="showSearchResults">
      <view class="result-header">
        <text class="result-count">找到 {{searchResults.length}} 个结果</text>
        <text class="close-results" @click="closeSearchResults">关闭</text>
      </view>
      
      <view class="result-list">
        <view class="result-item" v-for="(result, index) in searchResults" :key="index" @click="goToMedicineDetail(result.id)">
          <image :src="result.image" mode="aspectFit" class="result-image"></image>
          <view class="result-content">
            <text class="result-title">{{result.name}}</text>
            <text class="result-subtitle">{{result.brand}}</text>
            <text class="result-price">¥{{result.price}}</text>
          </view>
        </view>
      </view>
    </view>
    
    <!-- 语音搜索对话框 -->
    <view class="voice-dialog" v-show="showVoiceDialog">
      <view class="dialog-mask" @click="closeVoiceDialog"></view>
      <view class="dialog-content">
        <text class="dialog-title">语音搜索</text>
        <view class="voice-icon-container">
          <image src="/static/icons/mic.png" mode="aspectFit" class="voice-mic"></image>
        </view>
        <text class="dialog-text">{{voiceText}}</text>
        <view class="dialog-buttons">
          <view class="cancel-btn" @click="closeVoiceDialog">取消</view>
          <view class="confirm-btn" @click="confirmVoiceSearch">搜索</view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      searchKeyword: '',
      activeCategory: 0,
      showSearchResults: false,
      searchResults: [],
      showVoiceDialog: false,
      voiceText: '',
      
      // 药品分类数据
      categories: [
        { name: '全部' },
        { name: '感冒药'},
        { name: '退烧药' },
        { name: '胃药' },
        { name: '消炎药' },
        { name: '降压药' },
        { name: '维生素'},
        { name: '处方药'}
      ],
      
      // 药品列表数据
      medicineList: [
        {
          id: 1,
          name: '复方感冒灵颗粒',
          brand: '白云山',
          price: 19.8,
          rating: 4.8,
          image: 'https://ts1.tc.mm.bing.net/th/id/OIP-C.yRpM0KgEQAAoYchF1zOCugHaHa?w=176&h=185&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2',
          badge: '热销'
        },
        {
          id: 2,
          name: '布洛芬缓释胶囊',
          brand: '仁和',
          price: 25.5,
          rating: 4.7,
          image: 'https://ts1.tc.mm.bing.net/th/id/OIP-C.cTH6c2LYnbJa_trONrQ9ugHaHa?w=176&h=185&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2',
          badge: '推荐'
        },
        {
          id: 3,
          name: '奥美拉唑肠溶胶囊',
          brand: '修正',
          price: 32.0,
          rating: 4.9,
          image: 'https://ts1.tc.mm.bing.net/th/id/OIP-C.-noX8xNBWk0hg1PzApO7RwHaHa?w=176&h=185&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2'
        },
        {
          id: 4,
          name: '阿莫西林胶囊',
          brand: '三九',
          price: 28.5,
          rating: 4.6,
          image: 'https://ts1.tc.mm.bing.net/th/id/OIP-C.-IsXcevM3wDYT7pawrYzYgHaHa?w=176&h=185&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2',
          badge: '新品'
        },
        {
          id: 5,
          name: '硝苯地平缓释片',
          brand: '同仁堂',
          price: 45.0,
          rating: 4.8,
          image: 'https://ts1.tc.mm.bing.net/th/id/OIP-C.Ub5_rVws4JdzjnYw9R9k0AHaHa?w=176&h=185&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2'
        },
        {
          id: 6,
          name: '维生素C咀嚼片',
          brand: '汤臣倍健',
          price: 68.0,
          rating: 4.7,
          image: 'https://tse2-mm.cn.bing.net/th/id/OIP-C.12L6lrIdl2T1mqPeGWDc1gHaHa?w=203&h=203&c=7&r=0&o=5&dpr=1.3&pid=1.7'
        }
      ]
    }
  },
  methods: {
    // 处理输入事件
    handleInput() {
      // 实时搜索逻辑
      if (this.searchKeyword.length > 0) {
        this.searchMedicine()
      } else {
        this.showSearchResults = false
      }
    },
    
    // 搜索药品
    searchMedicine() {
      if (!this.searchKeyword.trim()) {
        uni.showToast({
          title: '请输入搜索关键词',
          icon: 'none'
        })
        return
      }
      
      // 模拟搜索结果
      this.searchResults = this.medicineList.filter(medicine => 
        medicine.name.includes(this.searchKeyword) || 
        medicine.brand.includes(this.searchKeyword)
      )
      
      this.showSearchResults = true
    },
    
    // 选择热门标签
    selectHotTag(tag) {
      this.searchKeyword = tag
      this.searchMedicine()
    },
    
    // 切换分类
    changeCategory(index) {
      this.activeCategory = index
      // 这里可以添加分类切换后的逻辑
      uni.showToast({
        title: `已切换到${this.categories[index].name}`,
        icon: 'none'
      })
    },
    
    // 查看全部药品
    viewAllMedicine() {
      uni.navigateTo({
        url: '/pages/medicine/all-medicine'
      })
    },
    
    // 跳转到药品详情页
    goToMedicineDetail(id) {
      uni.navigateTo({
        url: `/pages/medicine/detail?id=${id}`
      })
    },
    
    // 关闭搜索结果
    closeSearchResults() {
      this.showSearchResults = false
    },
    
    // 开始语音搜索
    startVoiceSearch() {
      this.showVoiceDialog = true
      this.voiceText = '正在识别...'
      
      // 模拟语音识别
      setTimeout(() => {
        this.voiceText = '布洛芬缓释胶囊'
      }, 1500)
    },
    
    // 关闭语音对话框
    closeVoiceDialog() {
      this.showVoiceDialog = false
    },
    
    // 确认语音搜索
    confirmVoiceSearch() {
      if (this.voiceText && this.voiceText !== '正在识别...') {
        this.searchKeyword = this.voiceText
        this.showVoiceDialog = false
        this.searchMedicine()
      }
    }
  },
  
  onLoad() {
    // 页面加载时的逻辑
  },
  
  onShow() {
    // 页面显示时的逻辑
  }
}
</script>

<style>
/* 全局样式 */
page {
  background-color: #f5f5f5;
  font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif;
}

.container {
  padding: 20rpx;
}

/* 搜索栏样式 */
.search-container {
  display: flex;
  align-items: center;
  margin-bottom: 20rpx;
}

.search-box {
  flex: 1;
  height: 80rpx;
  background-color: #fff;
  border-radius: 40rpx;
  display: flex;
  align-items: center;
  padding: 0 20rpx;
  margin-right: 20rpx;
}

.search-icon {
  width: 36rpx;
  height: 36rpx;
  margin-right: 10rpx;
}

.search-input {
  flex: 1;
  height: 100%;
  font-size: 28rpx;
}

.voice-icon {
  width: 36rpx;
  height: 36rpx;
  margin-left: 10rpx;
}

.search-btn {
  width: 160rpx;
  height: 80rpx;
  background-color: #1677ff;
  color: #fff;
  text-align: center;
  line-height: 80rpx;
  border-radius: 40rpx;
  font-size: 30rpx;
}

/* 热门搜索样式 */
.hot-search {
  margin-bottom: 30rpx;
}

.hot-title {
  font-size: 28rpx;
  color: #333;
  margin-bottom: 15rpx;
}

.hot-tags {
  display: flex;
  flex-wrap: wrap;
}

.tag {
  padding: 10rpx 20rpx;
  background-color: #f0f0f0;
  border-radius: 20rpx;
  font-size: 26rpx;
  color: #666;
  margin-right: 20rpx;
  margin-bottom: 15rpx;
}

.tag:hover {
  background-color: #e6f4ff;
  color: #1677ff;
}

/* 药品分类样式 */
.category-container {
  margin-bottom: 30rpx;
}

.category-title {
  font-size: 28rpx;
  color: #333;
  margin-bottom: 15rpx;
}

.category-scroll {
  white-space: nowrap;
  padding-bottom: 10rpx;
}

.category-item {
  display: inline-block;
  text-align: center;
  margin-right: 40rpx;
}

.category-item.active text {
  color: #1677ff;
}

.category-icon {
  width: 80rpx;
  height: 15rpx;
  background-color: #f5f5f5;
  border-radius: 40rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 10rpx;
}

.category-icon image {
  width: 48rpx;
  height: 48rpx;
}

.category-item text {
  font-size: 24rpx;
  color: #666;
}

/* 药品列表样式 */
.medicine-list {
  margin-bottom: 30rpx;
}

.list-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
}

.list-title {
  font-size: 30rpx;
  font-weight: bold;
  color: #333;
}

.view-all {
  font-size: 26rpx;
  color: #1677ff;
}

.medicine-cards {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}

.medicine-card {
  width: 345rpx;
  background-color: #fff;
  border-radius: 16rpx;
  margin-bottom: 20rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.card-image {
  position: relative;
  width: 100%;
  height: 345rpx;
  overflow: hidden;
}

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

.badge {
  position: absolute;
  top: 15rpx;
  left: 15rpx;
  background-color: #ff4d4f;
  color: #fff;
  font-size: 22rpx;
  padding: 5rpx 10rpx;
  border-radius: 8rpx;
}

.card-content {
  padding: 15rpx;
}

.card-title {
  font-size: 28rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 8rpx;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
  overflow: hidden;
}

.card-subtitle {
  font-size: 24rpx;
  color: #666;
  margin-bottom: 12rpx;
}

.card-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-price {
  font-size: 30rpx;
  font-weight: bold;
  color: #ff4d4f;
}

.card-rating {
  display: flex;
  align-items: center;
  font-size: 24rpx;
  color: #ff7d00;
}

.card-rating image {
  width: 24rpx;
  height: 24rpx;
  margin-right: 5rpx;
}

/* 搜索结果样式 */
.search-results {
  position: fixed;
  top: 260rpx;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #fff;
  z-index: 100;
  padding: 20rpx;
  overflow-y: auto;
}

.result-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
  padding-bottom: 20rpx;
  border-bottom: 1rpx solid #eee;
}

.result-count {
  font-size: 28rpx;
  color: #333;
}

.close-results {
  font-size: 26rpx;
  color: #999;
}

.result-list {
  padding-bottom: 20rpx;
}

.result-item {
  display: flex;
  align-items: center;
  padding: 20rpx 0;
  border-bottom: 1rpx solid #eee;
}

.result-image {
  width: 120rpx;
  height: 120rpx;
  border-radius: 12rpx;
  margin-right: 20rpx;
}

.result-content {
  flex: 1;
}

.result-title {
  font-size: 28rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 8rpx;
}

.result-subtitle {
  font-size: 24rpx;
  color: #666;
  margin-bottom: 10rpx;
}

.result-price {
  font-size: 30rpx;
  font-weight: bold;
  color: #ff4d4f;
}

/* 语音搜索对话框样式 */
.voice-dialog {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 200;
}

.dialog-mask {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
}

.dialog-content {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 600rpx;
  background-color: #fff;
  border-radius: 24rpx;
  padding: 40rpx;
  text-align: center;
}

.dialog-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 40rpx;
}

.voice-icon-container {
  width: 180rpx;
  height: 180rpx;
  background-color: #e6f4ff;
  border-radius: 90rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 40rpx;
}

.voice-mic {
  width: 80rpx;
  height: 80rpx;
}

.dialog-text {
  font-size: 30rpx;
  color: #666;
  margin-bottom: 60rpx;
}

.dialog-buttons {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.cancel-btn {
  width: 250rpx;
  height: 80rpx;
  background-color: #f0f0f0;
  color: #666;
  text-align: center;
  line-height: 80rpx;
  border-radius: 40rpx;
  font-size: 30rpx;
}

.confirm-btn {
  width: 250rpx;
  height: 80rpx;
  background-color: #1677ff;
  color: #fff;
  text-align: center;
  line-height: 80rpx;
  border-radius: 40rpx;
  font-size: 30rpx;
}
</style>
