<template>
  <view class="help-center-container">
    <!-- 顶部导航栏 -->
    <view class="header-bar">
      <view class="nav-left" @click="goBack">
        <text class="nav-icon">←</text>
      </view>
      <text class="page-title">帮助中心</text>
      <view class="nav-right"></view>
    </view>

    <!-- 搜索框 -->
    <view class="search-bar">
      <view class="search-input-wrapper">
        <text class="search-icon">🔍</text>
        <input 
          type="text" 
          placeholder="搜索常见问题..." 
          v-model="searchQuery"
          @input="onSearch"
          class="search-input"
        />
      </view>
    </view>

    <!-- 分类导航 -->
    <view class="category-tabs">
      <scroll-view scroll-x="true" class="tabs-scroll">
        <view 
          v-for="cat in categories" 
          :key="cat.id"
          class="tab-item"
          :class="{ active: activeCategory === cat.id }"
          @click="switchCategory(cat.id)"
        >
          {{ cat.name }}
        </view>
      </scroll-view>
    </view>

    <!-- 内容区域 -->
    <view class="content-wrapper">
      <!-- 筛选标签（仅在搜索时显示） -->
      <view v-if="searchQuery" class="filter-tags">
        <text class="tag">搜索结果：{{ searchResults.length }} 条</text>
        <text class="clear-btn" @click="clearSearch">清除</text>
      </view>

      <!-- 常见问题列表 -->
      <view v-if="currentQuestions.length > 0">
        <view v-for="item in currentQuestions" :key="item.id" class="question-item">
          <view class="question-header" @click="toggleExpand(item)">
            <text class="question-text">{{ item.title }}</text>
            <text class="expand-icon">{{ item.expanded ? '▲' : '▼' }}</text>
          </view>
          <view v-if="item.expanded" class="answer-content">
            <text class="answer-text">{{ item.content }}</text>
          </view>
        </view>
      </view>

      <!-- 无结果提示 -->
      <view v-else class="no-result">
        <text class="no-result-icon">❓</text>
        <text class="no-result-title">暂无匹配结果</text>
        <text class="no-result-desc">请尝试更换关键词，或查看以下热门问题</text>
        <view class="hot-questions">
          <text class="hot-tag">热门问题：</text>
          <text class="hot-item" @click="jumpToQuestion('q1')">如何接单？</text>
          <text class="hot-item" @click="jumpToQuestion('q2')">配送超时怎么办？</text>
          <text class="hot-item" @click="jumpToQuestion('q3')">如何联系客户？</text>
        </view>
      </view>
    </view>

    <!-- 底部联系客服 -->
    <view class="contact-footer">
      <view class="contact-card">
        <text class="contact-title">需要人工帮助？</text>
        <text class="contact-desc">7×24小时在线客服，为您解决配送中遇到的任何问题</text>
        <button class="contact-btn" @click="contactCustomerService">联系客服</button>
      </view>
    </view>
  </view>
</template>

<script lang="uts">
export default {
  data() {
    return {
      searchQuery: '',
      activeCategory: 'all',
      categories: [
        { id: 'all', name: '全部' },
        { id: 'order', name: '订单相关' },
        { id: 'delivery', name: '配送操作' },
        { id: 'account', name: '账号与认证' },
        { id: 'payment', name: '收入与结算' },
        { id: 'vehicle', name: '车辆管理' }
      ],
      questions: [
        {
          id: 'q1',
          title: '如何接单？',
          content: '1. 进入【任务列表】页面\n2. 查看“待接单”任务\n3. 点击任务卡片 → 点击【接受订单】按钮\n4. 系统将自动分配该订单给您，状态变为“已接单”\n⚠️ 注意：同一时间只能接1个订单，接单后需在15分钟内取货。',
          category: ['order', 'delivery'],
          expanded: false
        },
        {
          id: 'q2',
          title: '配送超时怎么办？',
          content: '若因交通堵塞、客户联系不上等不可抗力导致超时，请在送达后点击【异常上报】→ 选择原因并上传凭证（如截图、通话记录），系统将审核后减免处罚。建议提前10分钟联系客户确认收货时间。',
          category: ['delivery'],
          expanded: false
        },
        {
          id: 'q3',
          title: '如何联系客户？',
          content: '在【任务详情】页点击【联系客户】→ 选择【拨打电话】或【发送短信】。系统会隐藏真实号码，保护双方隐私。首次联系建议使用语音电话，确保沟通效率。',
          category: ['delivery', 'order'],
          expanded: false
        },
        {
          id: 'q4',
          title: '如何修改个人信息？',
          content: '进入【个人中心】→ 点击头像 → 【编辑资料】→ 修改姓名/电话/身份证号等信息 → 提交后等待审核（通常2小时内完成）。注意：身份证信息需上传清晰照片。',
          category: ['account'],
          expanded: false
        },
        {
          id: 'q5',
          title: '配送费如何计算？',
          content: '基础配送费 = 距离费（¥1.5/km） + 时间费（¥0.5/分钟） + 服务费（¥2~5，根据时段浮动）\n例如：3km + 15分钟 = 3×1.5 + 15×0.5 + 3 = ¥15\n实际金额以订单详情页为准。',
          category: ['payment'],
          expanded: false
        },
        {
          id: 'q6',
          title: '车辆信息如何绑定？',
          content: '【个人中心】→ 【车辆管理】→ 【添加车辆】→ 输入车牌号、车型、行驶证照片 → 提交审核。审核通过后，系统将自动关联您的配送任务。未绑定车辆无法接单。',
          category: ['vehicle'],
          expanded: false
        },
        {
          id: 'q7',
          title: '如何查看收入明细？',
          content: '【个人中心】→ 【收入明细】→ 可按日/周/月筛选 → 查看每笔订单的配送费、奖励、扣款及到账时间。支持导出Excel报表（需企业版权限）。',
          category: ['payment'],
          expanded: false
        },
        {
          id: 'q8',
          title: '任务被取消了怎么办？',
          content: '若客户取消订单，您将收到系统通知。已完成取货的订单，可申请补偿（路径：任务详情 → 【申请补偿】）；未取货的订单无补偿。每月最多3次无责取消不计入考核。',
          category: ['order'],
          expanded: false
        },
        {
          id: 'q9',
          title: '导航功能不准确？',
          content: '请检查：① 手机GPS权限是否开启；② 是否使用最新版APP；③ 在【设置】→ 【地图偏好】中切换高德/百度地图。仍无效请反馈至客服，并提供截图+坐标。',
          category: ['delivery'],
          expanded: false
        },
        {
          id: 'q10',
          title: '如何提升评分？',
          content: '评分由客户评价（70%）+ 系统考核（30%）组成。建议：① 准时送达（≤预计时间）；② 礼貌沟通；③ 主动拍照上传（取货/送达）；④ 避免频繁拒单。当前平均分：4.8/5.0',
          category: ['delivery', 'account'],
          expanded: false
        }
      ]
    }
  },
  computed: {
    currentQuestions() {
      if (this.searchQuery) {
        return this.searchResults
      }
      return this.questions.filter(q => 
        this.activeCategory === 'all' || q.category.includes(this.activeCategory)
      )
    },
    searchResults() {
      const query = this.searchQuery.toLowerCase()
      return this.questions.filter(q => 
        q.title.toLowerCase().includes(query) || 
        q.content.toLowerCase().includes(query)
      )
    }
  },
  methods: {
    goBack() {
      uni.navigateBack()
    },

    onSearch() {
      // 实时搜索（可加防抖）
      if (!this.searchQuery) {
        this.activeCategory = 'all'
      }
    },

    clearSearch() {
      this.searchQuery = ''
      this.activeCategory = 'all'
    },

    switchCategory(categoryId) {
      this.activeCategory = categoryId
      this.searchQuery = ''
    },

    toggleExpand(item) {
      item.expanded = !item.expanded
      // 关闭其他展开项（可选）
      // this.questions.forEach(q => { if (q !== item) q.expanded = false })
    },

    jumpToQuestion(id) {
      const item = this.questions.find(q => q.id === id)
      if (item) {
        item.expanded = true
        this.activeCategory = 'all'
        this.searchQuery = ''
        // 滚动到该问题（简化版）
        uni.pageScrollTo({ scrollTop: 300, duration: 300 })
      }
    },

    contactCustomerService() {
      uni.showActionSheet({
        itemList: ['在线客服', '电话咨询', '提交工单'],
        success: (res) => {
          if (res.tapIndex === 0) {
            uni.navigateTo({ url: '/pages/mall/common/customer-service' })
          } else if (res.tapIndex === 1) {
            uni.makePhoneCall({ phoneNumber: '400-123-4567' })
          } else {
            uni.navigateTo({ url: '/pages/mall/common/feedback?from=help' })
          }
        }
      })
    }
  }
}
</script>

<style scoped>
.help-center-container {
  background-color: #f8f9fa;
  min-height: 100vh;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

/* 顶部导航 */
.header-bar {
  background-color: #fff;
  padding: 20rpx 30rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1rpx solid #e9ecef;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
}

.nav-left {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 10rpx;
  border-radius: 8rpx;
  transition: background-color 0.2s ease;
}

.nav-left:hover {
  background-color: #f0f0f0;
}

.nav-left:active {
  background-color: #e0e0e0;
}

.nav-icon {
  font-size: 36rpx;
  color: #333;
}

.page-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  text-align: center;
  flex: 1;
}

.nav-right {
  width: 60rpx;
  height: 1rpx;
}

/* 搜索框 —— 修复放大镜与输入框垂直对齐 */
.search-bar {
  padding: 20rpx 30rpx;
  background-color: #fff;
}

.search-input-wrapper {
  display: flex;
  align-items: center; /* ✅ 垂直居中核心 */
  background-color: #f5f5f5;
  border-radius: 40rpx;
  padding: 12rpx 20rpx;
}

.search-icon {
  font-size: 28rpx;
  color: #999;
  margin-right: 12rpx;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  height: 48rpx; /* 与 input 高度一致 */
}

.search-input {
  flex: 1;
  font-size: 26rpx;
  background: transparent;
  outline: none;
  color: #333;
  height: 48rpx;
  padding: 0;
}

/* 分类导航 */
.category-tabs {
  background-color: #fff;
  padding: 0 30rpx 20rpx;
}

.tabs-scroll {
  white-space: nowrap;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.tab-item {
  display: inline-block;
  padding: 12rpx 24rpx;
  margin-right: 16rpx;
  background-color: #f8f9fa;
  border-radius: 30rpx;
  font-size: 26rpx;
  color: #666;
  transition: all 0.3s;
}

.tab-item.active {
  background-color: #74b9ff;
  color: white;
  font-weight: 500;
}

/* 内容区域 */
.content-wrapper {
  padding: 0 30rpx;
  flex: 1;
}

.filter-tags {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15rpx 0;
  background-color: #e8f4fd;
  border-radius: 12rpx;
  margin-bottom: 20rpx;
}

.tag {
  font-size: 24rpx;
  color: #333;
}

.clear-btn {
  font-size: 24rpx;
  color: #74b9ff;
  padding: 4rpx 12rpx;
  border-radius: 8rpx;
}

.question-item {
  background-color: #fff;
  border-radius: 16rpx;
  overflow: hidden;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.03);
}

.question-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 24rpx 20rpx;
  cursor: pointer;
  border-bottom: 1rpx solid #f0f0f0;
}

.question-text {
  font-size: 28rpx;
  color: #333;
  font-weight: 500;
  flex: 1;
}

.expand-icon {
  font-size: 32rpx;
  color: #999;
  width: 36rpx;
  text-align: center;
}

.answer-content {
  padding: 0 20rpx 20rpx;
  background-color: #f8f9fa;
}

.answer-text {
  font-size: 26rpx;
  color: #666;
  line-height: 1.6;
  white-space: pre-line;
}

/* 无结果提示 */
.no-result {
  text-align: center;
  padding: 80rpx 30rpx;
  background-color: #fff;
  border-radius: 16rpx;
  margin-top: 20rpx;
}

.no-result-icon {
  font-size: 80rpx;
  color: #e0e0e0;
  margin-bottom: 20rpx;
}

.no-result-title {
  font-size: 32rpx;
  color: #333;
  font-weight: bold;
  margin-bottom: 15rpx;
}

.no-result-desc {
  font-size: 26rpx;
  color: #666;
  margin-bottom: 25rpx;
}

.hot-questions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 12rpx;
}

.hot-tag {
  font-size: 24rpx;
  color: #999;
  margin-right: 12rpx;
}

.hot-item {
  font-size: 24rpx;
  color: #74b9ff;
  padding: 4rpx 12rpx;
  border-radius: 8rpx;
  background-color: #e8f4fd;
}

/* 底部联系客服 */
.contact-footer {
  padding: 30rpx;
  background-color: #fff;
  border-top: 1rpx solid #e9ecef;
}

.contact-card {
  background: linear-gradient(135deg, #74b9ff 0%, #0984e3 100%);
  border-radius: 20rpx;
  padding: 30rpx;
  color: white;
  text-align: center;
}

.contact-title {
  font-size: 32rpx;
  font-weight: bold;
  margin-bottom: 12rpx;
}

.contact-desc {
  font-size: 26rpx;
  opacity: 0.9;
  line-height: 1.5;
  margin-bottom: 20rpx;
}

.contact-btn {
  width: 100%;
  height: 80rpx;
  background-color: white;
  color: #74b9ff;
  font-size: 30rpx;
  font-weight: bold;
  border-radius: 40rpx;
  border: none;
  box-shadow: 0 6rpx 15rpx rgba(0, 0, 0, 0.1);
}
</style>