<template>
  <navbar-back title="样品调拨"></navbar-back>
  <view class="pl8 pr8 pt8">
    <view class="x-f">
      <text class="pl6">领取人：</text>
      <uni-data-select
        v-model="receiverId"
        :localdata="assignUserRange"
        placeholder="请选择样品领取人"
        :clear="false"
      ></uni-data-select>
    </view>
    <view class="x-f mt10 mb10" v-if="warehouseRange.length > 1">
      <text class="pl20">仓库：</text>
      <uni-data-select
        v-model="warehouseCode"
        :clear="false"
        :localdata="warehouseRange"
        placeholder="请选择仓库"
      ></uni-data-select>
    </view>
    <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;"
      @confirm="getScanSample"
    >
    </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: 44vh" 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 type="primary" :loading="btnLoading" style="width: 50%" text="提交" @click="handleSubmit"></up-button>
    </uni-section>
  </view>
</template>

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

const btnLoading = ref(false)
let sampleCode = ref('')
let sampleList = ref([])
const assignUserRange = ref([])
const userInfo = computed(() => nx.$store('user').userInfo)
const receiverId = ref('')
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/sampleDispatchInternal/index') return
  try {
    sampleCode.value = val
    getScanSample()
  } catch (error) {
    uni.showToast({
      title: '请扫描正确的样品编码',
      icon: 'none'
    })
  }
}, 300)
onShow(() => {
  scanQRInfo.value = ''
})

let dispatchTempId = ref('')
// 创建临时数据
async function createDispatchTempData() {
  const data = await nx.$api.sampleWarehouse.createDispatchTempData()
  dispatchTempId.value = data.id
}
async function getScanSample() {
  if (sampleCode.value === '') return
  await nx.$api.sampleWarehouse.addDispatchSample({
    id: dispatchTempId.value,
    sampleReturnCode: sampleCode.value,
    warehouseCode: warehouseCode.value
  })
  getSampleList()
}
async function getSampleList() {
  const { list } = await nx.$api.sampleWarehouse.querySampleDispatchApplyDetail({
    pageSize: 999,
    pageNo: 1,
    parentId: dispatchTempId.value
  })
  sampleList.value = list
}
async function handleSubmit() {
  if (receiverId.value === '') {
    return uni.showToast({
      title: '请选择样品领取人',
      icon: 'none'
    })
  }
  btnLoading.value = true
  const receiver = assignUserRange.value.find(item => item.value === receiverId.value)?.text
  await nx.$api.sampleWarehouse
    .execSampleDispatch({
      id: dispatchTempId.value,
      applyUserId: receiverId.value,
      applyUser: receiver,
      warehouseUser: userInfo.value.nickname,
      warehouseUserId: userInfo.value.id
    })
    .finally(() => {
      btnLoading.value = false
    })
  handleReset()
  setTimeout(() => {
    uni.showToast({
      title: '调拨成功',
      icon: 'none'
    })
  }, 200)
}
function handleReset() {
  receiverId.value = ''
  sampleCode.value = ''
  sampleList.value = []
  createDispatchTempData()
}
const warehouseRange = ref([])
const warehouseCode = ref('')
function getWarehouseRange() {
  nx.$api.sampleWarehouse.querySampleLocation({ warehouseType: '样品库' }).then(res => {
    warehouseRange.value = res.map(item => ({ text: item.name, value: item.code }))
    warehouseCode.value = res[0].code
  })
}
function getAssignUserRange() {
  nx.$api.user.getAssignUserList().then(res => {
    assignUserRange.value = res.map(item => ({ text: item.nickname, value: item.id }))
  })
}
onMounted(() => {
  getAssignUserRange()
  getWarehouseRange()
  handleReset()
})
</script>

<style lang="scss" scoped>
.sample-item {
  position: relative;
  pointer-events: none;
  .item-checkbox {
    position: absolute;
    right: 5px;
    top: 5px;
  }
}
:deep(.uni-select__input-placeholder) {
  font-size: 16px;
}
</style>
