<template>
	<view class="content">
		<nav-bar title="移库"></nav-bar>
		<view class="sticky search-confirm-wrap">
			<view class="zd-row">
				<view class="zd-row zd-col-14">
					<view class="zd-col-12"><search-box placeholder="输入查询条件" v-model="val1" /></view>
					<view class="zd-col-12 select_wraper mgl10">
						<zxz-uni-data-select placeholder="选择仓库" v-model="value1" filterable :localdata="range1"></zxz-uni-data-select>
					</view>
				</view>
				<button class="confirm-button" @tap="toSearch">查询</button>
			</view>
		</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-3 zd-th"><text class="fontcol1">移库单号</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-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 class="zd-col-2 zd-th"><text>业务日期</text></view>
					<view class="zd-col-4 zd-th"><text>备注</text></view>
				</view>
				<view class="zd-td-wraper" v-for="(e, i) in dataList" :key="e.id">
					<view class="zd-row" @tap="loadSubData(e)">
						<view class="zd-col-1 zd-td"><text>{{i+1}}</text></view>
						<view class="zd-col-3 zd-td fontcol1"><text>{{e.bill_code}}</text></view>
						<view class="zd-col-3 zd-td"><text>{{e.org_name}}</text></view>
						<view class="zd-col-3 zd-td"><text>{{e.stor_name}}</text></view>
						<view class="zd-col-2 zd-td"><text>{{e.total_qty}}</text></view>
						<view class="zd-col-2 zd-td"><text>{{e.bill_status}}</text></view>
						<view class="zd-col-2 zd-td"><text>{{e.input_time}}</text></view>
						<view class="zd-col-2 zd-td"><text>{{e.input_name}}</text></view>
						<view class="zd-col-2 zd-td"><text>{{e.biz_date}}</text></view>
						<view class="zd-col-4 zd-td"><text>{{e.remark}}</text></view>
					</view>
					<!-- 二级表格start -->
					<view v-if="e.subData.length > 0" class="zd-sec-wraper">
						<view class="search-confirm-wrap_2">
							<view class="zd-row jcflexend">
								<button class="confirm-button" :class="{'confirm-button_disabled': !e.subOneChecked}" :disabled="disabled1" @tap="_stIvtMoveinvConfirmMove(e)">确认移库</button>
							</view>
						</view>
						<view class="table-wrap table-wrap_1" :style="{height: e.subData.length >= 5 ? '230px' : (30 + 40 * e.subData.length) + 'px'}" @touchmove="handleInnerScroll">
							<table class="grid-table">
								<thead>
									<tr>
										<th>序号</th>
										<th>
											<view class="zd-row jcenter">
												<view class="zd-checkbox zd-checkbox_i" :class="{'zd-checkbox_active': e.subAllChecked}" @tap="toSubAllCheckbox(e)"></view>
											</view>
										</th>
										<th>物料编码</th>
										<th>物料名称</th>
										<th>移出库位编码</th>
										<th>移出库位名称</th>
										<th>移动数量</th>
										<th>移入库位编码</th>
										<th>移入库位名称</th>
										<th>单位</th>
										<th>备注</th>
									</tr>
								</thead>
								<tbody>
									<tr v-for="(el, j) in e.subData" :key="el.id">
										<td>{{j+1}}</td>
										<td @tap="toSubCheckbox(e, el)">
											<view class="zd-row jcenter">
												<view class="zd-checkbox zd-checkbox_i" :class="{'zd-checkbox_active': el.checked}"></view>
											</view>
										</td>
										<td>{{el.material_code}}</td>
										<td>{{el.material_name}}</td>
										<td>{{el.turnout_struct_code}}</td>
										<td>{{el.turnout_struct_name}}</td>
										<td><input class="td_input" type="number" confirm-type="go" v-model="el.qty" @focus="focusInput(e, el)" @blur="updateNum(e, el)"  @confirm="updateNum(e, el)"/></td>
										<td>
											<view class="td_scan_wraper">
												<scan-input v-model="el.turnin_struct_code" :maindata="e" :secdata="el" @handleFocus="handleFocus" @handleChange="handleChangekw" />
											</view>
										</td>
										<td>{{el.turnin_struct_name}}</td>
										<td>{{el.unit}}</td>
										<td>{{el.remark}}</td>
									</tr>
								</tbody>
							</table>
						</view>
					</view>
					<!-- 二级表格end -->
				</view>
			</view>
			<view class="zd-row jcenter pd20">
				<pagination v-show="dataList.length > 0" :total="totalCount1" :page-size="queryParams1.pageSize" :current-page="currentPage1" @page-change="handlePageChange1" />
			</view>
		</view>
		<view class="footer"></view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import ScanInput from '@/components/ScanInput_new.vue'
	import Pagination from '@/components/Pagination.vue'
	import GridDetail from '@/components/GridDetail.vue'
	// import {getWarehouseInfo, stIvtMoveinvMovePage, moveDtlByMoveId} from '@/utils/mork2.js'
	import {getWarehouseInfo, stIvtMoveinvMovePage, moveDtlByMoveId, stIvtMoveinvUpdateDtl, stIvtMoveinvConfirmMove} from '@/utils/getData2.js'
	export default {
		components: {
			NavBar,
			SearchBox,
			ScanInput,
			Pagination,
			GridDetail
		},
		data() {
			return {
				val1: '', // 模糊查询
				value1: '', // 仓库信息
				range1: [],
				totalCount1: 0, // 分页
				queryParams1:{
					pageSize: 10,
					pageNum: 1
				},
				currentPage1: 1,
				dataList: [], // 一级表格数据
				disabled1: false
			};
		},
		onLoad () {
			this.toSearch()
			setTimeout(() => {
				this._getWarehouseInfo()
			}, 3000)
		},
		methods: {
			handleInnerScroll(event) {
				event.stopPropagation()
			},
			// 仓库下拉框
			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)
					})
				}
			},
			// 查询一级表格
			toSearch () {
				this.queryParams1 = {
					pageSize: 10,
					pageNum: 1
				}
				this.currentPage1 = 1
				this._stIvtMoveinvMovePage()
			},
			// 一级表格接口
			async _stIvtMoveinvMovePage (e) {
				let res = await stIvtMoveinvMovePage(this.val1, this.value1, this.queryParams1.pageNum + '', this.queryParams1.pageSize + '')
				if (res.code === 200) {
					res.content.map(el => {
						this.$set(el, 'checked', false)
						this.$set(el, 'subData', [])
						this.$set(el, 'subAllChecked', false)
						this.$set(el, 'subOneChecked', false)
					})
					this.dataList = [...res.content]
					this.totalCount1 = Number(res.totalElements)
					this.queryParams1.pageSize = Number(this.queryParams1.pageSize + '')
					this.queryParams1.pageNum = Number(this.queryParams1.pageNum + '')
					if (e) {
						this._moveDtlByMoveId(e)
					}
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			},
			// 一级表格切换分页
			handlePageChange1(page) {
				this.queryParams1.pageNum = page
				this.currentPage1 = page
				this._stIvtMoveinvMovePage()
			},
			// 点击一级表格一行加载二级表格
			loadSubData (e) {
				if (e.subData.length > 0) {
					e.subData = []
				} else if (e.subData.length === 0) {
					this._moveDtlByMoveId(e)
				}
				e.subAllChecked = false
				e.subOneChecked = false
			},
			// 二级表格接口
			async _moveDtlByMoveId (e) {
				let res = await moveDtlByMoveId(e.id)
				res.map(el => {
					this.$set(el, 'checked', false)
				})
				this.dataList.map(el => {
					if (el.djid === e.djid) {
						el.subData = [...res]
					}
				})
			},
			// 二级表格全选
			toSubAllCheckbox (e) {
				e.subAllChecked = !e.subAllChecked
				e.subOneChecked = e.subAllChecked
				e.subData.map(el => {
					el.checked = e.subAllChecked
				})
			},
			// 二级表格多选
			toSubCheckbox (e,el) {
				if (el.checked) {
					this.$set(el, 'checked', false)
				} else if (!el.checked)  {
					this.$set(el, 'checked', true)
				}
				let arr = e.subData.filter(ele => ele.checked === true)
				e.subAllChecked = arr.length === e.subData.length
				e.subOneChecked = arr.length > 0
			},
			// 副表格单行数量获取焦点
			focusInput (e, el) {
				this.raw = el.qty
			},
			// 修改副表格单行移动数量
			updateNum (e, el) {
				this._stIvtMoveinvUpdateDtl(e, el, 'sl')
			},
			// 副表格单行库位编码获取焦点
			handleFocus (val, e, el) {
				this.raw = el.turnin_struct_code
				this.popObj = e
				this.rawObj = el
			},
			// 修改副表格单行库位
			handleChangekw (e) {
				this._stIvtMoveinvUpdateDtl(this.popObj, this.rawObj, 'kw')
			},
			// 二级表格单行收货数量和收货仓库接口
			async _stIvtMoveinvUpdateDtl (e, el, type) {
				try {
					let res = await stIvtMoveinvUpdateDtl(el)
					if (res.code === 1) {
						setTimeout(() => {
							this._moveDtlByMoveId(e)
						}, 1500)
					} else {
						this.dataList.map(ele => {
							if (e.id === ele.id) {
								e.subData.map(elem => {
									if (elem.id === el.id) {
										if (type === 'sl') {
											elem.qty = this.raw
										}
										if (type === 'kw') {
											elem.turnin_struct_code = this.raw
										}
									}
								})
							}
						})
					}
					uni.showToast({
						title: res.desc,
						icon: 'none'
					})
				} catch (e) {
					console.log(e)
				}
			},
			// 确认移库按钮
			async _stIvtMoveinvConfirmMove (e) {
				this.disabled1 = true
				if (!e.subOneChecked) {
					this.disabled1 = false
					return
				}
				let arr = e.subData.filter(ele => ele.checked === true)
				try {
					let res = await stIvtMoveinvConfirmMove(arr)
					if (res.code === 1) {
						this._stIvtMoveinvMovePage(e)
					}
					this.disabled1 = false
					uni.showToast({
						title: res.desc,
						icon: 'none'
					})
				} catch (e) {
					this.disabled1 = false
				}
			}
		}
	}
</script>

<style lang="stylus" scoped>
@import '../../common/style/mixin.styl';
.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
	/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
.zd-th
	font-size: 12px;
	line-height: 13px;
	height: 36px;
.grid-table th:last-child, .grid-table td:last-child
	position: inherit;
	box-shadow: none;
.grid-table td:last-child
	background-color: rgba(63,106,202,0.3);
</style>
