<template>
	<view>
		<uni-section title='单据信息' type='line' :strong="true"  class="bg-white"/>
		<view class="my-1 bg-white">
			<list-input title="调出仓库(名称)">
				<picker class="height-100 text-right form-item-input flex align-center justify-end" :value='defaultCkIndex' :range='defaultCkList' range-key='CKMC' @change="onChangeCK('自',$event)">
					<view v-if='defaultCkList.length > 0'>
						{{defaultCkList[defaultCkIndex].CKMC}}
					</view>
					<view v-else>--</view>
				</picker>
			</list-input>
			<list-input title="调出库位(号)">
				<picker class="height-100 text-right form-item-input flex align-center justify-end" :value='defaultKwIndex' :range='defaultKwList' range-key='KWH' @change="onChangeKW('自',$event)">
					<view v-if='defaultKwList.length > 0'>
						{{defaultKwList[defaultKwIndex].KWH}}
					</view>
					<view v-else>--</view>
				</picker>
			</list-input>
		</view>
		<view class="my-1 bg-white">
			<list-input title="调入仓库(名称)">
				<picker class="height-100 text-right form-item-input flex align-center justify-end" :value='toCkIndex' :range='toCkList' range-key='CKMC' @change="onChangeCK('至',$event)">
					<view v-if='toCkList.length > 0'>
						{{toCkList[toCkIndex].CKMC}}
					</view>
					<view v-else>--</view>
				</picker>
			</list-input>
			<list-input title="调入库位(号)">
				<picker class="height-100 text-right form-item-input flex align-center justify-end" :value='toKwIndex' :range='toKwList' range-key='KWH' @change="onChangeKW('至',$event)">
					<view v-if='toKwList.length > 0'>
						{{toKwList[toKwIndex].KWH}}
					</view>
					<view v-else>--</view>
				</picker>
			</list-input>
		</view>
		<table-list :list="list"></table-list>
		<btn-save @save='onSave' :disabledOk="disabledSubmit" back/>
	</view>
</template>

<script>
	import uniSection from "@/components/uni-ui/uni-section/uni-section.vue"
	import listInput from "@/components/common/list-input.vue"
	import tableList from "@/components/common/table-list.vue"
	import btnSave from "@/components/common/btn-save.vue"
	import {mapGetters} from "vuex"
	import {queryDepartStorageLocation,savePartmenttransfer,queryDefaultStorage} from "@/api/api.js"
	
	const headData=[{key:'itemno',dataIndex:"itemno",title:"物品号",},
						{key:'itemnum',dataIndex:"itemnum",title:"调拨数量",},]
	
	export default {
		components:{
			uniSection,
			listInput,
			tableList,
			btnSave
		},
		data () {
			return {
				defaultCkList:[], // 默认（自仓库）
				defaultCkIndex:0, // 自仓库索引
				defaultKwIndex:0, // 自库位索引
				toCkIndex:0,  // 至仓库索引
				toCkList:[],  // 至仓库列表
				toKwIndex:0,  // 至库位索引
				list:{  // 表格
					head:headData,
					body:[]
				},
			}
		},
		computed:{
			...mapGetters(['disabledSubmit']),
			// 自库位列表
			defaultKwList () {
				if (this.defaultCkList.length > 0) {
				  return this.defaultCkList[this.defaultCkIndex].storageloca
				}
				return []
			},
			 // 至库位列表
			toKwList () {
				if (this.toCkList.length > 0) {
				  return this.toCkList[this.toCkIndex].storageloca
				}
				return []
			}
		},
		onLoad (e) {
			if (e.list) {
				this.list.body=JSON.parse(e.list)
			}
			this.initPage()
		},
		onPageScroll(res) {
			uni.$emit('onPageScroll',res);
		},
		methods:{
			// 初始化页面
			async initPage () {
				this._queryDepartStorageLocation()
				this._queryDefaultStorage()
			},
			//获取默认仓库
			async _queryDefaultStorage () {
				const resdata=await queryDefaultStorage()
				this.defaultCkList=resdata
				return resdata
			},
			//保存接口
			async _savePartmenttransfer () {
				let items=[]
				this.list.body.forEach(item=>{
					items.push({"WPH":item.itemno,"SL":item.itemnum})
				})
				const reqdata={
					"CKHOUT":this.defaultCkList[this.defaultCkIndex].CKH,   //调出仓库
					"KWHOUT":this.defaultKwList[this.defaultKwIndex].KWH, //调出库位号
					"CKHIN":this.toCkList[this.toCkIndex].CKH,      //调入仓库
					"KWHIN":this.toKwList[this.toKwIndex].KWH,      //调入库位号
					"items": items    
				}
				console.log(reqdata)
				const resdata=await savePartmenttransfer(reqdata)
				return resdata
			},
			// 获取仓库
			async _queryDepartStorageLocation () {
				const resdata=await queryDepartStorageLocation()
				this.toCkList=resdata
			},
			// 改变仓库
			onChangeCK (key,event) {
				const value=event.detail.value
				switch (key) {
					case "自":
					this.defaultCkIndex=value
					break;
					case "至":
					this.toCkIndex=value
					break;
				}
			},
			// 改变库位
			onChangeKW (key,event) {
				const value=event.detail.value
				switch (key) {
					case "自":
					this.defaultKwIndex=value
					break;
					case "至":
					this.toKwIndex=value
					break;
				}
			},
			// 提交
			onSave () {
				uni.showModal({
					title:"确认保存吗?",
					success:async res=>{
						if (res.confirm) {
							
							await this._savePartmenttransfer()
							uni.showToast({
								title:"保存成功"
							})
							uni.reLaunch({
								url:"/pages/index/index"
							})
						}
					}
				})
			}
		}
	}
	
</script>

<style>
</style>
