<template>
	<view>
		<view class="container">
			<view v-if="orderType!='OQC'&&orderType!='OQCRET'"  style="position: fixed;top: 0;left:15px;right: 15px;z-index: 666;border-bottom: 1rpx solid #e0e0e0;">
				<tui-sel-load-list :maxCounts='5' typeName="生产班组" :disabled="false" :value="gcCode" title="选择生产班组"
					dotColor="#007aff" searchKey="" keyLabel="deptName" keyId="deptId" apiUrl="/product/whse/team/query"
					:postData="{}" @change="changeProItem">
					<uni-list-item showBorder showArrow title="生产班组" borderRight="0rpx" :rightText="gcName||'请选择生产班组'"
						link>
					</uni-list-item>
				</tui-sel-load-list>
			</view>
			<view  v-if="orderType!='OQC'&&orderType!='OQCRET'"  style="height: 30px;"></view>
			<tui-load-list ref="list" v-model="scaleList" apiUrl="/product/whse/parse/query" :postData={type:orderType}
				:limit="100">
				<scaleListCom @delete="deleteBtn" :scaleList="scaleList"></scaleListCom>
			</tui-load-list>
			<view style="height:100px"></view>
			<view class="fixedbombtn flex">
				<tui-button style="margin-right: 10rpx;" type="danger" class=" flex-1 margin-right-sm "
					@click="deleteBtn">清空</tui-button>
				<tui-button class=" flex-2 " @click="entry">{{butText}}</tui-button>
			</view>
		</view>
		<uni-fab ref="fab" :pattern="pattern" :horizontal="horizontal" :vertical="vertical" @fabClick="scan" />
		<!-- <tui-tabbar :tabBar="tabBar" :current="current" v-if="current>=0"></tui-tabbar> -->
		
	</view>
</template>
<script>
	import scaleListCom from '../scaleListCom.vue';
	import jweixin from 'weixin-js-sdk'
	import {
		parseAdd,
		whseAdd,
		whseTeam,
		whseRemove,
		whseRetAdd,
		getSign
	} from '@/api/whse.js'
	export default {
		components: {
			scaleListCom
		},
		data() {
			return {
				title: 'uni-fab',
				directionStr: '垂直',
				horizontal: 'right',
				vertical: 'bottom',
				direction: 'horizontal',
				orderType: null,
				getSign: getSign,
				gcCode: '',
				gcName: '',
				pattern: {
					color: '#7A7E83',
					backgroundColor: '#fff',
					selectedColor: '#007AFF',
					buttonColor: '#007AFF',
					iconColor: '#fff',
					icon: 'scan',
				},
				is_color_type: false,
				scaleList: [],
				current: 1,
				proTeam: [],
				proTeamid: [],
				butText: '入库',
			}
		},
		created() {
			this.getProTeam()
			this.getConfig()
		},
		onLoad(option) {
			// console.log(option, 'option')
			this.orderType = option.type
			var pageTitle = ''
			if (this.orderType == 'PRO') {
				pageTitle = '生产入库'
				this.butText = '入库'
			} else if (this.orderType == 'OQC') {
				pageTitle = '品检入库'
				this.butText = '入库'
			} else if (this.orderType == 'FTB') {
				pageTitle = '翻箱入库'
				this.butText = '入库'
			} else if (this.orderType == 'OQCRET') {
				pageTitle = '品检退库'
				this.butText = '退库'
			} else if (this.orderType == 'FTBRET') {
				pageTitle = '翻箱退库'
				this.butText = '退库'
			} else {
				this.butText = '入库'
				pageTitle = '生产入库'
			}
			uni.setNavigationBarTitle({
				title: pageTitle
			})
			// setTimeout(() => {
			// 	this.$refs.popup.open()
			// }, 300)

		},
		onPullDownRefresh() {
			uni.stopPullDownRefresh();
			this.$refs.list.loadData("rush")
		},
		onReachBottom() {
			this.$refs.list.loadData()
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		},
		methods: {
			changeProItem(e) {
				// console.log(e,'eee')
				this.proTeamids = []
				this.gcName = ''
				e.forEach((v, index) => {
					this.proTeamids.push(v.deptId)
					this.gcName += index > 0 ? ',' + v.deptName : v.deptName
				})
				// this.proTeamid = e.detail.value;
			},
			whseSucss() {
				this.$refs.list.loadData("rush")
				uni.hideLoading()
				this.$qsk.modal({
					title: this.butText + "成功",
					content: '',
					showCancel: false,
					confirmText: '确定'
				})
			},
			entry() {
				this.$qsk.modal({
					title: "您是否确认" + this.butText + "？",
					content: '',
					showCancel: true,
					confirmText: '确定'
				}).then(res2 => {
					const snCode = []
					this.scaleList.forEach(v => {
						snCode.push(v.snCode)
					})
			
					if (this.orderType == 'PRO' || this.orderType == 'OQC' || this.orderType == 'FTB') {
					
					const productWhseEntDet = []
					const aa = {
						teamIds: this.proTeamids,
						snCode: snCode
					}
					productWhseEntDet.push(aa)
						whseAdd({
							type: this.orderType,
							productWhseEntDet
						}).then(res => {
							this.whseSucss()
						}).catch(error => {
							uni.hideLoading()
						})
					} else {
						const retDets = []
						const aa = {
							teamIds: this.proTeamids,
							snCode: snCode
						}
						retDets.push(aa)
						whseRetAdd({
							type: this.orderType,
							retDets
						}).then(res => {
							this.whseSucss()
						}).catch(error => {
							uni.hideLoading()
						})
					}
				}).catch(() => {})
			},
			getConfig() {
				this.getSign({
					url: location.href,
					appid: 'wx2cfd24c7380cdc08',
				}).then(res => {
					var s = res.data
					jweixin.config({
						beta: true,// 必须这么写，否则wx.invoke调用形式的jsapi会有问题
						debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
						appId: s.appId, // 必填，公众号的唯一标识
						timestamp: s.timestamp, // 必填，生成签名的时间戳
						nonceStr: s.nonceStr, // 必填，生成签名的随机串
						signature: s.signature, // 必填，签名
						jsApiList: ['checkJsApi',"onMenushareTimeline",
			"onMenushareAppMessage",'scanQRCode'] // 必填，需要使用的JS接口列表
					})
				})
			},
			scan() {
				// parseAdd({
				// 	code: 'FP2-2-FF30-5-1',
				// 	type: this.orderType,
				// }).then(res => {
				// 	this.$refs.list.loadData("rush")
				// 	// uni.hideLoading()
				// 	// console.log(res,'res')
				// }).catch(error => {
				// 	// uni.hideLoading()
				// })
				
				
				const _this = this
				jweixin.scanQRCode({
					needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
					scanType: ['qrCode', 'barCode'], // 可以指定扫二维码还是一维码，默认二者都有
					success: function(res) {
						console.log(res,'resss')
						let str = res.resultStr // 当needResult 为 1 时，扫码返回的结果
						parseAdd({code:res.resultStr,type: _this.orderType,}).then(res => {
							_this.$refs.list.loadData("rush")
							uni.hideLoading()
							// console.log(res,'res')
						}).catch(error => {
							uni.hideLoading()
						})
					},
					fail: (err) => {
						// console.error('扫描失败：' + err);
						uni.showToast({
							title: '扫描失败',
							icon: 'none'
						});
					}
				})
			},
			getProTeam() {
				uni.showLoading({
					title: '加载中'
				})

				// fixedAssetList({}).then(res => {
				// 	uni.hideLoading()
				// }).catch(error => {
				// 	uni.hideLoading()
				// })
				whseTeam().then(res => {
					this.proTeam = res.data
					uni.hideLoading()
					// console.log(res,'res')
				}).catch(error => {
					uni.hideLoading()
				})
			},
			deleteBtn(item) {
				let ids=[]
				let text='清空'
				if(item){
					ids.push(item.id)
					text="删除"
				}
				this.$qsk.modal({
					title: "您是否确认"+text+"数据？",
					content: '',
					showCancel: false,
					confirmText: '确定'
				}).then(res2 => {
					whseRemove({
						ids: ids,
						type: this.orderType
					}).then(res => {
						this.$refs.list.loadData("rush")
					})
				})
			},
			onchange(e) {
				const value = e.detail.value
			},
			onnodeclick(node) {},
			changeValue() {},
			dialogOpend() {
				this.$refs.alertDialog.open()
			},
			dialogClosed() {
				this.$refs.alertDialog.close()
			},
			getUpQMImg(val) {
				this.picList.forEach((v, index) => {
					if (v.categ == val.categ) {
						this.picList.splice(index, 1)
						return
					}
				})
				this.picList.push(val)
			},
			clecKListkHasItem(list, objName) {
				return list.some(item => item.categ == objName)
			},
			addAssetsBtn() {
				this.$tab.navigateTo('/pages/takestock/addAssets/index')
			},
			selectAssetsBtn() {
				this.$tab.navigateTo('/pages/takestock/assetsList/index')
			},
		}
	}
</script>
<style lang="scss">
	page {
		background-color: #ffffff;

		.container {
			padding: 30rpx;
		
		}
	}
</style>