<template>
  <scroll-view scroll-y class="page">
    <view class="card">
      <view class="title">扫码查看信息</view>
      <view class="desc">请扫描资产二维码。二维码内容为资产编号 assetSn，我们会自动跳转到资产详情页。</view>
      <button class="scan-btn" @tap="startScan">开始扫码</button>
      <view v-if="lastContent" class="last">最近一次扫描内容：{{ lastContent }}</view>
      <view class="tips">如扫码失败，可点击上方按钮重试。</view>
    </view>
  </scroll-view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'

const lastContent = ref('')

function extractAssetSn(raw: string): string | null {
  if (!raw) return null
  const text = String(raw).trim()
  // 优先匹配 URL 中的 assetSn 参数
  const m = text.match(/[?#&]assetSn=([^&#]+)/i)
  if (m && m[1]) {
    try {
      return decodeURIComponent(m[1])
    } catch (_) {
      return m[1]
    }
  }
  // 否则直接当作 assetSn
  return text || null
}

function toDetail(assetSn: string) {
  if (!assetSn) {
    uni.showToast({ title: '二维码无效', icon: 'none' })
    return
  }
  uni.navigateTo({ url: `/pages/fixed-assets/detail?assetSn=${encodeURIComponent(assetSn)}` })
}

// @ts-ignore
import jzH5ScanCode from '@/uni_modules/jz-h5-scanCode/js/index.js'

function startScan() {
  // 导入插件
  // #ifdef WEB
  jzH5ScanCode.scanCode({
    success: (res: any) => {
      console.log('扫码成功:', res.result)
      const content = res.result
      lastContent.value = content || ''
      const sn = extractAssetSn(content)
      if (sn) {
        toDetail(sn)
      } else {
        uni.showToast({ title: '未识别到资产编号', icon: 'none' })
      }

    },
    fail: (res: any) => {
      console.log('扫码失败:', res.errMsg)
      uni.showToast({ title: '扫码已取消，' + res.errMsg, icon: 'none' })
    }
  })
  // #endif

  // #ifndef WEB
  uni.scanCode({
    scanType: ['qrCode'],
    autoZoom: false,

    success: (res) => {
      const content = (res as any)?.result || (res as any)?.path || ''
      lastContent.value = content || ''
      const sn = extractAssetSn(content)
      if (sn) {
        toDetail(sn)
      } else {
        uni.showToast({ title: '未识别到资产编号', icon: 'none' })
      }
    },
    fail: (e) => {
      uni.showToast({ title: '扫码已取消，' + JSON.stringify(e), icon: 'none' })
    },
    complete: () => {
      console.log(1);

      // 扫码完成
    }
  })
  // #endif
}

/* onMounted(() => {
  // 进入页面即触发一次扫码，便于快速使用
  startScan()
}) */
</script>

<style scoped>
.page {
  min-height: 100vh;
  padding: 24rpx;
  background: #f5f7fb;
  box-sizing: border-box;
}

.card {
  background: #fff;
  border-radius: 16rpx;
  padding: 28rpx;
  box-shadow: 0 6rpx 16rpx rgba(0, 0, 0, 0.06);
}

.title {
  font-size: 34rpx;
  font-weight: 600;
  color: #222;
  margin-bottom: 10rpx;
}

.desc {
  font-size: 26rpx;
  color: #666;
  margin-bottom: 24rpx;
  line-height: 1.6;
}

.scan-btn {
  background: #4b7aff;
  color: #fff;
  border-radius: 12rpx;
  padding: 0;
  font-size: 28rpx;
}

.last {
  margin-top: 16rpx;
  color: #444;
  font-size: 24rpx;
  word-break: break-all;
}

.tips {
  margin-top: 8rpx;
  color: #9a9aa0;
  font-size: 22rpx;
}
</style>
