<template>
  <view class="page-container">
   
    <view class="page-body">
      <view class="form-section">
        <view class="form-group">
          <view class="form-item">
            <text class="form-label required">数字识别码</text>
            <view class="input-row">
              <input class="form-input" placeholder="请输入数字识别码" v-model="form.code" />
              <view class="input-extra">
                <button class="btn btn-sm btn-outline" @click="scanCode">扫码</button>
              </view>
            </view>
          </view>
          <view class="form-item">
            <text class="form-label required">撤销原因</text>
            <input class="form-input" placeholder="请输入撤销原因" v-model="form.reason" />
          </view>
        </view>
      </view>

      <view class="data-list">
        <view class="data-card">
          <view class="data-card-header">
            <text class="data-card-title">{{ (recordData.list && recordData.list[1]) || '' }}</text>
            <text class="data-card-tag red">待撤销</text>
          </view>
          <view class="data-card-row"><text>当前状态:</text><text class="val">{{ recordData.lx || '' }}</text></view>
          <view class="data-card-row"><text>数字识别码:</text><text class="val">{{ recordData.list && recordData.list[2]|| '' }}</text></view>
          <view class="data-card-row"><text>重量:</text><text class="val">{{ (recordData.list && recordData.list[0]) || '' }}</text></view>
          <view class="data-card-row"><text>登记时间:</text><text class="val">{{ (recordData.list && recordData.list[3]) || '' }}</text></view>
        </view>
      </view>

      <view class="tip-bar tip-bar-danger">
        ⚠️ 撤销操作不可逆，请谨慎操作！
      </view>

      <view class="btn-area">
        <button class="btn btn-outline" @click="handleQuery">查询</button>
        <button class="btn btn-danger" @click="handleUndo" :disabled="!canUndo">确认撤销</button>
      </view>
    </view>
  </view>
</template>

<script>
import { api } from '@/api/index'
import scanMixin from '@/utils/barcodeMixin.js'

export default {
  mixins: [scanMixin],
  data() {
    return {
      form: { code: '', reason: '', type: 'auto' },
      recordData: {}
    }
  },
  computed: {
    canUndo() {
      return this.recordData.list && this.recordData.list.length && this.form.reason
    }
  },
  methods: {
    /** 扫码结果回调：填充数字识别码 -> 自动查询 */
    onBarcodeScanned(barcode) {
      this.form.code = barcode
      uni.showToast({ title: '扫码成功 ' + barcode, icon: 'none' })
      this.handleQuery()
    },

    // ==================== 原有方法 ====================
    handleQuery() {
      if (!this.form.code) return uni.showToast({ title: '请输入数字识别码', icon: 'none' })
      api.scanGet({ code: this.form.code }).then(res => {
        this.recordData = res.data
        this.form.reason = ''
        uni.showToast({ title: '查询成功', icon: 'success' })
      }).catch(() => {
        this.recordData = {}
      })
    },
    handleUndo() {
      if (!this.canUndo) return
      uni.showModal({
        title: '确认撤销',
        content: '确定要撤销这条记录吗？',
        success: (res) => {
          if (res.confirm) {
            api.undo({
              code: this.form.code,
              reason: this.form.reason,
              type: this.form.type
            }).then(res => {
              uni.showToast({ title: '撤销成功', icon: 'success' })
              setTimeout(() => { this.recordData = {}; this.form = { code: this.form.code, reason: '', type: 'auto' } }, 1500)
            }).catch(() => {
              // request.js 已处理 loading 和错误提示
            })
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
