<template>
	<view class="">
		<uni-popup ref="popup" @change="change">
			<view class="popup_box">
				<view class="pop_title uni-flex space-between">
					<view class="">
						扫描{{title}}
					</view>
					<view class="">
						<image class=" icons_scan_close" src="@/static/icons/icons_scan_close.svg"
							@click="closeScanPopup()"></image>
					</view>
				</view>
				<view class="">
					<view class="">
						<win-com-scan ref="scan" @getResult="getScanResult" :placeholder='title' :clearResult="false"
							headerType="HMQ,HPQ">
						</win-com-scan>
					</view>
				</view>

			</view>
		</uni-popup>
		<comMessage ref="comMessage"></comMessage>
	</view>

</template>

<script>
	import {
		getBasicItemByCode
	} from '@/api/request2.js';
	
	import {
		checkDirectoryItemExist,
		getListItemTypeDesc,
		getItemTypeInfo
	} from '@/common/directory.js';

	import winComScan from '@/mycomponents/scan/winComScan.vue'
	import comMessage from '@/mycomponents/common/comMessage.vue'

	export default {
		name: 'winScanItem',
		components: {
			winComScan,
			comMessage
		},
		emits: ["getScanCode", 'getScanResult'],
		props: {
			title: {
				type: String,
				default: ''
			},
			isShowRecord: {
				type: Boolean,
				default: true
			},
			itemCodeTypeList: {
				type: Array,
				default: []
			},
		},
		data() {
			return {
				code: '',
				result: {},
				isShow: false,
				expand: false,
				scanList: [],
				expendIcon: 'arrow-down'
			}

		},

		created() {

		},
		methods: {
			openScanPopup() {
				this.$refs.popup.open('bottom')
			},
			closeScanPopup() {
				this.$refs.popup.close()
			},
			getfocus() {
				if (this.isShow) {
					this.$refs.scan.getfocus()
				}
			},
			scanClick() {
				this.$refs.scan.clickScanMsg();
			},
			cancelClick() {
				this.$refs.scan.clearScanValue();
			},
			getScanResult(result) {
				uni.showLoading({
					title: '扫描中...',
					mask: true
				});
				
				var scanResult = result;
				if (scanResult.label.barType == "QRCode") {
					this.code = scanResult.label.itemCode;
				} else if (scanResult.label.barType == "BarCode") {
					this.code = scanResult.label.code;
				}
				if (this.code == undefined) {
					uni.hideLoading();
					this.showErrorMessage("扫描物料[" + this.code + "]为空,请输入正确的物料")
					return
				}
				getBasicItemByCode(this.code).then(res => {
					uni.hideLoading();
					if (res.data != null && res.data.list.length > 0) {
						var result =res.data.list[0];
						var status =result.status;
						var type = result.type;
						var itemCode = result.code
						var itemName =result.name
						var uom = result.uom
						var std = result.uom
						result.package = scanResult.package
						if(status=="ENABLE"){
							if(checkDirectoryItemExist(this.itemCodeTypeList,type)){
								this.result =result;
								this.callBack()
							}else {
								var hint = getListItemTypeDesc(this.itemCodeTypeList);
								this.showErrorMessage("扫描物料[" + this.code + "]是[" +
									getItemTypeInfo(type).label + "],需要的物料类型是[" + hint + "]")
							}
						}else {
							this.showErrorMessage('物料【' + this.code + '】不可用');
						}
					} else {
						this.showErrorMessage('未查找到物料【' + this.code + '】');
					}
				}).catch(error => {
					uni.hideLoading();
					this.showErrorMessage(error)
				})
			},
			callBack() {
				this.closeScanPopup();
				this.$refs.scan.clear();
				// this.$emit("getScanCode", this.code);
				this.$emit("getScanResult", this.code, this.result);
			},
			change(e) {
				this.isShow = e.show
			},
			showErrorMessage(message) {
				this.losefocus();
				this.$refs.comMessage.showErrorMessage(message, res => {
					this.code = '';
					this.getfocus();
				})
			},
			getfocus() {
				this.$refs.scan.getfocus();
			},
			losefocus() {
				this.$refs.scan.losefocus();
			},

		}
	}
</script>

<style>
</style>
