<template>
	<view class="page">
		<uni-section title='单据信息' type='line' :strong="true"  class="bg-white"/>
		<template v-if="type==='调出线边仓'">
			<view class="my-2 bg-white">
				<list-input title="调入仓库(名称)" class="bg-lightest">
					<picker class="height-100 text-right form-item-input flex align-center justify-end" :value='outCkIndex' :range='outCkList' range-key="CKMC" @change="onChangeCK('调出',$event)">
						<view v-if='outCkList.length>0'>
							{{outCkList[outCkIndex].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='outKwIndex' :range='outKwList' range-key="KWH" @change="onChangeKW('调出',$event)">
						<view v-if='outKwList.length>0'>
							{{outKwList[outKwIndex].KWH}}
						</view>
						<view v-else>--</view>
					</picker>
				</list-input>
			</view>
			<view class="my-2 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>
		</template>
		<!-- 调入线边仓，调出是从我的仓库调出， -->
		<template v-if="type==='调入线边仓'">
			<view class="my-2 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-2 bg-white">
				<list-input title="调入仓库(名称)">
					<picker class="height-100 text-right form-item-input flex align-center justify-end" :value='inCkIndex' :range='inCkList' range-key="CKMC" @change="onChangeCK('调入',$event)">
						<view v-if='inCkList.length > 0'>
							{{inCkList[inCkIndex].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='inKwIndex' :range='inKwList' range-key="KWH" @change="onChangeKW('调入',$event)">
						<view v-if='inKwList.length > 0'>
							{{inKwList[inKwIndex].KWH}}
						</view>
						<view v-else>--</view>
					</picker>
				</list-input>
			</view>
		</template>
		<table-list :list="list"></table-list>
		<btn-save :disabledOk="disabledSubmit || loading" @save='onSave' 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 {queryInStorageLocation,queryOutStorageLocation,queryDefaultStorage} from "@/api/api.js"
	import {mapState,mapGetters} from "vuex"
	import {saveLinesidetransfer} from "@/api/api.js"
	
	export default {
		components:{
			uniSection,
			listInput,
			tableList,
			btnSave
		},
		data () {
			return {
				defaultKwIndex:0, // 默认库位索引
				defaultCkList:[],  // 默认仓库列表
				defaultCkIndex:0,  // 默认仓库索引
				
				inRealCkList:[], // 调入仓库（后台数据）
				inCkIndex:0,  // 调入仓库索引
				inKwIndex:0,  // 调入 库位 索引
				
				outRealCkList:[], // 调出仓库（后台数据）
				outCkIndex:0,  // 调出
				outKwIndex:0,  // 调出 库位 索引
				list:{  // 表格
					head:[
						{key:'itemno',dataIndex:"itemno",title:"物品号",},
						{key:'itemnum',dataIndex:"itemnum",title:"调拨数量",},
					],
					body:[]
				},
				type:null  ,// 调拨类型
				loading:false
			}
		},
		computed : {
			...mapGetters(['disabledSubmit']),
			...mapState(['ckList']),
			// 调入仓库
			inCkList () {
				return this._getLineCk()
			},
			// 调出仓库
			outCkList () {
				return this._getLineCk()
			},
			// 默认仓库库位
			defaultKwList () {
				if (this.defaultCkList.length > 0) {
					return this.defaultCkList[this.defaultCkIndex].storageloca
				}
				return []
			},
			// 调入 库位列表
			inKwList () {
				if (this.inCkList.length > 0) {
					return this.inCkList[this.inCkIndex].storageloca
				}
				return []
			},
			// 调出 库位列表
			outKwList () {
				if (this.outCkList.length > 0) {
					return this.outCkList[this.outCkIndex].storageloca
				}
				return []
			}
		},
		onLoad (e) {
			this.type=e.type
			let list=JSON.parse(e.list)
			this.list.body=list
			this.initPage()
		},
		onPageScroll(res) {
			uni.$emit('onPageScroll',res);
		},
		methods:{
			// 初始化页面
			async initPage () {
				this.loading=true
				await this._queryDefaultStorage()
				switch (this.type) {
					case "调入线边仓":
					  await this._queryInStorageLocation()
				  break;
					case "调出线边仓" :
					  await this._queryOutStorageLocation()
					break;
				}
				this.loading=false
				uni.setNavigationBarTitle({
				  title:this.type
				});
			},
			// 获取默认仓库
			async _queryDefaultStorage () {
				let resdata=await queryDefaultStorage()
				this.defaultCkList=resdata
				return this.defaultCkList
			},
			// 依据仓库获取线边仓
			_getLineCk () {
				let arr=[]
				const realArr=this.type==='调入线边仓' ? 'inRealCkList' : 'outRealCkList'
				if (this.defaultCkList.length>0) {
					const BMH=this.defaultCkList[this.defaultCkIndex].BMH
					arr=this[realArr].filter(item=>{
						return item.BMH===BMH
					})
				}
				return arr
			},
			// 改变仓库
			onChangeCK (key,event) {
				const index=event.detail.value
				switch (key) {
					case "调入" :
					  this.inCkIndex=index
					break;
					case "调出" :
					  this.outCkIndex=index
					break;
					case "默认" :
					  this.defaultCkIndex=index
					break;
				}
			},	
			// 改变库位
			onChangeKW (key,event) {
				const index=event.detail.value
				switch (key) {
					case "调入" :
					  this.inKwIndex=index
					break;
					case "调出" :
					  this.outKwIndex=index
					break;
					case "默认" :
					  this.defaultKwIndex=index
					break;
				}
			},
			//查询 调出仓库信息
			async _queryOutStorageLocation () {
				const resdata=await queryOutStorageLocation()			
				this.outRealCkList=resdata
				return resdata
			},
			// 查询 调入仓库信息
			async _queryInStorageLocation () {
				const resdata=await queryInStorageLocation()
				this.inRealCkList=resdata
				return resdata
			},
			// 保存接口
			async _saveLinesidetransfer () {
				let type=-1,
						outCkh=this.defaultCkList[this.defaultCkIndex].CKH,
						outKwh=this.defaultKwList[this.defaultKwIndex].KWH,
						inCkh=null,
						inKwh=null,
						items=[];
				switch (this.type) {
					case "调入线边仓":
					  type=1 ;
						inCkh=this.inCkList[this.inCkIndex].CKH ;
						inKwh=this.inKwList[this.inKwIndex].KWH ;
					break;
					case "调出线边仓" :
					  type=0 ;
						inCkh=this.outCkList[this.outCkIndex].CKH ;
						inKwh=this.outKwList[this.outKwIndex].KWH ;
					break;
				}
				this.list.body.forEach(item=>{
					items.push({"WPH":item.itemno,"SL":item.itemnum})
				})
				const reqdata={
					"CKHOUT":outCkh,   //调出仓库
					"KWHOUT":outKwh,   //调出库位号
					"CKHIN":inCkh,     //调入仓库
					"KWHIN":inKwh,     //调入库位号
					"TYPE":type,       // 0为调出，1为调入
					"items":items
				}
				const resdata=await saveLinesidetransfer(reqdata)
				return resdata
			},
			onSave () {
				uni.showModal({
					title:"确认保存吗?",
					success:async (res)=>{
						if (res.confirm) {
							await this._saveLinesidetransfer()
							uni.showToast({
								title:"保存成功"
							})
							uni.reLaunch({
								url:"/pages/index/index"
							})
						}
					}
				})
			}
		}
	}
	
</script>

<style>
</style>
