<template>
	<view class="content">
		<nav-bar title="入库"></nav-bar>
		<view class="search-confirm-wrap">
			<view class="zd-row">
				<view class="zd-col-11 zd-row">
					<search-box
					v-model="val1"
					/>
					<button class="confirm-button" @tap="toSearch">查询</button>
				</view>
				<view class="zd-col-11 zd-row jcflexend">
					<button class="confirm-button" :class="{'confirm-button_disabled': checkArr1.length === 0}" :disabled="disabled1" @tap="toSure">单据审核</button>
				</view>
			</view>
		</view>
		<view class="zd-row jcflexstart table-title_wraper">{{title}}</view>
		<view class="grid-wrap">
			<view class="table-wrap">
				<view class="zd-row zd-th-wraper">
					<view class="zd-col-2 zd-th"><text>序号</text></view>
					<view class="zd-col-2 zd-th"><text>选择</text></view>
					<view class="zd-col-3 zd-th"><text>入库单号</text></view>
					<view class="zd-col-3 zd-th"><text>EAS单号</text></view>
					<view class="zd-col-2 zd-th"><text>状态</text></view>
					<view class="zd-col-3 zd-th"><text>去向</text></view>
					<view class="zd-col-3 zd-th"><text>入库日期</text></view>
					<view class="zd-col-3 zd-th"><text>领料人</text></view>
					<view class="zd-col-3 zd-th"><text>制单人</text></view>
				</view>
				<view class="zd-td-wraper" v-for="(e, i) in dataList1" :key="e.id">
					<view class="zd-row" :class="{'zd-td-checked': pkId === e.id}">
						<view class="zd-col-2 zd-td" @tap.stop="toCollapse(e)"><text>{{i+1}}</text></view>
						<view class="zd-col-2 zd-td" @tap.stop="toCheck1(e)">
							<view class="zd-checkbox" :class="{'zd-checkbox_active': e.checked}"></view>
						</view>
						<view class="zd-col-3 zd-td fontcol1" @tap.stop="toCollapse(e)"><text>{{e.code}}</text></view>
						<view class="zd-col-3 zd-td" @tap.stop="toCollapse(e)"><text>{{e.djbh}}</text></view>
						<view class="zd-col-2 zd-td" @tap.stop="toCollapse(e)"><text class="fontbg1">{{e.djzt}}</text></view>
						<view class="zd-col-3 zd-td" @tap.stop="toCollapse(e)"><text>{{e.ckmc}}</text></view>
						<view class="zd-col-3 zd-td" @tap.stop="toCollapse(e)"><text>{{e.cksj}}</text></view>
						<view class="zd-col-3 zd-td" @tap.stop="toCollapse(e)"><text>{{e.llr}}</text></view>
						<view class="zd-col-3 zd-td" @tap.stop="toCollapse(e)"><text>{{e.cjr}}</text></view>
					</view>
					<view v-show="pkId === e.id" class="zd-sec-wraper">
						<view class="zd-row zd-sec-th-wraper">
							<view class="zd-col-1 zd-sec-th"><text>序号</text></view>
							<view class="zd-col-1 zd-sec-th"><text>选择</text></view>
							<view class="zd-col-2 zd-sec-th"><text>入库单号</text></view>
							<view class="zd-col-2 zd-sec-th"><text>明细编号</text></view>
							<view class="zd-col-2 zd-sec-th"><text>存货编号</text></view>
							<view class="zd-col-2 zd-sec-th"><text>物料编号</text></view>
							<view class="zd-col-3 zd-sec-th"><text>物料名称</text></view>
							<view class="zd-col-2 zd-sec-th"><text>推荐库位</text></view>
							<view class="zd-col-2 zd-sec-th"><text>实际库位</text></view>
							<view class="zd-col-2 zd-sec-th"><text>已入数量</text></view>
							<view class="zd-col-2 zd-sec-th"><text>数量</text></view>
							<view class="zd-col-3 zd-sec-th"><text>单据日期</text></view>
						</view>
						<view class="zd-row zd-td-wraper" v-for="(el, j) in dataList2" :key="el.id" @tap.stop="toCheck2(el)">
							<view class="zd-col-1 zd-sec-td"><text>{{j+1}}</text></view>
							<view class="zd-col-1 zd-sec-td"><view class="zd-checkbox" :class="{'zd-checkbox_active': el.checked}"></view></view>
							<view class="zd-col-2 zd-sec-td"><text>{{el.djbh}}</text></view>
							<view class="zd-col-2 zd-sec-td"><text>{{el.zzbm}}</text></view>
							<view class="zd-col-2 zd-sec-td"><text>{{el.kwbm}}</text></view>
							<view class="zd-col-2 zd-sec-td"><text>{{el.wlbm}}</text></view>
							<view class="zd-col-3 zd-sec-td"><text>{{el.wlmc}}</text></view>
							<view class="zd-col-2 zd-sec-td"><text>{{el.djbh}}</text></view>
							<view class="zd-col-2 zd-sec-td"><text>{{el.djbh}}</text></view>
							<view class="zd-col-2 zd-sec-td"><text>{{el.jbsl}}</text></view>
							<view class="zd-col-2 zd-sec-td"><text>{{el.fzsl}}</text></view>
							<view class="zd-col-3 zd-sec-td"><text>{{el.cksj}}</text></view>
						</view>
						<view class="zd-row mgt40">
							<view class="zd-col-12 zd-row jcflexstart">
								<button class="confirm-button" :class="{'confirm-button_disabled': checkArr2.length === 0}" :disabled="disabled2" @tap="_easOutInBillDetailUpdate">多库位修改</button>
								<button class="confirm-button" :class="{'confirm-button_disabled': checkArr2.length !== 1}" @tap="_queryInventoryInfo">单库位修改</button>
							</view>
							<view class="zd-col-12">
								<pagination :total="totalCount2" :page-size="queryParams2.pageSize" :current-page="currentPage2" @page-change="handlePageChange2" />
							</view>
						</view>
					</view>
				</view>
			</view>
			<pagination v-show="dataList1.length > 0" :total="totalCount1" :page-size="queryParams1.pageSize" :current-page="currentPage1" @page-change="handlePageChange1" />
		</view>
		<view class="pop-wraper" :class="show ? 'popshow' : 'pophide'">
			<view class="allwidth pop-title">库位修改</view>
			<view class="pop-grid-wraper">
				<view class="zd-row zd-sec-th-wraper zd-pop-th-wraper">
					<view class="zd-col-2 zd-sec-th"><text>序号</text></view>
					<view class="zd-col-4 zd-sec-th"><text>出库单号</text></view>
					<view class="zd-col-4 zd-sec-th"><text>物料编号</text></view>
					<view class="zd-col-5 zd-sec-th"><text>物料名称</text></view>
					<view class="zd-col-5 zd-sec-th"><text>实际库位</text></view>
					<view class="zd-col-4 zd-sec-th"><text>数量</text></view>
				</view>
				<view class="zd-row zd-td-wraper" v-for="(ele, t) in dataList3" :key="ele.id">
					<view class="zd-col-2 zd-sec-td zd-pop-td"><text>{{t+1}}</text></view>
					<view class="zd-col-4 zd-sec-td zd-pop-td"><text>{{ele.code}}</text></view>
					<view class="zd-col-4 zd-sec-td zd-pop-td"><text>{{ele.code}}</text></view>
					<view class="zd-col-5 zd-sec-td zd-pop-td"><text>{{ele.code}}</text></view>
					<view class="zd-col-5 zd-sec-td zd-pop-td">
						<scan-input
						v-model="ele.ckmc"
						/>
					</view>
					<view class="zd-col-4 zd-sec-td zd-pop-td">
						<input v-show="!ele.checked" type="number" class="pop-input" v-model="ele.qty" @blur="handleBlur(ele)">
						<text v-show="ele.checked">{{ele.qty}}</text>
					</view>
				</view>
			</view>
			<view class="zd-row jcenter">
				<view class="zd-col-4">
					<button class="confirm-button confirm-button_cancle" @tap="show = false">取消</button>
				</view>
				<view class="zd-col-4">
					<button class="confirm-button" :class="{'confirm-button_disabled': dataList3.length === 0}" :disabled="disabled3" @tap="_easOutInBillDetailUpdate1">确定</button>
				</view>
			</view>
		</view>
		<view v-if="show" class="modal" @click.stop="show = false"></view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import ScanInput from '@/components/ScanInput.vue'
	import Pagination from '@/components/Pagination.vue'
	import {easOutInBillPage, easOutInBillDetailPage, easOutInBillUpdate, easOutInBillDetailUpdate, queryInventoryInfo} from '@/utils/getData2.js'
	export default {
		components: {
			NavBar,
			SearchBox,
			ScanInput,
			Pagination
		},
		data() {
			return {
				title: '',
				id: '',
				val1: '',
				dataList1: [],
				dataList2: [],
				dataList3: [],
				pkId: '',
				checkArr1: [],
				disabled1: false,
				totalCount1: 0,
				queryParams1:{
					pageSize: 10,
					pageNum: 1
				},
				currentPage1: 1,
				code: '',
				totalCount2: 0,
				queryParams2:{
					pageSize: 5,
					pageNum: 1
				},
				currentPage2: 1,
				checkArr2: [],
				disabled2: false,
				show: false,
				disabled3: false
			};
		},
		onShow() {
			if (this.$store.getters.publicArr) {
				this.dataList1 = this.$store.getters.publicArr
			}
		},
		destroyed () {
			this.$store.dispatch('setPublicArr', '')
		},
		onLoad (options) {
			this.id = options.id
			this.title = options.name
		},
		methods: {
			toSearch () {
				this.queryParams1 = {
					pageSize: 10,
					pageNum: 1
				}
				this.currentPage1 = 1
				this.dataList2 = []
				this._easOutInBillPage()
			},
			async _easOutInBillPage () {
				let res = await easOutInBillPage(this.val1, this.id, this.queryParams1.pageNum + '', this.queryParams1.pageSize + '')
				this.checkArr1 = []
				res.result.map(el => {
					this.$set(el, 'checked', false)
				})
				this.dataList1 = [...res.result]
				this.totalCount1 = Number(res.totalElements)
				this.queryParams1.pageSize = Number(res.pageSize)
				this.queryParams1.pageNum = Number(res.pageNum)
			},
			async _easOutInBillDetailPage () {
				let res = await easOutInBillDetailPage(this.code, this.queryParams2.pageNum + '', this.queryParams2.pageSize + '')
				this.checkArr2 = []
				res.result.map(el => {
					this.$set(el, 'checked', false)
				})
				this.dataList2 = [...res.result]
				this.totalCount2 = Number(res.totalElements)
				this.queryParams2.pageSize = Number(res.pageSize)
				this.queryParams2.pageNum = Number(res.pageNum)
			},
			handlePageChange1(page) {
				this.queryParams1.pageNum = page
				this.currentPage1 = page
				this.dataList2 = []
				this._easOutInBillPage()
			},
			handlePageChange2(page) {
				this.queryParams2.pageNum = page
				this.currentPage2 = page
				this._easOutInBillDetailPage()
			},
			toCollapse (e) {
				this.pkId = this.pkId === e.id ? '' : e.id
				if (this.pkId === e.id) {
					this.queryParams2 = {
						pageSize: 10,
						pageNum: 1
					}
					this.currentPage2 = 1
					this.code = e.code
					this._easOutInBillDetailPage()
				}
			},
			toCheck1 (e) {
				e.checked = !e.checked
				let arr = this.dataList1.filter(el => el.checked === true)
				this.checkArr1 = arr.map(el => {return el.code})
			},
			async toSure () {
			  this.disabled1 = true
			  if (this.checkArr1.length === 0) {
				this.disabled1 = false
				return
			  }
			  try {
				let res = await easOutInBillUpdate(this.checkArr1) 
				this.disabled1 = false
				this.checkArr1 = []
				this.toSearch()
				uni.showToast({
					title: res.desc,
					icon: 'none'
				})
			  } catch (e) {
				this.disabled1 = false
			  }
			},
			toCheck2 (e) {
				e.checked = !e.checked
				this.checkArr2 = this.dataList2.filter(el => el.checked === true)
			},
			async _easOutInBillDetailUpdate () {
			  this.disabled2 = true
			  if (this.checkArr2.length === 0) {
				this.disabled2 = false
				return
			  }
			  try {
				let res = await easOutInBillDetailUpdate(this.checkArr2) 
				this.disabled2 = false
				this.checkArr2 = []
				this.queryParams2 = {
					pageSize: 10,
					pageNum: 1
				}
				this.currentPage2 = 1
				this._easOutInBillDetailPage()
				uni.showToast({
					title: res.desc,
					icon: 'none'
				})
			  } catch (e) {
				this.disabled2 = false
			  }
			},
			async _queryInventoryInfo () {
				let res = await queryInventoryInfo(this.checkArr2)
				res.result.map(el => {
					this.$set(el, 'checked', false)
					this.$set(el, 'yqty', el.qty)
				})
				this.dataList3 = [...res.result]
				this.show = true
			},
			handleBlur (ele) {
				console.log(this.dataList3)
				ele.checked = true
				let index = ''
				let obj = {}
				this.dataList3.map((e, i) => {
					if (e.id === ele.id) {
						index = i
						obj = Object.assign({}, e, {'checked': false}, {'ckmc': ''}, {id: e.id + Math.random() * 1000}, {qty: Number(e.yqty) - Number(ele.qty)}, {yqty: Number(e.yqty) - Number(ele.qty)})
					}
				})
				this.dataList3.splice(index+1, 0, obj)
				this.dataList3 = [...this.dataList3]
			},
			async _easOutInBillDetailUpdate1 () {
			  this.disabled3 = true
			  if (this.dataList3.length === 0) {
				this.disabled3 = false
				return
			  }
			  try {
				let res = await easOutInBillDetailUpdate(this.dataList3) 
				this.disabled3 = false
				this.show = false
				this.queryParams2 = {
					pageSize: 10,
					pageNum: 1
				}
				this.currentPage2 = 1
				this._easOutInBillDetailPage()
				uni.showToast({
					title: res.desc,
					icon: 'none'
				})
			  } catch (e) {
				this.disabled3 = false
			  }
			}
		}
	}
</script>

<style lang="stylus" scoped>
@import '../../common/style/mixin.styl';
</style>
