<template>
	<view class="content">
		<nav-bar :title="crType === 'IN' ? '入库' : '出库'"></nav-bar>
		<view class="search-confirm-wrap">
			<view class="zd-row">
				<view class="zd-col-24 zd-row jcflexstart">
					<view class="zd-col-8"><search-box v-model="val1" /></view>
					<view class="zd-col-8 select_wraper mgl10">
						<zxz-uni-data-select placeholder="请选择仓库名称" v-model="value1" filterable :localdata="range1"></zxz-uni-data-select>
					</view>
					<view v-if="crType === 'OUT'" class="zd-col-8 select_wraper mgl10">
						<zxz-uni-data-select placeholder="请选择仓管员" v-model="value2" filterable :localdata="range2"></zxz-uni-data-select>
					</view>
				</view>
				<button class="confirm-button" @tap="toSearch">查询</button>
				<button class="confirm-button" :class="{'confirm-button_disabled': checkArr1.length === 0}" :disabled="disabled1" @tap="toSure">单据审核</button>
			</view>
		</view>
		<view class="zd-row jcflexstart table-title_wraper">{{title}}</view>
		<view class="grid-wrap">
			<view class="zdrow-wrap">
				<view class="zd-row sticky 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-2 zd-th"><text class="fontcol1">{{crType === 'IN'? '入库单号':'出库单号'}}</text></view>
					<view class="zd-col-2 zd-th nowrap"><text>来源部门</text></view>
					<view class="zd-col-2 zd-th nowrap"><text>去向部门</text></view>
					<view class="zd-col-2 zd-th nowrap"><text>仓库名称</text></view>
					<view class="zd-col-2 zd-th"><text>状态</text></view>
					<view class="zd-col-2 zd-th nowrap"><text>制单日期</text></view>
					<view class="zd-col-2 zd-th nowrap"><text>制单人</text></view>
					<view class="zd-col-2 zd-th nowrap"><text>业务日期</text></view>
					<view v-if="crType === 'OUT'" class="zd-col-2 zd-th"><text>仓管员</text></view>
					<view class="zd-col-4 zd-th"><text>审核结果</text></view>
					<view class="zd-th" :class="crType === 'OUT' ? 'zd-col-1' : 'zd-col-2'">更多</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}" @tap="toCollapse(e)">
						<view class="zd-col-1 zd-td"><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-2 zd-td fontcol1"><text>{{e.djbh}}</text></view>
						<view class="zd-col-2 zd-td"><text>{{e.bmmc}}</text></view>
						<view class="zd-col-2 zd-td"><text>{{e.zzmc}}</text></view>
						<view class="zd-col-2 zd-td"><text>{{e.ckmc}}</text></view>
						<view class="zd-col-2 zd-td"><text class="fontbg1">{{e.djzt}}</text></view>
						<view class="zd-col-2 zd-td nowrap"><text>{{e.cjsj}}</text></view>
						<view class="zd-col-2 zd-td"><text>{{e.cjr}}</text></view>
						<view class="zd-col-2 zd-td nowrap"><text>{{e.ywrq}}</text></view>
						<view v-if="crType === 'OUT'" class="zd-col-2 zd-td"><text>{{e.cgymc}}</text></view>
						<view class="zd-col-4 zd-td"><text>{{e.shjg}}</text></view>
						<view class="zd-td" :class="crType === 'OUT' ? 'zd-col-1' : 'zd-col-2'" @tap.stop="getDetails(1,e)">
							<uni-icons type="more-filled" size="14" color="#fff"></uni-icons>
						</view>
					</view>
					<!-- 二级表格start -->
					<view v-if="pkId === e.djid" class="zd-sec-wraper pd0">
						<view class="table-wrap">
							<table class="grid-table">
								<thead>
									<tr>
										<th>序号</th>
										<th>选择</th>
										<th>仓库名称</th>
										<th>物料编号</th>
										<th>物料名称</th>
										<th>单位</th>
										<th>库位</th>
										<th>数量</th>
										<th>总数</th>
										<th>{{crType === 'IN'? '已入数量':'已出数量'}}</th>
										<th>剩余数量</th>
										<th>库存</th>
										<th>跟踪号</th>
										<th>操作人</th>
										<th v-if="crType === 'OUT'">仓管员</th>
										<th>操作日期</th>
										<th>更多</th>
									</tr>
								</thead>
								<tbody>
									<tr v-for="(el, j) in dataList2" :key="el.id">
										<td>{{j+1}}</td>
										<td @tap="toCheck2(el)">
											<view class="zd-row jcenter">
												<view class="zd-checkbox" :class="{'zd-checkbox_active': el.checked}"></view>
											</view>
										</td>
										<td>{{el.ckmc}}</td>
										<td>{{el.wlbm}}</td>
										<td>{{el.wlmc}}</td>
										<td>{{el.jldw}}</td>
										<td>{{el.kwbm}}</td>
										<td>{{el.sl}}</td>
										<td>{{el.wlzs}}</td>
										<td>{{el.czsl}}</td>
										<td>{{el.sysl}}</td>
										<td>{{el.kcsl}}</td>
										<td>{{el.gzhbm}}</td>
										<td>{{el.update_name}}</td>
										<td v-if="crType === 'OUT'">{{el.cgymc}}</td>
										<td>{{el.update_time}}</td>
										<td @tap.stop="getDetails(2,el)">
											<uni-icons type="more-filled" size="14" color="#fff"></uni-icons>
										</td>
									</tr>
								</tbody>
							</table>
						</view>
						<view class="zd-row jcflexstart mgt20 mgb20">
							<view class="zd-col-1 zd-row jcenter mgl52">
								<view class="zd-checkbox" :class="{'zd-checkbox_active': checkArrActive}" @tap="selectAll"></view>
							</view>
							<button class="confirm-button" :class="{'confirm-button_disabled': checkArr2.length === 0}" @tap="kuwConfirm">{{crType === 'IN' ? '入库' : '出库'}}</button>
							<button v-if="crType === 'IN'" class="confirm-button" @tap="onekeyUpdate">一键设置</button>
							<view class="zd-col-10">
								<!-- <view class="zd-col-12 zd-row jcflexend">
									<pagination :total="totalCount2" :page-size="queryParams2.pageSize" :current-page="currentPage2" @page-change="handlePageChange2" />
								</view> -->
							</view>
						</view>
					</view>
					<!-- 二级表格end -->
				</view>
			</view>
			<view class="zd-row jcenter pd20">
				<pagination v-show="dataList1.length > 0" :total="totalCount1" :page-size="queryParams1.pageSize" :current-page="currentPage1" @page-change="handlePageChange1" />
			</view>
		</view>
		<view class="footer"></view>
		<grid-detail :show="delShow" :data="detailObj" :crType="crType" @closeDetail="delShow=false"></grid-detail>
		<view class="pop-wraper" :class="{'popshow': show, 'pophide': !show}">
			<view v-show="type === '1'" class="allwidth pop-title">{{crType === 'IN' ? '入库' : '出库'}}</view>
			<view v-show="type === '3'" class="allwidth pop-title">批量设置入库库位</view>
			<view v-if="type === '1'" class="pop-grid-wraper">
				<view class="zd-row sticky 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-5 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>
				<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-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.sysl" @blur="handleBlur(ele)">
						<text v-show="ele.checked">{{ele.sysl}}</text>
					</view> -->
					<view class="zd-col-4 zd-sec-td zd-pop-td">
						<input type="number" class="pop-input" v-model="ele.sysl">
					</view>
					<view class="zd-col-4 zd-sec-td zd-pop-td"><text>{{ele.kcsl}}</text></view>
				</view>
			</view>
			<view v-if="type === '3'" class="kwcode_wraper">
				<view class="zd-row jcenter mgt20">
					<view class="zd-col-3 pop_label">入库库位</view>
					<view class="zd-col-10 select_wraper">
						<scan-input v-model="val3" />
					</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="mgb20 zd-row jcenter">
				<view class="zd-col-10 zd-row">
					<button class="confirm-button confirm-button_cancle mgl0" @tap="show = false">取消</button>
					<button v-if="type === '3'" class="confirm-button mgl0" :class="{'confirm-button_disabled': val3 === ''}" :disabled="disabled2" @tap="modalConfirm1">确定</button>
					<button v-else class="confirm-button mgl0" :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>
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog type="info" cancelText="关闭" confirmText="确定" title="提示" :content="crType === 'IN'? '是否确认入库？':'是否确认出库？'" @confirm="dialogConfirm" @close="dialogClose"></uni-popup-dialog>
		</uni-popup>
	</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 GridDetail from '@/components/GridDetail.vue'
	// import {updateBillDetail} from '@/utils/getData2.js'
	import {getWarehouseInfo, getUserInfo, easOutInBillPage, easOutInBillDetailPage, easOutInBillUpdate, easOutInBillDetailUpdate, queryInventoryInfo, updateBillDetail} from '@/utils/getData2.js'
	export default {
		components: {
			NavBar,
			SearchBox,
			ScanInput,
			Pagination,
			GridDetail
		},
		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: 500,
					pageNum: 1
				},
				currentPage2: 1,
				checkArr2: [],
				checkArrActive: false,
				disabled2: false,
				show: false,
				type: '',
				kwCode: '',
				checkArr3: [],
				delShow: false,
				detailObj: {},
				value1: '',
				range1: [],
				value2: '',
				range2: [],
				val3: ''
			};
		},
		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
			if (this.crType === 'IN') {
				this._getWarehouseInfo()
			} else {
				this._getUserInfo()
			}
		},
		methods: {
			// 仓库下拉框
			async _getWarehouseInfo () {
				let res = await getWarehouseInfo()
				if (res.code === 1) {
					this.range1 = [...res.result]
					this.range1.map(e => {
						this.$set(e, 'text', e.ckmc)
						this.$set(e, 'value', e.ckbm)
					})
					this.toSearch()
				}
			},
			// 仓管员下拉框
			async _getUserInfo () {
				let res = await getUserInfo()
				if (res.code === 1) {
					this.range2 = [...res.result]
					if (this.$store.getters.userInfo !== '') {
						let flag = this.range2.includes({text: JSON.parse(this.$store.getters.userInfo).person_name, value: JSON.parse(this.$store.getters.userInfo).username})
						if (!flag) {
							this.range2.push({text: JSON.parse(this.$store.getters.userInfo).person_name, value: JSON.parse(this.$store.getters.userInfo).username})
						}
						this.value2 = JSON.parse(this.$store.getters.userInfo).username
					}
					this._getWarehouseInfo()
				}
			},
			// 查询一级表格
			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.value1, this.value2, 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 = []
					this.checkArrActive = false
					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 : {}
				this.dataList2 = []
				if (this.pkId === e.djid) {
					this.queryParams2 = {
						pageSize: 500,
						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
			 //  }
			 if (this.checkArr1.length === 0) {
			 				this.disabled1 = false
			 				return
			 }
			  try {
				let res = await easOutInBillUpdate(this.checkArr1)
				if (res.code === 1) {
					this.disabled1 = false
					this.checkArr1 = []
					this._easOutInBillPage()
					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)
				if (this.checkArr2.length === this.dataList2.length) {
					this.checkArrActive = true
				}
			},
			// 二级表格全选
			selectAll () {
				this.checkArrActive = !this.checkArrActive
				this.dataList2.map(el => {
					el.checked = this.checkArrActive
				})
				this.checkArr2 = this.dataList2.filter(el => el.checked === true)
			},
			// 点击出入库按钮
			kuwConfirm () {
				if (this.checkArr2.length === 0) {
					return
				} else if (this.checkArr2.length === 1) {
					if ((Number(this.checkArr2[0].sl) > Number(this.checkArr2[0].kcsl)) && this.crType === 'OUT') {
						uni.showToast({
							title: '库存不足',
							icon: 'none'
						})
						return
					} else {
						this.type = '1'
						this._queryInventoryInfo()
					}
				} else if (this.checkArr2.length >= 2) {
					let flag = false
					this.checkArr2.map(el => {
						if (Number(el.sl) > Number(el.kcsl)) {
							flag = true
						}
					})
					if (flag && this.crType === 'OUT') {
						uni.showToast({
							title: '库存不足',
							icon: 'none'
						})
						return
					} else {
						this.type = '2'
						// this.show = true
						// this.kwCode = ''
						this.$refs.alertDialog.open()
					}
				}
			},
			// 选择二级表格一行，点击出入库按钮，弹窗加载出入库表格
			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.sysl)
					})
					this.dataList3 = [...res.result]
					this.show = true
				} else {
					uni.showToast({
						title: res.desc,
						icon: 'none'
					})
				}
			},
			// 出入库表格输入框计算，功能暂时隐藏
			handleBlur (ele) {
				if (Number(ele.sysl) >= Number(ele.yqty)) {
					ele.sysl = 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}, {sysl: Number(e.yqty) - Number(ele.sysl)}, {yqty: Number(e.yqty) - Number(ele.sysl)})
					}
				})
				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
					this.checkArr3 = [...this.dataList3]
					this.$refs.alertDialog.open()
					this.disabled2 = false
					return
				} else if (this.type === '2') {
					this.checkArr2.map(el => {
						el.kwbm = this.kwCode
					})
					arr = [...this.checkArr2]
				}
				this._easOutInBillDetailUpdate(arr)
			},
			// 提示弹窗确定按钮
			dialogConfirm () {
				if (this.type === '2') {
					this._easOutInBillDetailUpdate(this.checkArr2)
				} else {
					this._easOutInBillDetailUpdate(this.checkArr3)
				}
			},
			dialogClose () {},
			// 出入库确定接口
			async _easOutInBillDetailUpdate (arr) {
			  try {
				
				let res = await easOutInBillDetailUpdate(arr)
				if (res.code === 1) {
					this.disabled2 = false
					this.show = false
					this.queryParams2 = {
						pageSize: 500,
						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
			  }
			},
			// 详情弹窗
			getDetails (type,e) {
				this.delShow = !this.delShow
				this.detailObj = {type: type, data:e}
			},
			// 一键设置
			onekeyUpdate () {
				this.type = '3'
				this.val3 = ''
				this.show = true
			},
			async _updateBillDetail (e) {
				try {
					let res = await updateBillDetail(e)
					if (res.code === 1) {
						uni.showToast({
							title: res.desc,
							icon: 'none'
						})
					}
					this.disabled2 = false
				} catch (e) {
					console.log(e)
					this.disabled2 = false
				}
			},
			// 一键设置弹窗确认
			modalConfirm1 () {
				this.disabled2 = true
				if (this.val3 === '') {
					uni.showToast({
						title: '请设置入库库位',
						icon: 'none'
					})
					this.disabled2 = false
					return
				}
				let arr = []
				this.dataList2.map(e => {
					e.kwbm = this.val3
					let {djid, flid, kwbm} = {...e}
					let obj = {djid, flid, kwbm}
					arr.push(obj)
				})
				this.show = false
				this._updateBillDetail(arr)
			}
		}
	}
</script>

<style lang="stylus" scoped>
@import '../../common/style/mixin.styl';
.mgr30
	margin-right 30rpx
.select_wraper
	/deep/ .uni-select__input-box
		height 30px
	/deep/ .uni-select__selector
		z-index 11
	/deep/ .uni-select
		height: 30px;
		min-height: 30px;
		line-height: 30px;
		padding: 0 2px 0 12px;
		border: 1px solid #4980bd;
		background-color: rgba(45,88,184,0.1);
		border-radius: 6px;
	/deep/ .uni-select__input-placeholder, /deep/ .uni-select__input-text
		font-size 13px
		color #fff
	/deep/ .uni-select__selector
		background-color #071F3E
		border-color #0c3162
	/deep/ .uni-popper__arrow
		border-bottom-color #0c3162
		&:after
			border-bottom-color #071F3E
	/deep/ .uni-select__selector-item
		color #fff
</style>
