<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="palletCode">
					<view class="flex-twice  padding-0 radius">
						<uni-easyinput v-model="formData.palletCode" placeholder="请输入托盘编号" @blur="queryRkOrder" />
					</view>
				</uni-forms-item>
			</uni-forms>
			<view  v-if="palletInfo.pallet.id" class="line-blue">
				<view class="margin flex " >
					当前位置：
					<view class="bg-white flex-sub margin-right radius shadow-lg">
						{{palletInfo.pallet.locCode}}
					</view>
					当前区域：
					<view class="bg-white flex-sub radius shadow-lg">
						{{palletInfo.pallet.palletArea}}
					</view>
				</view>
				<view class="margin flex">
					任务标记：
					<view class="bg-white flex-sub margin-right radius shadow-lg">
						{{palletInfo.pallet.jobFlag}}
					</view>
					托盘类型：
					<view class="bg-white flex-sub radius shadow-lg">
						{{palletInfo.pallet.palletType}}
					</view>
				</view>
				<view class="margin flex">
					托盘大小：
					<view class="bg-white flex-sub margin-right radius shadow-lg">
						{{palletInfo.pallet.sizeType}}
					</view>
					库存状态：
					<view class="bg-white flex-sub radius shadow-lg">
						{{palletInfo.pallet.status}}
					</view>
				</view>
			</view>
			
			<button type="primary" @click="formSubmit">提交</button>
			<view v-if='palletInfo.details.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 palletInfo.details" :key="index">
							<uni-td>
								<view class="flex">
									<view class="flex-sub padding-0 margin-xs radius">托盘主建ID</view>
									<view class="flex-twice padding-0 margin-xs radius">{{item.palletId}}</view>
								</view>
								<view class="flex">
									<view class="flex-sub padding-0 margin-xs radius">托盘明细ID</view>
									<view class="flex-twice padding-0 margin-xs radius">{{item.id}}</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.palletCode}}</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>
									<view class="flex-twice padding-0 margin-xs radius">{{item.skuQty}}{{item.baseUnit}}</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.unboundQty"></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 class="cu-modal" :class="msgModalName=='Modal'?'show':''">
			<view class="cu-dialog">
				<view class="cu-bar bg-white justify-end">
					<view class="content">{{msgTitleName}}</view>
					<view class="action" @tap="hideMsgModal">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<view class="padding-xl">
					{{msg}}
				</view>
			</view>
		</view>
	</view>

</template>

<script>
	import {
		queryPalletInfo,
		palletDetailUnBind
	} from "@/api/wms/palletDetailUnBind.js"
	import {
		formatDate
	} from 'tough-cookie';
	export default {
		data() {
			return {
				//表单中的数据信息	
				formData: {
					palletCode: "",
				},
				rules: {
					palletCode: {
						rules: [{
							required: true,
							errorMessage: '托盘号不能为空'
						}]
					},
					details: {
						rules: [{
							required: true,
							errorMessage: '托盘信息不能为空'
						}]
					},
				},
				//查询获取的托盘信息
				palletInfo: {
					pallet: {},
					details: []
				},
				//table 相关
				searchVal: '',
				tableData: [],
				// 每页数据量
				pageSize: 10,
				// 当前页
				pageCurrent: 1,
				// 数据总量
				total: 0,
				loading: false,
				//tabe选中的数据对象
				selectedIndexs: [],
				// 列配置
				tableClumns: [{
						dataIndex: "palletId",
						key: "palletId",
						title: "托盘主编号"
					},
					{
						dataIndex: "id",
						key: "id",
						title: "托盘明细编号"
					},
					{
						dataIndex: "palletCode",
						key: "palletCode",
						title: "托盘编号"
					},
					{
						dataIndex: "skuCode",
						key: "skuCode",
						title: "物料编号"
					},
					{
						dataIndex: "skuName",
						key: "skuName",
						title: "物料描述"
					},
					{
						dataIndex: "locCode",
						key: "locCode",
						title: "托盘位置"
					},
					{
						dataIndex: "batch",
						key: "batch",
						title: "批次"
					},
					{
						dataIndex: "skuQty",
						key: "skuQty",
						title: "物料数量"
					},
					{
						dataIndex: "unboundQty",
						key: "unboundQty",
						title: "解绑数量"
					},
				],


				skuArray: [],
				skushow: false,
				palletCode: null,
				msgModalName: null,
				msgTitleName: null,
				msg: null
			};
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			/// 数据来源 [模拟]
			async fetch(param) {
				var response = await queryPalletInfo(param);
				return {
					total: response.data.total,
					data: response.data.record,
				};
			},
			// 多选处理
			selectedItems() {
				return this.selectedIndexs.map(i => this.tableData[i])
			},
			// 多选
			selectionChange(e) {
				console.log(e.detail.index)
				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 => {
						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.palletInfo.details.length;
				for (let i = 0; i < len; i++) {
					if (this.selectedIndexs.indexOf(i) >= 0) {
						var detail = this.palletInfo.details[i];
						arr.push({
							'palletCode': detail.palletCode,
							'id': detail.id,
							'unboundQty': detail.unboundQty
						});
					} else {
						// console.log(this.selectArr.indexOf(i))
						//this.selectArr.indexOf(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)
			},
			// submit(ref) {
			// 	this.$refs.form.validate().then(res => {
			// 		updateUserProfile(this.user).then(response => {
			// 			this.$modal.msgSuccess("修改成功")
			// 		})
			// 	})
			// }
			//表单提交
			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
						palletDetailUnBind(this.formData).then(response => {
							console.log("response", response)
							this.$modal.msgSuccess("解绑成功")
						})
					})
				}
			},

			hideMsgModal(e) {
				//关闭信息模态框
				this.msgModalName = null;
			},
			textareaAInput(e) {
				this.textareaAValue = e.detail.value
			},
			textareaBInput(e) {
				this.textareaBValue = e.detail.value
			},
			//根据托盘号查询托盘信息
			queryRkOrder: async function(e) {
				console.log('进入查询方法', e);
				console.log('this.formData', this.formData);
				console.log('this.formData.palletCode', this.formData.palletCode);
				var palletCode = this.formData.palletCode;
				if (palletCode == null) {
					this.$modal.msgError("托盘编号不能为空")
					return;
				}
				if (palletCode.length < 1) {
					this.$modal.msgError("托盘编号不能为空")
					return;
				}
				//查询托盘信息
				var resPalletInfo = await queryPalletInfo({
					'palletCode': palletCode
				});
				console.log("resPalletInfo", resPalletInfo)
				if (resPalletInfo) {
					this.palletInfo = resPalletInfo.data;
					this.palletInfo.pallet = resPalletInfo.data.pallet
					this.palletInfo.details = resPalletInfo.data.details
					// console.log("this.palletInfo", this.palletInfo)
					// console.log("palletInfo.data", palletInfo.data)
				} else {
					this.palletInfo.pallet = {}
					this.palletInfo.details = []
				};
			}
		}
	}
</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>