<template>
  <view class="p4 pt8">
    <navbar-back title="样品查询"></navbar-back>
    <up-input
      v-model="sampleCode"
      placeholder="请扫描或者输入样品编号"
      prefixIcon="scan"
      fontSize="16"
      prefixIconStyle="font-size: 30px;"
      @confirm="handleSearch"
    >
      <template #suffix>
        <!-- <up-button type="primary" text="查询" icon="search"></up-button> -->
        <!-- <up-icon size="20" name="search" @click="handleSearch"></up-icon> -->
      </template>
    </up-input>

    <uni-section v-if="sampleData.id" type="line" title="样品详情" titleFontSize="15px">
      <uni-card>
        <view>
          <view class="x-bc">
            <view
              >样品名称：<text>{{ sampleData.sampleName }}</text></view
            >
            <up-tag
              v-if="sampleData.dispatchStatus == 1"
              plain
              plainFill
              size="mini"
              type="info"
              text="已调拨"
            ></up-tag>
            <up-tag
              v-else
              plain
              plainFill
              size="mini"
              :type="sampleData.returnStatus === 'completed' ? 'success' : 'info '"
              :text="sampleData.returnStatus_dictText"
            ></up-tag>
          </view>
          <view class="mt4">
            样品编号：<text>{{ sampleData.sampleCode }}</text>
          </view>
          <view class="mt4"
            >归库编码：<text>{{ sampleData.sampleReturnCode }}</text></view
          >
          <view class="mt4"
            >样品库名称：<text>{{ sampleData.warehouseName }}</text></view
          >
          <view class="mt4"
            >库位信息：<text>{{ sampleData.warehouseLocationCode }}</text></view
          >
        </view>
      </uni-card>
    </uni-section>
    <up-button
      v-if="sampleData.id"
      :disabled="sampleData.returnStatus !== 'completed'"
      :loading="btnLoading"
      type="primary"
      style="width: 90%"
      text="打印归库标签"
      @click="handlePrint"
    ></up-button>
  </view>
</template>

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

const { scanQRInfo } = toRefs(nx.$store('biz'))
watch(scanQRInfo, newVal => {
  debouncedHandleScan(newVal)
})
const debouncedHandleScan = debounce(val => {
  if (!val) return
  scanQRInfo.value = ''
  if (nx.$router.getCurrentPage().route !== 'pages/sampleWarehouse/sampleSearch/index') return
  try {
    sampleCode.value = val
    handleSearch()
  } catch (error) {
    uni.showToast({
      title: '请扫描正确的样品编码',
      icon: 'none'
    })
  }
}, 300)
onShow(() => {
  scanQRInfo.value = ''
})
let sampleCode = ref('')
function handleSearch() {
  getSampleDetail()
}

let sampleData = ref({})
async function getSampleDetail() {
  sampleData.value = {}
  const { list } = await nx.$api.sampleWarehouse.queryReturnToStockSample({
    sampleReturnCode: sampleCode.value,
    sampleFlowNodeKey: 'flw_sample_storage',
    pageSize: 10,
    pageNo: 1
  })
  if (list.length == 0) {
    uni.showToast({
      title: '未查询到该样品信息',
      icon: 'none'
    })
  } else if (list.length > 1) {
    uni.showToast({
      title: '查询出重复的样品编号,请联系管理员',
      icon: 'none'
    })
  } else {
    sampleData.value = list[0]
  }
}
let btnLoading = ref(false)
function handlePrint() {
  btnLoading.value = true
  sampleData.value.returnTime = nx.$dayjs(sampleData.value.returnTime).format('YYYY-MM-DD')
  nx.$print.print({ ...sampleData.value, printTemplate: 'returnLabel' })
  btnLoading.value = false
}
</script>

<style lang="scss" scoped>
text {
  color: #000;
}
</style>
