<template>
	<view class="container">
		<cu-custom bgColor="bg-gradual-pink" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">单据查询组盘</block>
		</cu-custom>
		<view class="example">
			<uni-forms ref="form" :model="formData" labelWidth="80px">
				<uni-forms-item label="业务类型" name="businessType">
					<uni-data-select v-model="typeData.businessType" :localdata="orderTypesList"
						@change="selectOrderTypeChange" placeholder="请选择业务类型">
					</uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="单据号:" name="invoiceCode">
					<view class="flex-twice  padding-0 radius">
						<uni-data-select v-model="formData.invoiceCode" :localdata="billList"
							@change="selectInvoiceCodeChange" placeholder="请选择入库单号">
						</uni-data-select>
					</view>
				</uni-forms-item>
				<uni-forms-item label="托盘编号" name="palletCode">
					<uni-easyinput v-model="formData.palletCode" placeholder="请输入托盘号" @blur="trimInput" />
				</uni-forms-item>
				<uni-forms-item label="仓库编号" name="houseCode">
					<picker @change="PickerHouseCodeChange" :value="index" :range="picker">
						<uni-easyinput v-model="formData.houseCode" placeholder="请选择仓库编号" />
						<view class="picker">
						</view>
					</picker>
				</uni-forms-item>
				<uni-forms-item label="托盘重量" name="ext2">
					<picker @change="PickerWeightChange" :value="index" :range="picker1">
						<uni-easyinput v-model="formData.ext2" placeholder="请选择托盘重量" />
						<view class="picker">
						</view>
					</picker>
				</uni-forms-item>
			</uni-forms>
			<!-- <button class="cu-btn "> -->
			<button type="primary" @click="formSubmit">提交</button>
			<!-- </button> -->
			<view v-if='billInfo.wmsInBillSkuInfos.length>0'>
				<view class="uni-container">
					<uni-table ref="table" :loading="loading" border stripe type="selection" emptyText="暂无更多数据"
						@selection-change="selectionChange">
						<uni-tr>
							<uni-th width="280" align="center">基本信息</uni-th>
							<uni-th width="100" align="center">设置</uni-th>
						</uni-tr>
						<uni-tr v-for="(item, index) in billInfo.wmsInBillSkuInfos" :key="index">
							<uni-td>
								<view class="flex">
									<view class="flex-sub padding-0 margin-xs radius">明细号</view>
									<view class="flex-twice padding-0 margin-xs radius">{{item.itemCode}}</view>
								</view>
								<view class="flex">
									<view class="flex-sub padding-0 margin-xs radius">物料信息</view>
									<view class="flex-twice padding-0 margin-xs radius">
										{{item.skuCode}}
										<view class='cu-tag line-blue '>
											{{item.skuName}}
										</view>
									 </view>
								</view>
								<view class="flex">
									<view class="flex-sub padding-0 margin-xs radius">批次号</view>
									<view class="flex-twice padding-0 margin-xs radius">{{item.batch}}</view>
								</view>
								<view class="flex">
									<view class="flex-sub padding-0 margin-xs radius">
										<view class='cu-tag bg-blue '>
											计划数量 {{item.planQty}}  {{item.baseUnit}}
										</view>
									</view>
									<view class="flex-twice padding-0 margin-xs radius">
										<view class="cu-tag line-blue">
											锁定数量 {{item.lockQty}}  {{item.baseUnit}}
										</view>
									</view>
								</view>
								<view class="flex">
									<view class="flex-sub padding-0 margin-xs radius">组盘数量</view>
									<view class="flex-twice padding-0 margin-xs radius">
										<input placeholder="请输入组盘数量" v-model="item.qty"></input>
									</view>
								</view>
							</uni-td>
						</uni-tr>
					</uni-table>
					<view class="uni-pagination-box"><uni-pagination show-icon :page-size="pageSize"
							:current="pageCurrent" :total="total" @change="change" /></view>
				</view>
			</view>
		</view>
	</view>

</template>

<script>
	import {
		queryInBillSkuInfo,
		createPalletByBill,
		getInvoicesByBusinessType,
		listStatus
	} from "@/api/wms/bill.js"
	import {
		getDictionary
	} from "@/api/wms/dictData.js"
	import {
		formatDate
	} from 'tough-cookie';
	export default {
		data() {
			return {
				//表单中的数据信息	
				formData: {
					invoiceCode: "",
					palletCode: "",
					houseCode: "密集库",
					ext2: "",//托盘重量
					details: []
				},
				typeData: {
					businessType: "",
				},
				rules: {
					invoiceCode: {
						rules: [{
							required: true,
							errorMessage: '单据号不能为空'
						}]
					},
					palletCode: {
						rules: [{
							required: true,
							errorMessage: '托盘号不能为空'
						}]
					},
					houseCode: {
						rules: [{
							required: true,
							errorMessage: '仓库号不能为空'
						}]
					},
					ext2: {
						rules: [{
							required: true,
							errorMessage: '托盘重量不能为空'
						}]
					},
					details: {
						rules: [{
							required: true,
							errorMessage: '物料信息不能为空'
						}]
					},
				},
				index:-1,
				//查询获取的单据信息
				billInfo: {
					totalPlanQty: 0,
					totalSurplusQty: 0,
					wmsInBillSkuInfos: []
				},
				//table 相关
				searchVal: '',
				tableData: [],
				// 每页数据量
				pageSize: 10,
				// 当前页
				pageCurrent: 1,
				// 数据总量
				total: 0,
				loading: false,
				//tabe选中的数据对象
				selectedIndexs: [],
				// 列配置
				tableClumns: [{
						dataIndex: "itemCode",
						key: "itemCode",
						title: "明细单号"
					},
					{
						dataIndex: "batch",
						key: "batch",
						title: "批次"
					},
					{
						dataIndex: "skuCode",
						key: "skuCode",
						title: "物料编号"
					},
					{
						dataIndex: "skuName",
						key: "skuName",
						title: "物料名称"
					},
					{
						dataIndex: "planQty",
						key: "planQty",
						title: "计划数量"
					},
					{
						dataIndex: "lockQty",
						key: "lockQty",
						title: "锁定数量"
					},
					{
						dataIndex: "qty",
						key: "qty",
						title: "组盘数量"
					},
				],
				
				picker: ['成品库', '原料库', '高架库', '密集库'],
				picker1: ['轻', '重'],
				orderTypesList: [],
				billList: [],

			};
		},
		onLoad() {
			//加载入库单据类型下拉值列表
			this.getInOrderTypes()
			//this.getInvoicesByBusinessType()

		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			//去除空格
			trimInput(event) {
				this.formData.palletCode.trim()
			},
			//选择仓库类型
			PickerHouseCodeChange(e) {
				console.log("PickerHouseCodeChange", e)
				console.log("PickerHouseCodeChange", e.detail.value)
				this.index = e.detail.value
				var picked = this.picker[e.detail.value]
				this.formData.houseCode = picked;
				console.log("picked", picked)
			},
			//选择托盘重量
			PickerWeightChange(e) {
				console.log("PickerWeightChange", e)
				console.log("PickerWeightChange", e.detail.value)
				this.index = e.detail.value
				var picked = this.picker1[e.detail.value]
				this.formData.ext2 = picked;
				console.log("picked", picked)
			},
			//获取入库单业务类型
			async getInOrderTypes() {
				this.orderTypesList = await getDictionary('inOrderTypes');
			},
			//获取单据编号
			async getInvoicesList(e) {
				this.billList = await getInvoicesByBusinessType(e);
			},
			//触发指定类型的单据列表的加载  更新 billList
			selectOrderTypeChange(inOrderBusinessType) {
				console.log("selectOrderTypeChange", inOrderBusinessType)
				this.getInvoicesList(inOrderBusinessType)
			},
			// 触发指定单据的当局明细加载 跟新 billInfo
			selectInvoiceCodeChange(invoiceCode) {
				console.log("selectInvoiceCodeChange", invoiceCode)
				this.getInvoiceDetailByCode(invoiceCode)
			},
			//获取指定单好对应的当局信息
			getInvoiceDetailByCode: async function(invoiceCode) {
				console.log('this.formData.invoiceCode', this.formData.invoiceCode);
				var invoiceCode = this.formData.invoiceCode;
				if (!invoiceCode || invoiceCode.length < 1) {
					this.$modal.msgError("单据号不能为空")
					return;
				}
				//根据单据号查询入库单明细
				var billInfo = await queryInBillSkuInfo({
					'invoiceCode': invoiceCode
				});
				console.log("billInfo", billInfo)
				if (billInfo) {
					this.billInfo = billInfo.data;
				};
			},
			/// 数据来源 [模拟]
			async fetch(param) {
				var response = await queryInBillSkuInfo(param);
				return {
					total: response.data.total,
					data: response.data.record,
				};
			},
			// 多选处理
			selectedItems() {
				return this.selectedIndexs.map(i => this.tableData[i])
			},
			// 多选
			selectionChange(e) {
				console.log("selectionChange",e)
				this.selectedIndexs = e.detail.index
				//选中内容改变时获取选中数据
			},
			//批量删除
			delTable() {
				console.log(this.selectedItems())
			},
			// 分页触发
			change(e) {
				this.$refs.table.clearSelection()
				this.selectedIndexs.length = 0
				this.getData(e.current)
			},
			// 搜索
			search() {
				this.getData(1, this.searchVal)
			},
			// 获取数据
			getData(pageCurrent, value = '') {
				this.loading = true
				this.pageCurrent = pageCurrent
				this.request({
					pageSize: this.pageSize,
					pageCurrent: pageCurrent,
					value: value,
					success: res => {
						// console.log('data', res);
						this.tableData = res.data
						this.total = res.total
						this.loading = false
					}
				})
			},
			//获取选中的数据
			getSelectDetail() {
				if (this.selectedIndexs.length == 0) {
					//提示 
					this.$modal.msgError("请先选择需要组盘的单据明细")
					return;
				}
				//筛选出需要拣选的单据明细
				let arr = [];
				let len = this.billInfo.wmsInBillSkuInfos.length;
				for (let i = 0; i < len; i++) {
					if (this.selectedIndexs.indexOf(i) >= 0) {
						arr.push(this.billInfo.wmsInBillSkuInfos[i]);
					} 
				}
				console.log("选中的单据明细数据为", arr.length)
				return arr;
			},

			// 伪request请求
			request(options) {
				const {
					pageSize,
					pageCurrent,
					success,
					value
				} = options
				let total = tableData.length
				let data = tableData.filter((item, index) => {
					const idx = index - (pageCurrent - 1) * pageSize
					return idx < pageSize && idx >= 0
				})
				if (value) {
					data = []
					tableData.forEach(item => {
						if (item.name.indexOf(value) !== -1) {
							data.push(item)
						}
					})
					total = data.length
				}

				setTimeout(() => {
					typeof success === 'function' &&
						success({
							data: data,
							total: total
						})
				}, 500)
			},

			//表单提交
			formSubmit: function(ref) {
				//拿到 formaData
				var details = this.getSelectDetail()
				if (details && details.length > 0) {
					this.$refs.form.validate().then(res => {
						console.log("数据校验通过", res)
						this.formData.details = details
						if(this.formData.invoiceCode.length>0){
								/* //刷新指定的单据明细
								this.getInvoiceDetailByCode(this.formData.invoiceCode);
								//清空 TABLE的默认选中效果
								this.$refs.table.clearSelection()
								console.log("this.selectedIndexs",this.selectedIndexs) */
							} 
						createPalletByBill(this.formData).then(response => {
							console.log("response", response)
							let {code,msg,data} = response
							this.$modal.msgSuccess("组盘成功")
							if(this.formData.invoiceCode.length>0){
								//刷新指定的单据明细
								this.getInvoiceDetailByCode(this.formData.invoiceCode);
								//清空 TABLE的默认选中效果
								this.$refs.table.clearSelection()
								this.formData.palletCode=""
							} 
						})
					})
				}
			},

			hideMsgModal(e) {
				//关闭信息模态框
			},
			textareaAInput(e) {
				this.textareaAValue = e.detail.value
			},
			textareaBInput(e) {
				this.textareaBValue = e.detail.value
			}

		}
	}
</script>

<style lang="scss">
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}

	page {
		background-color: #ffffff;
	}

	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
		flex: 1;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		line-height: 35px;
		margin-left: 10px;
	}
</style>