<template>
	<view class="p-content">
		<view class="u-forms">
			<u--form
					labelPosition="left"
					:model="form"
					ref="uForm"
					errorType="toast"
					labelWidth="auto"
			>
				<u-form-item
						label="盘点类型"
						prop="checkType"
						borderBottom
						@click="showCheckType = true;"
						ref="item1"
						required
				>
					<!-- <u-radio-group 
					     v-model="form.checkType"
						 @change="checkTypeChange"
					     placement="row">
					 	<u-radio 
						:customStyle="{marginLeft: '20px'}" 
						v-for="item in checkTypeList" 
						:key="item.code" 
						:label="item.name" 
						:name="item.code">
						</u-radio>
					 </u-radio-group> -->
					 <view style="text-align: right;width: 100%;">
						 自选盘点
					 </view>
				</u-form-item>
				<!-- <u-form-item
						label="是否区分仓库"
						prop="warehouseFlag"
						borderBottom
						@change="warehouseFlagChange"
						@click="showWarehouseFlag = true;"
						ref="item2"
						required
						v-if="form.checkType=='SELECT'"
				>
					 <u-radio-group
					     v-model="form.warehouseFlag"
					     placement="row">
					 	<u-radio 
							:customStyle="{marginLeft: '20px'}" 
							v-for="item in warehouseFlagList" 
							:key="item.code" 
							:label="item.name" 
							:name="item.code">
							</u-radio>
					 </u-radio-group>
				</u-form-item> -->
				<u-form-item
						label="盘点仓库"
						prop="warehouseSnList"
						borderBottom
						@click="showWarehouse = true;"
						ref="item2"
						required
						v-if="form.checkType=='SELECT'"
				>
					<u--input
							v-model="warehouseName"
							disabled
							inputAlign="right"
							disabledColor="#ffffff"
							placeholder="请选择盘点仓库"
							border="none"
					></u--input>
					<u-icon
							slot="right"
							name="arrow-right"
					></u-icon>
				</u-form-item>
			</u--form>
		</view>
		<view class="f-button">
			<u-button @click="cansel" hairline>取消</u-button>
			<u-button :loading="spinning" type="primary" @click="submit" hairline>提交</u-button>
		</view>
		
		<!-- 请选择盘点仓库 -->
		<u-picker
		title="请选择盘点仓库" 
		:show="showWarehouse" 
		@cancel="showWarehouse = false" 
		@close="showWarehouse = false"
		@confirm="warehouseSelect" 
		:columns="warehouseList" 
		:closeOnClickOverlay="true"
		keyName="name">
		</u-picker>
	</view>
</template>

<script>
import { checkWarehouseSave } from '@/config/api.js'
export default {
	data() {
		return {
			spinning: false,
			showCheckType: false,
			checkTypeList: [],
			showWarehouseFlag: false,
			warehouseFlagList: [],
			showWarehouse: false,
			warehouseList:[],
			warehouseName: '',
			form:{
				checkType: 'SELECT',
				warehouseFlag: '0',
				warehouseSnList: undefined
			}
		};
	},
	onLoad() {
		this.checkTypeList = this.$store.state.vuex_checkTypeList
		this.warehouseFlagList = this.$store.state.vuex_warehouseFlagList
		const warehouseList = this.$store.state.vuex_checkWarehouseList
		this.warehouseList = [warehouseList]
		console.log(this.warehouseList)
		// 只有一个仓库是默认选中
		if(warehouseList.length==1){
			this.form.warehouseSnList = warehouseList[0].warehouseSn
			this.warehouseName = warehouseList[0].name
			this.$refs.uForm.validateField('warehouseSnList')
		}
	},
	onBackPress(e) {
		if(this.showWarehouse){
			this.showWarehouse = false
			return true
		}
	},
	onNavigationBarButtonTap(e){
		if(e.index == 0){
			uni.navigateTo({
				url: "/pages/stockPan/list"
			})
		}
	},
	methods: {
		checkTypeChange (e) {
		  this.form.warehouseFlag = '0'
		  this.form.warehouseSnList = undefined
		},
		warehouseFlagChange (e) {
		  this.form.warehouseSnList = undefined
		},
		// 请选择盘点仓库
		warehouseSelect(e) {
			this.form.warehouseSnList = e.value[0].warehouseSn
			this.warehouseName = e.value[0].name
			this.$refs.uForm.validateField('warehouseSnList')
			this.showWarehouse = false
		},
		// 获取仓库列表
		getWarehouseList () {
		  checkWarehouseWarehouse({}).then(res => {
			if (res.status == 200) {
			  this.warehouseList = res.data || []
			} else {
			  this.warehouseList = []
			}
		  })
		},
		submit() {
			const form = JSON.parse(JSON.stringify(this.form))
			form.sourceType = 'HAND'
			if(!form.warehouseSnList){
				uni.$u.toast("请选择盘点仓库")
				return false
			}
			// 全部
			if (form.warehouseSnList == '-1') {
				form.warehouseFlag = '0'
				form.warehouseSnList = undefined
			}else{
				form.warehouseFlag = '1'
			}
			this.spinning = true
			checkWarehouseSave(form,{custom:{catch:true}}).then(res => {
				console.log(res)
				if (res.status == 200) {
				  uni.$u.toast(res.message)
				  uni.redirectTo({
					url:"/pages/stockPan/index?id="+res.data.id+"&checkWarehouseSn="+res.data.checkWarehouseSn
				  })
				}
				this.spinning = false
			}).catch(res => {
				this.spinning = false
			})
		},
		cansel(){
			uni.navigateBack()
		}
	}
};
</script>

<style lang="scss">
	.p-content{
		.u-forms{
			flex-grow: 1;
			height: 100%;
			overflow: auto;
			padding: 0 50upx;
		}
		.f-button{
			display: flex;
			justify-content: space-around;
			padding: 60upx 40upx;
			button{
				width: 40%;
			}
		}
	}
</style>
