<template>
  <view class="inspect-result-container">
    <!-- 顶部导航栏 -->
    <view class="nav-bar">
      <view class="back" @tap="goBack">
        <uni-icons type="left" size="20" color="#222" />
      </view>
      <text class="nav-title">步骤二：合格质检</text>
      <view class="nav-icons">
        <uni-icons type="more" size="24" color="#222" />
      </view>
    </view>
    <view class="main-content">
      <!-- 合格产品卡片 -->
      <view class="result-card">
        <view class="card-title">合格产品</view>
        <view v-for="item in qualifiedList" :key="item.id" class="result-row">
          <view class="row-main">
            <text class="row-name">{{item.name}}</text>
            <text class="row-price">¥ {{item.price}} <text class="row-unit">/件</text></text>
            <text class="row-count">x{{item.count}}</text>
            <text class="row-total">¥{{item.total}}</text>
          </view>
          <view class="row-reason">
            <text class="reason-label">理由填写</text>
            <input class="reason-input" v-model="item.reason" placeholder="请选择理由（选填）" />
          </view>
        </view>
      </view>
      <!-- 不合格产品卡片 -->
      <view class="result-card">
        <view class="card-title">不合格产品</view>
        <view v-for="group in unqualifiedGroups" :key="group.name" class="result-group">
          <view class="row-main">
            <text class="row-name">{{group.name}}</text>
            <text class="row-price">¥ {{group.price}} <text class="row-unit">/件</text></text>
            <text class="row-total">¥{{group.total}}</text>
          </view>
          <view v-for="item in group.items" :key="item.id" class="row-reason">
            <text class="reason-label">{{item.name}}</text>
            <view class="reason-select" @tap="selectReason(item)">
              <text class="reason-placeholder">请选择理由</text>
              <uni-icons type="right" size="18" color="#bbb" />
            </view>
          </view>
        </view>
      </view>
      <!-- 不可回收产品卡片 -->
      <view class="result-card">
        <view class="card-title">不可回收产品</view>
        <view v-for="item in unrecyclableList" :key="item.id" class="result-row">
          <view class="row-main">
            <text class="row-name">{{item.name}}</text>
            <text class="row-price">¥ {{item.price || '—'}} <text class="row-unit">/件</text></text>
            <text class="row-count">x{{item.count}}</text>
            <text class="row-total">¥{{item.total}}</text>
          </view>
          <view class="row-reason">
            <text class="reason-label">理由填写</text>
            <input class="reason-input" v-model="item.reason" placeholder="请选择理由（选填）" />
          </view>
        </view>
      </view>
      <!-- 回收信息卡片 -->
      <view class="result-card info-card">
        <view class="card-title-row">
          <text class="card-title">回收信息</text>
          <view class="status-tag">待质检</view>
        </view>
        <view class="info-row">
          <text class="info-label">订单编号</text>
          <text class="info-value copy-btn">RE82738127861525 复制</text>
        </view>
        <view class="info-row">
          <text class="info-label">取件时间</text>
          <text class="info-value">2025-03-20 11:00</text>
        </view>
      </view>
    </view>
    <!-- 底部操作按钮 -->
    <view class="footer-btns">
      <button class="btn-outline" @tap="goPrev">上一步</button>
      <button class="btn-main" @tap="finishInspect">完成质检</button>
    </view>
    <!-- 理由弹窗 -->
    <uni-popup ref="reasonPopup" type="bottom" :mask-click="false">
      <view class="reason-popup">
        <view class="popup-header">
          <text class="popup-close" @tap="closeReasonPopup">关闭</text>
          <text class="popup-title">{{ currentReasonTitle }}</text>
        </view>
        <view class="popup-section">
          <text class="section-label">上传图片</text>
          <view class="img-list">
            <view v-for="(img, idx) in reasonImages" :key="idx" class="img-item">
              <image :src="img" class="img" />
              <view class="img-del" @tap="removeReasonImg(idx)">×</view>
            </view>
            <view class="img-item add" @tap="addReasonImg">
              <uni-icons type="plusempty" size="40" color="#bbb" />
            </view>
          </view>
        </view>
        <view class="popup-section">
          <text class="section-label">选择理由</text>
          <view v-for="(item, idx) in reasonOptions" :key="item" class="reason-row" @tap="toggleReason(idx)">
            <view :class="['checkbox', {checked: reasonChecked[idx]}]" />
            <text class="reason-text">{{ item }}</text>
          </view>
        </view>
        <button class="popup-save-btn" @tap="saveReason">保存</button>
      </view>
    </uni-popup>
  </view>
</template>

<script>
import pullRefreshMixin from '../mixins/pullRefreshMixin.js'
export default {
  mixins: [pullRefreshMixin],
  data() {
    return {
      qualifiedList: [
        { id: 1, name: '羽绒服', price: 8, count: 8, total: 64, reason: '' }
      ],
      unqualifiedGroups: [
        {
          name: '羽绒服', price: 8, total: 0,
          items: [
            { id: 11, name: '羽绒服1', reason: '' },
            { id: 12, name: '羽绒服2', reason: '' },
            { id: 13, name: '羽绒服3', reason: '' },
            { id: 14, name: '羽绒服4', reason: '' },
            { id: 15, name: '羽绒服5', reason: '' },
            { id: 16, name: '羽绒服6', reason: '' },
            { id: 17, name: '羽绒服7', reason: '' },
            { id: 18, name: '羽绒服8', reason: '' }
          ]
        },
        {
          name: '品牌服饰', price: 8, total: 0,
          items: [
            { id: 21, name: '品牌服饰1', reason: '' }
          ]
        }
      ],
      unrecyclableList: [
        { id: 31, name: '毛衣', price: '', count: 8, total: 0, reason: '' }
      ],
      reasonPopupVisible: false,
      currentReasonTitle: '',
      reasonImages: [],
      reasonOptions: ['大面积破损', '不可回收', '顽固污渍', '污渍无法清除', '异味严重', '带有危险物品'],
      reasonChecked: [false, true, false, true, false, false],
      currentReasonItem: null
    }
  },
  methods: {
    goBack() {
      uni.navigateBack()
    },
    goPrev() {
      uni.navigateBack()
    },
    finishInspect() {
      uni.showToast({ title: '完成质检', icon: 'success' })
    },
    selectReason(item) {
      this.currentReasonItem = item
      this.currentReasonTitle = item.name
      this.reasonImages = item.images || []
      this.reasonChecked = item.reasons || Array(this.reasonOptions.length).fill(false)
      this.$refs.reasonPopup.open()
    },
    closeReasonPopup() {
      this.$refs.reasonPopup.close()
    },
    addReasonImg() {
      uni.chooseImage({
        count: 3 - this.reasonImages.length,
        success: res => {
          this.reasonImages = this.reasonImages.concat(res.tempFilePaths)
        }
      })
    },
    removeReasonImg(idx) {
      this.reasonImages.splice(idx, 1)
    },
    toggleReason(idx) {
      this.$set(this.reasonChecked, idx, !this.reasonChecked[idx])
    },
    saveReason() {
      // 保存到当前item
      if (this.currentReasonItem) {
        this.currentReasonItem.images = [...this.reasonImages]
        this.currentReasonItem.reasons = [...this.reasonChecked]
      }
      this.closeReasonPopup()
    },
    async onRefresh() {
      await this.refreshData && this.refreshData()
    },
    refreshData() {
      // 可根据实际需求刷新质检结果数据
      // 例如重新请求接口或重置数据
    }
  }
}
</script>

<style lang="scss" scoped>
.inspect-result-container {
  min-height: 100vh;
  background: #f8f8f8;
  display: flex;
  flex-direction: column;
}
.nav-bar {
  display: flex;
  align-items: center;
  height: calc(150rpx + var(--status-bar-height));
  padding: 0 32rpx;
  padding-top: var(--status-bar-height);
  background: #fff;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 999;
  box-sizing: border-box;
  box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.03);
  .back {
    padding: 20rpx;
    margin-left: -20rpx;
  }
  .nav-title {
    flex: 1;
    text-align: center;
    font-size: 32rpx;
    font-weight: 500;
    color: #222;
  }
  .nav-icons {
    display: flex;
    align-items: center;
    gap: 12px;
  }
}
.main-content {
  margin-top: calc(150rpx + var(--status-bar-height));
  display: flex;
  flex-direction: column;
  background: none;
  padding-bottom: 120px;
}
.result-card {
  background: #fff;
  border-radius: 24px;
  box-shadow: 0 4px 24px rgba(0,0,0,0.06);
  margin: 0 24px 24px 24px;
  padding: 24px 24px 0 24px;
}
.card-title {
  font-size: 18px;
  font-weight: bold;
  color: #222;
  margin-bottom: 18px;
}
.card-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
}
.status-tag {
  background: #fff7e6;
  color: #ffb400;
  font-size: 14px;
  border-radius: 12px;
  padding: 2px 14px;
  font-weight: 400;
  height: 22px;
  display: flex;
  align-items: center;
}
.result-row, .result-group {
  margin-bottom: 18px;
}
.row-main {
  display: flex;
  align-items: center;
  .row-name {
    font-size: 16px;
    font-weight: bold;
    color: #222;
    margin-right: 8px;
  }
  .row-price {
    font-size: 15px;
    color: #ffb400;
    font-weight: bold;
    margin-right: 8px;
    .row-unit {
      font-size: 13px;
      color: #bbb;
    }
  }
  .row-count {
    font-size: 15px;
    color: #888;
    margin-right: 8px;
  }
  .row-total {
    font-size: 16px;
    color: #222;
    font-weight: bold;
    margin-left: auto;
  }
}
.row-reason {
  display: flex;
  align-items: center;
  margin-top: 8px;
  .reason-label {
    font-size: 15px;
    color: #bbb;
    min-width: 80px;
  }
  .reason-input {
    flex: 1;
    height: 40px;
    border-radius: 12px;
    background: #f6f6f6;
    border: none;
    font-size: 16px;
    color: #222;
    padding-left: 12px;
    margin-left: 8px;
  }
  .reason-select {
    flex: 1;
    display: flex;
    align-items: center;
    height: 40px;
    border-radius: 12px;
    background: #f6f6f6;
    font-size: 16px;
    color: #bbb;
    padding-left: 12px;
    margin-left: 8px;
    justify-content: space-between;
  }
  .reason-placeholder {
    color: #bbb;
    font-size: 16px;
  }
}
.info-card {
  background: #fff;
  border-radius: 24px;
  box-shadow: 0 4px 24px rgba(0,0,0,0.06);
  margin: 0 24px 24px 24px;
  padding: 24px 24px 0 24px;
}
.info-row {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
  .info-label {
    font-size: 15px;
    color: #bbb;
    min-width: 80px;
  }
  .info-value {
    font-size: 15px;
    color: #222;
    margin-left: 8px;
  }
  .copy-btn {
    color: #ffb400;
    margin-left: 8px;
  }
}
.footer-btns {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: #fff;
  display: flex;
  gap: 16px;
  padding: 12px 16px 24px 16px;
  z-index: 101;
  .btn-outline {
    flex: 1;
    height: 40px;
    border-radius: 16px;
    border: 1px solid #ffe09a;
    color: #ffb400;
    background: #fff0d2;
    font-size: 15px;
    font-weight: 500;
    box-shadow: none;
    padding: 0 18px;
  }
  .btn-main {
    flex: 1;
    height: 40px;
    border-radius: 16px;
    background: linear-gradient(90deg, #ffd01e 0%, #ffac04 100%);
    color: #fff;
    border: none;
    font-size: 15px;
    font-weight: 500;
    box-shadow: none;
    padding: 0 18px;
  }
}
.reason-popup {
  background: #fff;
  border-radius: 32px 32px 0 0;
  padding: 0 0 24px 0;
  .popup-header {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 56px;
    border-bottom: 1px solid #f5f5f5;
    position: relative;
    .popup-close {
      position: absolute;
      left: 24px;
      color: #888;
      font-size: 18px;
    }
    .popup-title {
      font-size: 20px;
      font-weight: bold;
      color: #222;
    }
  }
  .popup-section {
    padding: 24px 24px 0 24px;
    .section-label {
      font-size: 16px;
      color: #888;
      margin-bottom: 12px;
      display: block;
    }
    .img-list {
      display: flex;
      gap: 12px;
      margin-bottom: 16px;
      .img-item {
        width: 80px;
        height: 80px;
        border-radius: 16px;
        background: #f6f6f6;
        position: relative;
        .img {
          width: 100%;
          height: 100%;
          border-radius: 16px;
        }
        .img-del {
          position: absolute;
          top: 4px;
          right: 4px;
          width: 20px;
          height: 20px;
          background: rgba(0,0,0,0.3);
          color: #fff;
          border-radius: 50%;
          text-align: center;
          line-height: 20px;
          font-size: 16px;
        }
        &.add {
          display: flex;
          align-items: center;
          justify-content: center;
          background: #f6f6f6;
          color: #bbb;
        }
      }
    }
    .reason-row {
      display: flex;
      align-items: center;
      padding: 18px 0;
      border-bottom: 1px solid #f5f5f5;
      .checkbox {
        width: 22px;
        height: 22px;
        border-radius: 6px;
        border: 2px solid #ddd;
        margin-right: 16px;
        background: #fff;
        &.checked {
          border-color: #ffb400;
          background: #ffb400;
        }
      }
      .reason-text {
        font-size: 16px;
        color: #222;
      }
    }
  }
  .popup-save-btn {
    width: 90%;
    margin: 32px auto 0 auto;
    height: 52px;
    border-radius: 26px;
    background: linear-gradient(90deg, #ffd01e 0%, #ffac04 100%);
    color: #fff;
    font-size: 20px;
    font-weight: bold;
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
</style> 