<template>
  <navbar-back title="物料开封"></navbar-back>
  <view class="pl8 pr8 pt8">
    <up-input
      style="padding-top: 20px"
      border="bottom"
      v-model="materialCode"
      placeholder="请扫描需要开封的物料编号"
      prefixIcon="scan"
      fontSize="16"
      prefixIconStyle="font-size: 30px;"
      @confirm="getMaterialList"
    >
    </up-input>
	<view class="x-f mt20" v-if="materialList.length > 0">
	  <text class="pl6">开封日期：</text>
	  <uni-datetime-picker type="date" return-type="timestamp" :clear-icon="false" v-model="operatorDate"  />
	</view>
    <uni-section v-if="materialList.length > 0" type="line" title="试剂信息" titleFontSize="15px">
      <!-- <template #right> <up-text type="error" size="18" bold :text="materialList.length"></up-text></template> -->
      <scroll-view style="height: 60vh" scroll-y scroll-with-animation>
        <uni-card margin="5px" v-for="item in materialList" class="sample-item">
          <view class="mt4">
            <view
              >物料编号：<text class="black">{{ item.code }}</text></view
            >
          </view>
          <view class="mt4">
            <view
              >物料名称：<text class="black">{{ item.name }}</text></view
            >
          </view>
          <view class="mt4"
            >规格型号：<text class="black">{{ item.modelNo }}</text></view
          >
          <view class="mt4"
            >保质期：<text class="black">{{ item.due }}</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 { debounce } from 'lodash'
import nx from '@/nx'
import { onLoad, onShow } from '@dcloudio/uni-app'

const btnLoading = ref(false)
let materialCode = ref('')
let materialList = ref([])
let operatorDate = 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/material/openMark/index') return
  try {
    materialCode.value = val
    getMaterialList()
  } catch (error) {
    uni.showToast({
      title: '请扫描正确的物料编码',
      icon: 'none'
    })
  }
}, 300)
onShow(() => {
  scanQRInfo.value = ''
})

async function getMaterialList() {
  if (materialCode.value === '') return
  let params = {
    code: materialCode.value,
    usageStatus: 1,
    openStatus:0,
	openDueFalg:1,
	businessType:'open_seal'
	
  }
  const data = await nx.$api.material.queryMaterialInfo(params)
  if (!data) return uni.showToast({ title: '未查询到可以开封的物料信息', icon: 'none' })
  const existingCodes = new Set(materialList.value.map(item => item.id))
  if (existingCodes.has(data.id)) {
    return uni.showToast({ title: '该物料已存在，无需重复扫描', icon: 'none' })
  }

  materialList.value.push(data)
}
async function handleSubmit() {
	if (operatorDate.value === '') {
	  return uni.showToast({
	    title: '请选择开封日期',
	    icon: 'none'
	  })
	}
  btnLoading.value = true
  let params = {
    businessType: '开封',
    operatorDate:operatorDate.value,
	detailList: materialList.value.map(item => ({infomationId:item.id}))
	
  }
  await nx.$api.material.addUseOver(params).finally(() => {
    btnLoading.value = false
  })
  uni.showToast({
    title: '开封成功',
    icon: 'none'
  })
  handleReset()
}
function handleReset() {
  materialCode.value = ''
  materialList.value = []
  operatorDate.value = ''
}
onMounted(async () => {})
</script>

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