<template>
  <navbar-back title="样品调拨"></navbar-back>
  <view class="pl8 pr8">
    <view class="border-b p6 x-f"
      ><view class="pr16">库管员：</view><text>{{ userInfo.nickname }}</text></view
    >
    <up-input
      style="padding-top: 20px"
      border="bottom"
      v-model="sampleCode"
      placeholder="请扫描需要调拨的样品编号"
      prefixIcon="scan"
      fontSize="16"
      prefixIconStyle="font-size: 30px;"
    >
    </up-input>

    <uni-section v-if="sampleList.length > 0" type="line" title="调拨样品明细" titleFontSize="15px">
      <template #right> <up-text type="error" size="18" bold :text="sampleList.length"></up-text></template>
      <scroll-view style="height: 49vh" scroll-y scroll-with-animation>
        <uni-card margin="5px" v-for="item in sampleList" class="sample-item">
          <view
            >样品名称：<text class="black">{{ item.sampleName }}</text></view
          >
          <view class="mt4"
            >归库编码：<text class="black">{{ item.sampleReturnCode }}</text></view
          >
          <view class="mt4"
            >样品库名称：<text class="black">{{ item.warehouseName }}</text></view
          >
          <view class="mt4"
            >库位码：<text class="black">{{ item.warehouseLocationCode }}</text></view
          >
        </uni-card>
      </scroll-view>
      <up-button :disabled="!receiver" type="primary" style="width: 50%" text="提交" @click="handleSubmit"></up-button>
    </uni-section>
  </view>
</template>

<script setup>
import { computed, ref, toRefs, watch } from 'vue'
import nx from '@/nx'
import { onLoad, onShow } from '@dcloudio/uni-app'

let sampleCode = ref('')
let sampleList = ref([])
let receiver = ref('')

const userInfo = computed(() => nx.$store('user').userInfo)

const { scanQRInfo } = toRefs(nx.$store('biz'))
watch(scanQRInfo, newVal => {
  if (!newVal) return
  scanQRInfo.value = ''
  if (nx.$router.getCurrentPage().route !== 'pages/sampleWarehouse/sampleDispatchExternal/detail') return
  try {
    sampleCode.value = newVal
  } catch (error) {
    uni.showToast({
      title: '请扫描正确的样品编码',
      icon: 'none'
    })
  }
})
onShow(() => {
  scanQRInfo.value = ''
})

async function handleSubmit() {
  await nx.$api.sampleWarehouse.execSampleDispatch({})
  uni.showToast({
    title: '调拨成功',
    icon: 'none'
  })
  uni.navigateBack()
}
</script>

<style lang="scss" scoped>
.sample-item {
  position: relative;
  pointer-events: none;
  .item-checkbox {
    position: absolute;
    right: 5px;
    top: 5px;
  }
}
</style>
