<template>
	<view>
		<van-nav-bar
		  title="扫码入库"
		  left-text="返回"
		  left-arrow
		  right-text="扫一扫"
		  @click-left="onClickLeft"
		  @click-right="onClickRight"
		/>
		<view style="text-align: center;margin-top: 20px;">请扫描库位码</view>
		<cshaptx4869-scancode
		  v-if="h5ScanCode"
		  @success="handleSuccess"
		  @fail="handleFail"
		  @close="handleClose"
		></cshaptx4869-scancode>
	</view>
</template>

<script setup lang="ts">
import { showConfirmDialog, showToast } from 'vant';
import { onMounted, ref } from 'vue';
import { putAction } from '../../common/http';

onMounted(()=>{
})

const onClickLeft = () =>{
  uni.navigateBack()
}
const onClickRight=()=>{
	scanCode()
}
	const h5ScanCode = ref(false);
	function scanCode() {
	  // #ifdef H5
	  h5ScanCode.value = true;
	  // #endif
	  // #ifndef H5
	  uni.scanCode({
	    success: (res) => {
	      uni.showToast({
	        icon: "none",
	        title: res.result,
	      });
	    },
	    faile: (err) => {
	      console.log("err", err);
		  location.value=''
		  barcode.value=''
	    },
	  });
	  // #endif
	}
	const barcode = ref()
	const location = ref()
	function handleSuccess(res:any) {
	  h5ScanCode.value = false;
	  if(location.value){
		  barcode.value=res
		  putAction('/rawFabric/sign',{barcode:barcode.value,location:location.value},{'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}).then((res1:any)=>{
			  if(res1.code===7778){//已经入库
			  let data=JSON.parse(res1.msg)
			  console.log(data);
				  showConfirmDialog({
					  title: `面料编号为${barcode.value}的${data.category}已放置在${data.location},是否将面料移动到${location.value}?`,
					  confirmButtonText:'确认',
					})
					  .then(() => {
						  putAction('/rawFabric/sign',{barcode:barcode.value,location:location.value,force:true},{'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8'}).then((res2:any)=>{
							 if(res2.code===200){
								  showToast('入货架成功!')
							 }else{
								  showToast(res2.msg)
							 }
						  })
					  })
					  .catch(() => {
						location.value=''
						barcode.value=''
				  });
			  }else if(res1.code===7777){//暂存
			  let data=JSON.parse(res1.msg)
				  showConfirmDialog({
					  title: `面料编号为${barcode.value}的${data.category}被暂存在检验室,是否确认移动到${location.value}?`,
					  confirmButtonText:'确认',
					})
					  .then(() => {
						  putAction('/rawFabric/sign',{barcode:barcode.value,location:location.value,force:true},{'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8'}).then((res2:any)=>{
							  if(res2.code===200){
								  showToast('入货架成功!')
							  }else{
								  showToast(res2.msg)
							  }
						  })
					  })
					  .catch(() => {
						location.value=''
						barcode.value=''
				  });
			  }else if(res1.code===200) {
				 showConfirmDialog({
					  title: '扫码成功,请扫描面料编号',
					  confirmButtonText:'扫一扫',
					})
					  .then(() => {
						scanCode()
					  })
					  .catch(() => {
						location.value=''
						barcode.value=''
				 });
			  }else{
				   showToast(res1.msg)
			  }
		  })
	  }else{
		 location.value=res 
		 showConfirmDialog({
			  title: '扫码成功,请扫描面料编号',
			  confirmButtonText:'扫一扫',
			})
			  .then(() => {
				scanCode()
			  })
			  .catch(() => {
				location.value=''
				barcode.value=''
		 });
	  }
	}
	function handleFail(err) {
	  uni.showModal({
	    title: err.errName,
	    content: err.errMsg,
	    complete: () => {
	      h5ScanCode.value = false;
	    },
	  });
	  // TODO
	  
	}
	function handleClose() {
	  h5ScanCode.value = false;
	}
	
</script>

<style lang="scss" scoped>
	       
</style>