<template>
	<view class="content">
		<nav-bar title="移库"></nav-bar>
		<view class="search-confirm-wrap">
			<view class="zd-row jcflexstart">
				<view class="zd-col-7"><search-box placeholder="输入查询条件" v-model="val1" /></view>
				<view class="zd-col-7 select_wraper mgl10">
					<zxz-uni-data-select placeholder="选择仓库" v-model="value1" filterable :localdata="range1"></zxz-uni-data-select>
				</view>
				<button class="confirm-button" @tap="toSearch">查询</button>
			</view>
		</view>
		<view class="zd-row jcflexstart table-title_wraper">移库</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-2 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-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-2 zd-td fontcol1"><text>{{e.check_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.dtl_num}}</text></view>
						<view class="zd-col-2 zd-td"><text>{{e.status}}</text></view>
						<view class="zd-col-2 zd-td"><text>{{e.is_nok}}</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.check_type}}</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="_stIvtCheckdtlCheckFinish(e)">盘点完成</button>
								<button class="confirm-button" :class="{'confirm-button_disabled': !e.subOneChecked}" :disabled="disabled1" @tap="_stIvtCheckdtlReally(e)">实物为准</button>
								<button class="confirm-button" :class="{'confirm-button_disabled': !e.subOneChecked}" :disabled="disabled1" @tap="_stIvtCheckdtlBasedOnFinance(e)">财务为准</button>
							</view>
						</view>
						<view class="table-wrap table-wrap_1">
							<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>
										<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.struct_code}}</td>
										<td>{{el.struct_name}}</td>
										<td>{{el.material_code}}</td>
										<td>{{el.material_name}}</td>
										<td><input class="td_input" type="number" v-model="el.qty"  @blur="updateNumkw(e, el)" /></td>
										<td>{{el.fac_qty}}</td>
										<td>{{el.unit}}</td>
										<td>{{el.check_result}}</td>
										<td>{{el.status}}</td>
										<td>{{el.check_name}}</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.vue'
	import Pagination from '@/components/Pagination.vue'
	import GridDetail from '@/components/GridDetail.vue'
	import {getWarehouseInfo, stIvtCheckdtlCheckPage, checkDtlByCheckCode, stIvtCheckdtlUpdateDtl, stIvtCheckdtlCheckFinish, stIvtCheckdtlReally, stIvtCheckdtlBasedOnFinance} 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()
		},
		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)
					})
				}
			},
			// 查询一级表格
			toSearch () {
				this.queryParams1 = {
					pageSize: 10,
					pageNum: 1
				}
				this.currentPage1 = 1
				this.pkId = ''
				this._stIvtCheckdtlCheckPage()
			},
			// 一级表格接口
			async _stIvtCheckdtlCheckPage () {
				let res = await stIvtCheckdtlCheckPage(this.val1, this.value1, 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.dataList = [...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'
					})
				}
				this._getWarehouseInfo()
			},
			// 一级表格切换分页
			handlePageChange1(page) {
				this.queryParams1.pageNum = page
				this.currentPage1 = page
				this._stIvtCheckdtlCheckPage()
			},
			// 点击一级表格一行加载二级表格
			loadSubData (e) {
				if (e.subData.length > 0) {
					e.subData = []
				} else if (e.subData.length === 0) {
					this._checkDtlByCheckCode(e)
				}
				e.subAllChecked = false
				e.subOneChecked = false
			},
			// 二级表格接口
			async _checkDtlByCheckCode (e) {
				let res = await checkDtlByCheckCode(e.id)
				if (res.code === 1) {
					res.result.map(el => {
						this.$set(el, 'subChecked', false)
					})
					this.dataList.map(el => {
						if (el.djid === e.djid) {
							el.subData = [...res.result]
						}
					})
				} 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
				})
			},
			// 二级表格多选
			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
			},
			// 修改副表格单行移动数量
			updateNumkw (e, el) {
				this._stIvtCheckdtlUpdateDtl(e, el)
			},
			// 二级表格单行盘点数量接口
			async _stIvtCheckdtlUpdateDtl (e, el) {
				try {
					let res = await stIvtCheckdtlUpdateDtl(el)
					if (res.code === 1) {
						this._checkDtlByCheckCode(e)
					}
					uni.showToast({
						title: res.desc,
						icon: 'none'
					})
				} catch (e) {
					console.log(e)
				}
			},
			// 盘点完成按钮
			async _stIvtCheckdtlCheckFinish (e) {
				this.disabled1 = true
				if (!e.subOneChecked) {
					this.disabled1 = false
					return
				}
				let arr = e.subData.filter(ele => ele.checked === true)
				try {
					let res = await stIvtCheckdtlCheckFinish(arr)
					if (res.code === 1) {
						this._checkDtlByCheckCode(e)
					}
					this.disabled1 = false
					uni.showToast({
						title: res.desc,
						icon: 'none'
					})
				} catch (e) {
					this.disabled1 = false
				}
			},
			// 实物为准按钮
			async _stIvtCheckdtlReally (e) {
				this.disabled1 = true
				if (!e.subOneChecked) {
					this.disabled1 = false
					return
				}
				let arr = e.subData.filter(ele => ele.checked === true)
				try {
					let res = await stIvtCheckdtlReally(arr)
					if (res.code === 1) {
						this._checkDtlByCheckCode(e)
					}
					this.disabled1 = false
					uni.showToast({
						title: res.desc,
						icon: 'none'
					})
				} catch (e) {
					this.disabled1 = false
				}
			},
			// 财务为准
			async _stIvtCheckdtlBasedOnFinance (e) {
				this.disabled1 = true
				if (!e.subOneChecked) {
					this.disabled1 = false
					return
				}
				let arr = e.subData.filter(ele => ele.checked === true)
				try {
					let res = await stIvtCheckdtlBasedOnFinance(arr)
					if (res.code === 1) {
						this._checkDtlByCheckCode(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>
