<template>
  <navbar-back title="样品下架"></navbar-back>
  <view class="pl8 pr8">
    <view class="border-b p8 x-f"
      ><view class="pr16">下架人：</view><text>{{ userInfo.nickname }}</text></view
    >
    <up-radio-group class="mt10" v-model="takeOffType" size="20px" @change="handleTakeOffType">
      <up-radio
        :customStyle="{ marginLeft: '8px' }"
        v-for="(item, index) in takeOffTypeOptions"
        :key="index"
        :label="item.label"
        :name="item.name"
      >
      </up-radio>
    </up-radio-group>
    <up-input
      class="mt10"
      v-model="targetCode"
      placeholder="请扫描需要下架的样品编号"
      prefixIcon="scan"
      fontSize="16"
      prefixIconStyle="font-size: 30px;"
      @confirm="getSampleList"
    >
    </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 :loading="btnLoading" type="primary" style="width: 50%" text="提交" @click="handleSubmit"></up-button>
    </uni-section>
  </view>
</template>

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

const takeOffType = ref('sample')
const takeOffTypeOptions = reactive([
  {
    name: 'sample',
    label: '按样品下架'
  },
  {
    name: 'warehouseLocation',
    label: '按库位下架'
  }
])
function handleTakeOffType() {
  handleReset()
}
let targetCode = ref('')
let sampleList = ref([])
const btnLoading = ref(false)

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

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/sampleTakeOff/index') return
  try {
    const isJson = nx.$helper.isJsonString(val)
    const isSample = takeOffType.value === 'sample'
    if (isJson) {
      if (isSample) {
        return uni.showToast({
          title: '请扫描正确的样品编号',
          icon: 'none'
        })
      }
      const codeObj = JSON.parse(val)
      targetCode.value = codeObj.code
    } else {
      if (isSample) {
        targetCode.value = val
      } else {
        return uni.showToast({
          title: '请扫描正确库位码',
          icon: 'none'
        })
      }
    }
    getSampleList()
  } catch (error) {
    uni.showToast({
      title: '扫码内容解析失败',
      icon: 'none'
    })
  }
}, 300)
onShow(() => {
  scanQRInfo.value = ''
})

async function getSampleList() {
  if (targetCode.value === '') return
  let params = {
    pageSize: 999,
    pageNo: 1,
    returnStatus: 'completed',
    dispatchStatus: '0',
    sampleFlowNodeKey: 'flw_sample_storage'
  }
  if (takeOffType.value === 'sample') {
    params.sampleReturnCode = targetCode.value
  } else {
    params.warehouseLocationCode = targetCode.value
  }
  sampleList.value = []
  const { list } = await nx.$api.sampleWarehouse.queryReturnToStockSample(params)
  if (list.length === 0) {
    return uni.showToast({ title: '未查询到该样品信息', icon: 'none' })
  }
  const existingCodes = new Set(sampleList.value.map(item => item.id)) // 假设唯一标识是 `code`
  const newItems = list.filter(item => !existingCodes.has(item.id))

  if (newItems.length === 0) {
    return uni.showToast({ title: '该样品已存在，无需重复添加', icon: 'none' })
  }

  sampleList.value.push(...newItems)
}
async function handleSubmit() {
  let params = { actionWay: takeOffType.value }
  if (takeOffType.value === 'warehouseLocation') {
    params.locationCode = targetCode.value
  } else {
    params.sampleReturnCodes = sampleList.value.map(item => item.sampleReturnCode).join(',')
  }
  btnLoading.value = true
  await nx.$api.sampleWarehouse.execTakeOff(params).finally(() => {
    btnLoading.value = false
  })
  uni.showToast({
    title: '下架成功',
    icon: 'none'
  })
  handleReset()
}
function handleReset() {
  targetCode.value = ''
  sampleList.value = []
}
</script>

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