<template>
	<view class="content">
		<nav-bar :title="crType === 'IN' ? '入库' : '出库'"></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 || btnActive1}" :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-1 zd-th"><text>序号</text></view>
					<view class="zd-col-1 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-2 zd-th"><text>状态</text></view>
					<view class="zd-col-3 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-2 zd-th"><text>业务日期</text></view>
					<view class="zd-col-2 zd-th"><text>操作日期</text></view>
					<view class="zd-col-2 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-1 zd-td" @tap.stop="toCollapse(e)"><text>{{i+1}}</text></view>
						<view class="zd-col-1 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.djbh}}</text></view>
						<view class="zd-col-3 zd-td" @tap.stop="toCollapse(e)"><text>{{e.zzmc}}</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-2 zd-td" @tap.stop="toCollapse(e)"><text>{{e.cjsj.substring(0, 10)}}</text></view>
						<view class="zd-col-3 zd-td" @tap.stop="toCollapse(e)"><text>{{e.cjr}}</text></view>
						<view class="zd-col-2 zd-td" @tap.stop="toCollapse(e)"><text>{{e.ywrq.substring(0, 10)}}</text></view>
						<view class="zd-col-2 zd-td" @tap.stop="toCollapse(e)"><text>{{e.update_time}}</text></view>
						<view class="zd-col-2 zd-td" @tap.stop="toCollapse(e)"><text>{{e.update_name}}</text></view>
					</view>
					<!-- 二级表格start -->
					<view v-show="pkId === e.djid" 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-2 zd-sec-th"><text>物料名称</text></view>
							<view class="zd-col-3 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>
						<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.code}}</text></view>
							<view class="zd-col-2 zd-sec-td"><text>{{el.zzmc}}</text></view>
							<view class="zd-col-2 zd-sec-td"><text>{{el.ckmc}}</text></view>
							<view class="zd-col-2 zd-sec-td"><text>{{el.wlbm}}</text></view>
							<view class="zd-col-2 zd-sec-td"><text>{{el.wlmc}}</text></view>
							<view class="zd-col-3 zd-sec-td"><text>{{el.ggxh}}</text></view>
							<view class="zd-col-1 zd-sec-td"><text>{{el.jldw}}</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.sl}}</text></view>
							<view class="zd-col-2 zd-sec-td"><text>{{el.czsl}}</text></view>
							<view class="zd-col-2 zd-sec-td"><text>{{el.kcsl}}</text></view>
						</view>
						<view class="zd-row mgt10">
							<view class="zd-col-12 zd-row jcflexstart">
								<button class="confirm-button" :class="{'confirm-button_disabled': checkArr2.length === 0}" @tap="kuwConfirm">库位修改</button>
							</view>
							<view class="zd-col-12 zd-row jcflexend">
								<pagination :total="totalCount2" :page-size="queryParams2.pageSize" :current-page="currentPage2" @page-change="handlePageChange2" />
							</view>
						</view>
					</view>
					<!-- 二级表格end -->
				</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="{'popshow': show, 'pophide': !show, 'pop-wraper_1': type === '2'}">
			<view class="allwidth pop-title">{{type === '1' ?'库位修改' : '库位确认'}}</view>
			<view v-if="type === '1'" 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-5 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-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.wlbm}}</text></view>
					<view class="zd-col-5 zd-sec-td zd-pop-td"><text>{{ele.wlmc}}</text></view>
					<view class="zd-col-4 zd-sec-td zd-pop-td"><text>{{ele.tjkw}}</text></view>
					<view class="zd-col-5 zd-sec-td zd-pop-td">
						<scan-input
						v-model="ele.kwbm"
						/>
					</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.sl" @blur="handleBlur(ele)">
						<text v-show="ele.checked">{{ele.sl}}</text>
					</view>
				</view>
			</view>
			<view v-else class="zd-row jcenter kwcode_wraper">
				<view class="zd-col-12">
					<scan-input
					v-model="kwCode"
					/>
				</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': (type === '1' && dataList3.length === 0) || (type === '2' && kwCode === '')}" :disabled="disabled2" @tap="modalConfirm">确定</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: '',
				crType: '',
				val1: '',
				dataList1: [],
				dataList2: [],
				dataList3: [],
				pkId: '',
				pkObj: {},
				checkArr1: [],
				disabled1: false,
				btnActive1: false,
				totalCount1: 0,
				queryParams1:{
					pageSize: 10,
					pageNum: 1
				},
				currentPage1: 1,
				totalCount2: 0,
				queryParams2:{
					pageSize: 10,
					pageNum: 1
				},
				currentPage2: 1,
				checkArr2: [],
				disabled2: false,
				show: false,
				type: '',
				kwCode: ''
			};
		},
		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
			this.crType = options.type
		},
		created () {
			this.toSearch()
		},
		methods: {
			toSearch () {
				this.queryParams1 = {
					pageSize: 10,
					pageNum: 1
				}
				this.currentPage1 = 1
				this.dataList2 = []
				this.pkId = ''
				this._easOutInBillPage()
			},
			async _easOutInBillPage () {
				let res = await easOutInBillPage(this.val1, this.id, this.queryParams1.pageNum + '', this.queryParams1.pageSize + '')
				if (res.code === 1) {
					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)
				} else {
					uni.showToast({
						title: res.desc,
						icon: 'none'
					})
				}
			},
			async _easOutInBillDetailPage () {
				let res = await easOutInBillDetailPage(this.pkObj.djid, this.queryParams2.pageNum + '', this.queryParams2.pageSize + '')
				if (res.code === 1) {
					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)
				} else {
					uni.showToast({
						title: res.desc,
						icon: 'none'
					})
				}
			},
			handlePageChange1(page) {
				this.queryParams1.pageNum = page
				this.currentPage1 = page
				this.dataList2 = []
				this.pkId = ''
				this._easOutInBillPage()
			},
			handlePageChange2(page) {
				this.queryParams2.pageNum = page
				this.currentPage2 = page
				this._easOutInBillDetailPage()
			},
			toCollapse (e) {
				this.pkId = this.pkId === e.djid ? '' : e.djid
				this.pkObj = this.pkId === e.djid ? e : {}
				if (this.pkId === e.djid) {
					this.queryParams2 = {
						pageSize: 10,
						pageNum: 1
					}
					this.currentPage2 = 1
					this._easOutInBillDetailPage()
				}
			},
			toCheck1 (e) {
				e.checked = !e.checked
				let arr = this.dataList1.filter(el => el.checked === true)
				this.checkArr1 = arr.map(el => {return el.djid})
				if (arr.length === 1) {
					this.btnActive1 = arr[0].djzt === '提交' ? true : false
				} else {
					this.btnActive1 = false
				}
			},
			async toSure () {
			  this.disabled1 = true
			  if (this.checkArr1.length === 0 || this.btnActive1) {
				this.disabled1 = false
				return
			  }
			  try {
				let res = await easOutInBillUpdate(this.checkArr1)
				if (res.code === 1) {
					this.disabled1 = false
					this.checkArr1 = []
					this.toSearch()
					uni.showToast({
						title: res.desc,
						icon: 'none'
					})
				} else {
					this.disabled1 = false
					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)
			},
			kuwConfirm () {
				if (this.checkArr2.length === 0) {
					return
				} else if (this.checkArr2.length === 1) {
					this.type = '1'
					this._queryInventoryInfo()
				} else {
					this.type = '2'
					this.show = true
					this.kwCode = ''
				}
			},
			async _queryInventoryInfo () {
				let res = await queryInventoryInfo(this.checkArr2)
				if (res.code ===1) {
					res.result.map(el => {
						this.$set(el, 'checked', false)
						this.$set(el, 'yqty', el.sl)
					})
					this.dataList3 = [...res.result]
					this.show = true
				} else {
					uni.showToast({
						title: res.desc,
						icon: 'none'
					})
				}
			},
			handleBlur (ele) {
				if (Number(ele.sl) >= Number(ele.yqty)) {
					ele.sl = ele.yqty
					return
				}
				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}, {'kwbm': ''}, {id: e.id + Math.random() * 1000}, {sl: Number(e.yqty) - Number(ele.sl)}, {yqty: Number(e.yqty) - Number(ele.sl)})
					}
				})
				this.dataList3.splice(index+1, 0, obj)
				this.dataList3 = [...this.dataList3]
			},
			modalConfirm () {
				this.disabled2 = true
				let arr = []
				if (this.type === '1' && this.dataList3.length === 0) {
					this.disabled2 = false
					return
				} else if (this.type === '2' && this.kwCode === '') {
					this.disabled2 = false
					return
				} else if (this.type === '1') {
					arr = this.dataList3
				} else if (this.type === '2') {
					this.checkArr2.map(el => {
						el.kwbm = this.kwCode
					})
					arr = this.checkArr2 
				}
				this._easOutInBillDetailUpdate(arr)
			},
			async _easOutInBillDetailUpdate (arr) {
			  try {
				
				let res = await easOutInBillDetailUpdate(arr)
				if (res.code === 1) {
					this.disabled2 = false
					this.show = false
					this.queryParams2 = {
						pageSize: 10,
						pageNum: 1
					}
					this.currentPage2 = 1
					this._easOutInBillDetailPage()
					uni.showToast({
						title: res.desc,
						icon: 'none'
					})
				} else {
					this.disabled2 = false
					uni.showToast({
						title: res.desc,
						icon: 'none'
					})
				}
			  } catch (e) {
				this.disabled2 = false
			  }
			}
		}
	}
</script>

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