<template>
	<view class="withdrawal">
		<view class="form-box" style="margin:0 30rpx">
			<uni-forms ref="baseForm" :modelValue="formData" label-position="top">
				<uni-forms-item label="申请人:" required>
					<picker @change="bindPickerChange" :range="userList" range-key="nickName">
						<view class="" style="text-align: right;display: flex;justify-content: space-between;">
							<view class="">
								{{formData.nickName || '请选择'}}
							</view>
							 <uni-icons type="forward" size="30"></uni-icons>
						</view>
					</picker>
				</uni-forms-item>
				<view class="commons-line-box"></view>
				<uni-forms-item label="申请时间:">
					{{date}}
				</uni-forms-item>
				<view class="commons-line-box"></view>
				<view class="commons-title">
					退料明细
				</view>
				<uni-forms-item label="选择物料:" required>
					<picker @change="bindPickerChange2" :range="materialData" range-key="name">
						<view class="" style="text-align: right;display: flex;justify-content: space-between;">
							<view class="">
								{{formData.materialName || '请选择'}}
							</view>
							 <uni-icons type="forward" size="30"></uni-icons>
						</view>
					</picker>
				</uni-forms-item>
				<view class="commons-line-box"></view>
				<uni-forms-item label="退料件数:" required>
					<uni-number-box v-model="formData.count" :min="0" />
				</uni-forms-item>
				<view class="commons-line-box"></view>
				<uni-forms-item label="退料数量:" required>
					<uni-number-box v-model="formData.quantity" :min="0" />
				</uni-forms-item>
				<uni-forms-item label="备注:" required>
					<uni-easyinput placeholder="请输入" v-model="formData.remark" :min="0" />
				</uni-forms-item>
			</uni-forms>
			<view class="submit-box">
				<button  style="background: #0066CC;color:#fff;margin-bottom: 30rpx;" @click="returnMaterial">提交</button>
			</view>
		</view>
	</view>
	
</template>

<script>
	import {
		getInfo
	} from "@/util/api.js";
	import Vue from 'vue'
	import {dateFormat,getTime} from '../../util/uitl.js'
	export default {
		data() {
			return {
				stockChangeData: [{
					text: '出库',
					value: 20
				},{
					text: '补出库',
					value: 23
				},{
					text: '绑定错误',
					value: 24
				},{
					text: '退货出库',
					value: 26
				},{
					text: '报废出库',
					value: 27
				}],
				listData: [],
				formData: {
					operUserName:"",
					qrCodeList:[],
					stockChangeType:null,
					operUserId:null,
					nickName:null,
					materialName:null,
				},
				req: {
					qrCodeList: [],
					stockHouseId: null,
				},
				btnType: 1,
				isScanCode: false,
				userData: {},
				dtlData: null,
				qrCodeList: [],
				index: 0,
				stockHouseData: [],
				date:null,
				userList:[],
				materialData:[],
				quantitytol:0,
			}
		},
		onLoad(option) {
			const v = this
			v.userData = Vue.prototype.$userInfo
			v.$post('/wx/common/getStockHouse', {}).then(res => {
				v.stockHouseData = res.data
			})
			v.date = getTime().substr(0,11)
			v.$post('/wx/common/getUser',{}).then(res=>{
				v.userList = res.data
			})
			v.$post('/wx/common/getMaterial',{}).then(res=>{
				v.materialData = res.data
			})
			
		},
		methods: {
			returnMaterial(){
				const v = this
				v.$post('/wx/codeScanIn/returnMaterial',v.formData).then(res=>{
					console.log(res)
					if(res.code == 200){
						uni.showToast({
							title:'退仓成功，跳转中'
						})
						setTimeout(()=>{
							wx.reLaunch({
								url: '/pages/home'
							})
						},2000)
					}
				})
			},
			addOutInfo(){
				const v = this
				if(v.qrCodeList.length == 0){
					uni.showToast({
						icon: "error",
						title: '请添加物料信息',
						duration: 2000
					});
					return
				}
				if(!v.formData.operUserId){
					uni.showToast({
						icon: "error",
						title: '请选择领料人',
						duration: 2000
					});
					return
				}
				if(!v.formData.stockChangeType){
					uni.showToast({
						icon: "error",
						title: '请选择出库类型',
						duration: 2000
					});
					return
				}
				v.req.qrCodeList = []
				for (var i = 0; i < v.qrCodeList.length; i++) {
					v.formData.qrCodeList.push(v.qrCodeList[i].qrCode)
				}
				console.log(v.formData)
				v.$post('/wx/codeScanOut/addOutInfo',v.formData).then(res=>{
					if(res.code == 200){
						uni.showToast({
							title:'出库成功，跳转中！'
						})
						setTimeout(()=>{
							wx.reLaunch({
								url: '/pages/home'
							})
						},2000)
					}
				})
			},
			delelte(index) {
				const v = this
				uni.showModal({
					title: "提示",
					content: "你确定删除此信息么？",
					success(res) {
						if (res.confirm) {
							v.qrCodeList.splice(index, 1)
						}
					}
				})

			},
			bindPickerChange2(e){
				const v = this
				v.formData.materialName = v.materialData[e.detail.value].name
				v.formData.material_id = v.materialData[e.detail.value].id
			},
			bindPickerChange(e) {
				const v = this
				v.formData.nickName = v.userList[e.detail.value].nickName
				v.formData.create_id = v.userList[e.detail.value].userId
			},
			scanCode(_isReload) {
				const v = this
				uni.scanCode({
					onlyFromCamera: true,
					success: function(res) {
						uni.showToast({
							icon: "loading",
							title: '加载中',
							duration: 1500
						});
						v.$post('/wx/common/gerMaterialInfoByQrcode', {
							qrCode: res.result
						}).then(res => {
							if(!res.data.stockHouseId){
								uni.showToast({
									icon: "error",
									title: '此物料不在库存内！',
									duration: 2000
								});
								return
							}else{
								v.qrCodeList.push(res.data)
								v.quantitytol =+ res.data.quantity
							}
							if (_isReload) {
								v.scanCode(_isReload)
							}
						})
					}
				});
			},


		},
	}
</script>

<style lang="less">
	.withdrawal{
		
	}
</style>
