<template>
  
  <view class="container">
    <view style="display: flex;align-items: center;justify-content: space-between;">
      <view>当前扫描到，共：{{ searchList.length }}条</view>
      <view>总条目：{{ scanTotal || '暂无' }}</view>
    </view>
    <uni-list style="margin-bottom: 80rpx;margin-top: 20rpx;">
      <uni-list-item v-for="item in searchList" :title="item"></uni-list-item>
    </uni-list>

    <view class="bottom">
      <button type="primary" v-show="!pandianStatus" @click="startScan">开始盘点</button>
      <button type="primary" v-show="pandianStatus" @click="stopInventory(1)">开始匹配</button>
      <button v-show="pandianStatus" style="margin-left: 20rpx;" @click="stopInventory(0)">取消盘点</button>
    </view>
    
  </view>
</template>

<script setup>

import { ref } from "vue";
import { onLoad, onUnload } from "@dcloudio/uni-app";
import { getHandMatch, getTaskCount } from "@/api/inventory"

const scanTotal = ref("")
const taskInfo = ref({})

const getTotal = () => {
  let obj = {
    taskId: taskInfo.value.id,
    showLoading: true
  }
  obj.sceneId = uni.getStorageSync("taskInfo").sceneId
  obj.warehouseCode = uni.getStorageSync("taskInfo").warehouseCode
  getTaskCount(obj).then(res => {
    scanTotal.value = res.data
  })
}

// 开始盘点
const startScan = () => {
  uni.showToast({
    title: '开始盘点',
    duration: 2000,
    icon: "none"
  });
  pandianStatus.value = true
  startInventory()
}



// 加载插件 获取 module 
var rfidManager = uni.requireNativePlugin("TH-PlatformRFID")

console.log("rfidManager", rfidManager)
// 扫码数组
const searchList = ref([]);
// 盘点状态对应显示按钮
const pandianStatus = ref(false)

// 页面
onLoad(() => {
  var ret = rfidManager.soundInit();//初始时插件中的声音播放资源，可按自己实际需求看是否调用，如果有自己的声音播放资源，可以不用这个
  //监听读取标签的数据回调
  rfidManager.onInventoryTag((result) => {
    if (!searchList.value.some(e => e == result.epc) && pandianStatus.value) {
      console.log(JSON.stringify(result))
      searchList.value.push(result.epc)
      //16进制的epc 转 ASCII 码字符串
      // var ascii = rfidManager.hexToASCII(result.epc);
      // var epc = ascii;

      rfidManager.soundPlay(1);
    }
  });
  taskInfo.value = uni.getStorageSync("taskInfo")
  getTotal()
  init()
})
onUnload(() => {
  rfidManager.soundRelease();
})

// 插件初始化
const init = function () {
  uni.showLoading({
    title: '正在初始化...',
    mask: true
  })
  rfidManager.init((ret) => {
    if (ret) {
      uni.hideLoading()
    } else {
      uni.showToast({
        title: ret,
        duration: 2000,
        icon: "none"
      });
    }
  });

}
// 开始盘点
const startInventory = function () {
  console.log('startInventory')
  var ret = rfidManager.startInventory(0);
}
// 结束盘点
const stopInventory = function (status) {
  console.log('stopInventory');
  rfidManager.stopInventory();
  if (status == 1) {
    let obj = {
      ids: searchList.value,
      taskId: taskInfo.value.id,
    }
    getHandMatch(obj).then(res => {
      console.log("res", res)
      scanTotal.value = ""
      pandianStatus.value = false
      searchList.value = []
      uni.redirectTo({
        url: '/pagesInventory/matchResult?taskId=' + taskInfo.value.id
      })
    })
  } else {
    searchList.value = []
    scanTotal.value = ""
    pandianStatus.value = false
  }
}

</script>

<style scoped lang="scss">
.container {
  position: relative;
  padding: 32rpx;

  .bottom {
    display: flex;
    align-items: center;
    position: fixed;
    bottom: 0;
    width: 100vw;
    padding: 10rpx 32rpx;
    left: 0;
    box-sizing: border-box;

    ::v-deep uni-button {
      flex: 1;
    }
  }
}
::v-deep .uni-select{
  border: unset;
  border-bottom: unset;
}

::v-deep .uv-popup .uv-popup__content {
  overflow: unset !important;
}
::v-deep .uv-modal {
  overflow: unset !important;
}
::v-deep .uv-input__content{
  flex-direction: column;
}
::v-deep .uni-select__input-placeholder{
  font-size: unset;
  color: #c0c4cc;
}
::v-deep .uni-select{
  padding: 0 !important;
}
</style>