<template>
	<view class="content">
		<nav-bar title="收货确认"></nav-bar>
		<view class="sticky search-confirm-wrap" style="z-index: 11;">
			<view class="zd-row">
				<view class="zd-col-14 zd-row jcflexstart">
					<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>
				<view class="zd-col-10 zd-row jcflexend">
					<button class="confirm-button" @tap="toSearch">查询</button>
					<button class="confirm-button" :class="{'confirm-button_disabled': checkData.length === 0}" :disabled="disabled1" @tap="_feedbackSapJhd">确认收货</button>
				</view>
			</view>
		</view>
		<view class="grid-wrap">
			<view class="zdrow-wrap">
				<view class="zd-row sticky zd-th-wraper zd-bor-bottom">
					<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 class="fontcol1">交货单号</text></view>
					<view class="zd-col-2 zd-th"><text class="fontcol1">交货日期</text></view>
					<view class="zd-col-2 zd-th"><text class="fontcol1">凭证日期</text></view>
					<view class="zd-col-2 zd-th"><text class="fontcol1">计划日期</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">更多</view>
				</view>
				<view class="zd-td-wraper" v-for="(e, i) in dataList" :key="e.id">
					<view class="zd-row zd-bor-bottom" @tap="loadSubData(e)">
						<view class="zd-col-1 zd-td"><text>{{i+1}}</text></view>
						<view class="zd-col-1 zd-td" @tap.stop="toCheckbox(e)">
							<view class="zd-checkbox" :class="{'zd-checkbox_active': e.checked}">
								<uni-icons type="checkmarkempty" size="12" class="checkbox_icon"></uni-icons>
							</view>
						</view>
						<view class="zd-col-3 zd-td fontcol1"><text>{{e.vbeln}}</text></view>
						<view class="zd-col-2 zd-td fontcol1"><text>{{e.lfdat}}</text></view>
						<view class="zd-col-2 zd-td fontcol1"><text>{{e.bldat}}</text></view>
						<view class="zd-col-2 zd-td fontcol1"><text>{{e.wadat}}</text></view>
						<view class="zd-col-2 zd-td"><text>所属厂区</text></view>
						<view class="zd-col-2 zd-td"><text>{{e.lifnr}}</text></view>
						<view class="zd-col-2 zd-td"><text>供应商名称</text></view>
						<view class="zd-col-2 zd-td" @tap.stop="getDetails(5,e)">
							<uni-icons type="more-filled" size="14" color="#909399"></uni-icons>
						</view>
					</view>
					<!-- 副表格start -->
					<view v-if="e.subData.length > 0" class="zd-sec-wraper">
						<view class="search-confirm-wrap_2">
							<view class="zd-row">
								<view class="zd-col-24 zd-row jcflexstart">
									<view class="zd-col-9"><search-box v-model="val2" :scanShow="false" placeholder="请输入物料信息" /></view>
								</view>
								<button class="confirm-button" @tap="toSubSearch(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 class="fontcol1">采购订单号</th>
										<th>外部项目号</th>
										<th>物料编码</th>
										<th>物料名称</th>
										<th>单位</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><view class="underline fontcol1">{{el.ebeln}}</view></td>
										<td>{{el.posnr}}</td>
										<td>{{el.material}}</td>
										<td>{{el.matlDesc}}</td>
										<td>{{el.unit}}</td>
										<td>订单量</td>
										<td>总数</td>
										<td>本次送货数量</td>
										<td>收货组织</td>
										<td>是否质检</td>
										<td>{{el.delivQty}}</td>
										<td>收货仓库编码</td>
										<td>收货仓库名称</td>
										<td @tap.stop="getDetails(6,el,e)"><uni-icons type="more-filled" size="14" color="#909399"></uni-icons></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>
		<grid-detail :show="delShow" :data="detailObj" :mainRow="mainRow"  @closeDetail="delShow=false" @update‑rsitems="handleUpdateRsItems"></grid-detail>
		<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 GridDetail from '@/components/ReceiveDetail.vue'
	import {getOrganizationInfo, getWarehouseInfo, querySapJhd, querySapJhdMx, feedbackSapJhd, updateSapJhdMx} from '@/utils/getData2.js'
	export default {
		components: {
			NavBar,
			SearchBox,
			ScanInput,
			Pagination,
			GridDetail
		},
		data() {
			return {
				crType: '',
				val1: '', // 模糊查询
				value1: '', // 收货组织信息
				range1: [],
				totalCount1: 0, // 分页
				queryParams1:{
					pageSize: 10,
					pageNum: 1
				},
				currentPage1: 1,
				dataList: [], // 一级表格数据
				checkData: [], // 一级表格多选数组
				val2: '',
				popData: [], // 副表格点击物料编码弹出弹窗
				popdisabled: false,
				popObj: {}, // 弹窗当前主表格内容
				show: false, // 弹窗显示布尔值
				type: '', // 弹窗类型
				delShow: false, // 详情弹窗布尔值
				detailObj: {}, // 详情弹窗
				mainRow: {}, // 一级表格行
				disabled1: false,
				value2: '', // 收货仓库信息
				range2: [],
				subCheckData: [] // 二级表格多选数组
			}
		},
		onLoad (options) {
			this.crType = options.type
			this.toSearch()
			// setTimeout(() => {
			// 	this._getWarehouseInfo()
			// 	this._getOrganizationInfo()
			// }, 3000)
		},
		methods: {
			handleInnerScroll(event) {
				event.stopPropagation()
			},
			// 组织信息下拉框
			async _getOrganizationInfo () {
				let res = await getOrganizationInfo()
				if (res.code === 1) {
					this.range1 = [...res.result]
					this.range1.map(e => {
						this.$set(e, 'text', e.kczzmc)
						this.$set(e, 'value', e.kczzbm)
					})
				}
			},
			// 仓库下拉框
			async _getWarehouseInfo () {
				let res = await getWarehouseInfo()
				if (res.code === 1) {
					this.range2 = [...res.result]
					this.range2.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.pkId = ''
				this._querySapJhd()
			},
			// 一级表格接口
			async _querySapJhd (e) {
				let res = await querySapJhd(this.val1, this.queryParams1.pageNum + '', this.queryParams1.pageSize + '')
				if (res.code === 1) {
					res.result.map(el => {
						this.$set(el, 'checked', false)
						this.$set(el, 'subData', [])
						this.$set(el, 'subAllChecked', false)
						this.$set(el, 'subOneChecked', false)
						this.$set(el, 'subStatus1', false)
					})
					this.dataList = [...res.result]
					this.totalCount1 = Number(res.totalElements)
					this.queryParams1.pageSize = Number(res.pageSize)
					this.queryParams1.pageNum = Number(res.pageNum)
					if (e) {
						this._querySapJhdMx(e)
					}
				} else {
					uni.showToast({
						title: res.desc,
						icon: 'none'
					})
				}
			},
			// 一级表格切换分页
			handlePageChange1(page) {
				this.queryParams1.pageNum = page
				this.currentPage1 = page
				this._querySapJhd()
			},
			// 一级表格多选
			toCheckbox (e) {
				e.checked = !e.checked
				let arr = this.dataList.filter(el => el.checked === true)
				this.checkData = arr.map(el => {
					return {
						vbeln: el.vbeln
					}
				})
			},
			// 点击一级表格一行加载副表格
			loadSubData (e) {
				if (e.subData.length > 0) {
					e.subData = []
				} else if (e.subData.length === 0) {
					this._querySapJhdMx(e)
				}
				e.subAllChecked = false
				e.subOneChecked = false
				e.subStatus1 = false
			},
			// 副表格接口
			async _querySapJhdMx (e) {
				let res = await querySapJhdMx(this.val2, e.id, '1', '200')
				if (res.code === 1) {
					res.result.map(el => {
						this.$set(el, 'checked', false)
					})
					this.dataList.map(el => {
						if (el.id === e.id) {
							el.subData = [...res.result]
						}
					})
					this.toSubAllCheckbox(e)
				} else {
					uni.showToast({
						title: res.desc,
						icon: 'none'
					})
				}
			},
			// 副表格全选
			toSubAllCheckbox (e) {
				e.subAllChecked = !e.subAllChecked
				e.subOneChecked = e.subAllChecked
				e.subData.map(el => {
					el.checked = e.subAllChecked 
				})
				let arr = e.subData.filter(el => el.ckbm !== '')
				e.subStatus1 = e.subAllChecked && e.subData.length > 0 && e.subData.length === arr.length
			},
			// 副表格多选
			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
				let arr1 = arr.filter(el => el.ckbm !== '')
				e.subStatus1 = arr.length > 0 && arr.length === arr1.length
			},
			// 副表格查询
			toSubSearch (e) {
				e.subAllChecked = false
				e.subOneChecked = false
				this._querySapJhdMx(e)
			},
			// 详情弹窗
			getDetails (type,e, mainRow) {
				this.delShow = !this.delShow
				this.detailObj = {type: type, data: {...e}}
				this.mainRow = mainRow
			},
			// 点击确认收货按钮
			async _feedbackSapJhd () {
				this.disabled1 = true
				if (!this.checkData.length) {
					this.disabled1 = false
					return
				}
				uni.showModal({
					title: '提示',
					content: '是否确认收货？',
					confirmColor: '#fff',
					success: (res) => {
						if (res.confirm) {
							this.__feedbackSapJhd()
						} else if (res.cancel) {
							this.disabled1 = false
						}
					}
				})
			},
			async __feedbackSapJhd () {
				try {
					let res = await feedbackSapJhd(this.checkData)
					if (res.code === 1) {
						this.disabled1 = false
						this.checkData = []
						this.subCheckData = []
						this._querySapJhd()
					} else {
						this.disabled1 = false
						uni.showToast({
							title: res.desc,
							icon: 'none'
						})
					}
				} catch (e) {
					this.disabled1 = false
				}
			},
			// 副表格查看更多数量修改
			handleUpdateRsItems (newJsonStr, parent, detailItemId, rawOldJsonStr){
				this._updateSapJhdMx(newJsonStr, parent, detailItemId, rawOldJsonStr)
			},
			async _updateSapJhdMx (newRsJsonStr, parent, detailItemId, rawOldJsonStr) {
				try {
					let targetDetail = null
					this.dataList.some(mainRow=>{
						if(mainRow.id === parent.id){
							targetDetail = mainRow.subData.find(sub=> sub.id === detailItemId)
							return true
						}
					})
			
					if(!targetDetail){
						uni.showToast({title:'找不到该行数据',icon:'none'})
						return
					}
			
					const submitRow = {...targetDetail}
					submitRow.rsItems = newRsJsonStr
					
					let res = await updateSapJhdMx(submitRow)
					if (res.code === 1) {
						setTimeout(() => {
							this._querySapJhd(parent)
						}, 1500)
					} else {
						this.dataList.map(mainRow => {
							if (mainRow.id === parentId) {
								mainRow.subData.map(subItem => {
									if(subItem.id === detailItemId){
										this.$set(subItem, 'rsItems', rawOldJsonStr)
									}
								})
							}
						})
					}
					uni.showToast({
						title: res.desc,
						icon: 'none'
					})
				} catch (e) {
					console.log(e)
				}
			}
		}
	}
</script>

<style lang="stylus" scoped>
@import '../../common/style/mixin.styl';
.confirm-button_1
	min-width 100px
.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 #dcdfe6;
		background-color: #fff;
		border-radius: 6px;
	/deep/ .uni-select__input-placeholder, /deep/ .uni-select__input-text
		font-size 13px
.zd-th
	font-size: 12px;
	line-height: 13px;
	height: 36px;
</style>
