<!-- 手动入库 -->
<template>
	<view class="container-wrap">
		<uni-nav-bar title="手动入库" :status-bar="true" background-color="#3F92F9" color="#FFF">
			<view slot="left">
				<u-icon name="account-fill" color="#FFF" size="35"></u-icon>
				<span style="padding: 0 5rpx;color: #FFFFFF;">{{ userInfo.name }}</span>
			</view>
			<view slot="right" @tap="$utils.back()">
				<span style="color: #FFFFFF;">返回</span>
			</view>
		</uni-nav-bar>
		<view class="container">
			<view class="title">
				<span>
					<span style="color: red;">*</span>
					入库信息
				</span>
			</view>
			<view class="text">
				<view class="label">领料人：</view>
				<select-lay :value="operateUser" placeholder="默认当前用户" :options="userList" @selectitem="selectitem"
					:zindex="2000"></select-lay>
			</view>
			<view class="text">
				<view class="label">仓库：</view>
				<select-lay :zindex="1000" :value="plcStationCode" :options="selectList" @selectitem="storeSelectitem">
				</select-lay>
			</view>
			<view class="text">
				<view class="label">入库类型：</view>
				<select-lay :value="stockChangeType" :options="typeList" @selectitem="typeSelectitem"></select-lay>
			</view>
			<view class="text" style="align-items: flex-start;">
				<view class="label">备注：</view>
				<u-input v-model="remark" type="textarea" :border="true" />
			</view>
			<view class="text" style="margin: 20rpx 0 30rpx 0;">
				<view class="label">领料时间：</view>
				<view style="line-height: 1;">{{ time }}</view>
			</view>
			<view class="title flex-between">
				<span>
					<span style="color: red;">*</span>
					入库列表
				</span>
				<a href="#" @click="scan">+扫描添加</a>
			</view>
			<view class="list">
				<view class="item" v-for="(item, index) in scanList" :key="index">
					<view class="row">
						<view class="col">
							<view class="value">{{ item.materialCode }}</view>
						</view>
						<view class="col">
							<view class="value">{{ item.materialName }}</view>
						</view>
					</view>
					<view class="row sub-row">
						<view class="col">
							<view class="label">规格：</view>
							<view class="value">{{ item.spec }}</view>
						</view>
						<view class="col">
							<view class="label">数量：</view>
							<view class="value">{{ item.quantity }}米</view>
						</view>
					</view>
				</view>
			</view>
			<my-fixed-button :customClick="true" @click="cfm" text="提交"></my-fixed-button>
		</view>
	</view>
</template>

<script>
	import DateFormat from '../../../js_sdk/xfl-DateFormat/DateFormat.js';
	export default {
		data() {
			return {
				timer: null,
				time: new DateFormat(new Date()).toString(),
				show: false,
				operateUser: this.$storage.getStorageSync('userInfo').id,
				plcStationCode: '1',
				stockChangeType: '10',
				remark: '',
				isReading: false,
				scanList: [],
				userList: [],
				selectList: [],
				typeList: [{
						label: '入库',
						value: 10
					},
					{
						label: '盘点新增',
						value: 11
					},
					{
						label: '补入库',
						value: 13
					},
				]
			};
		},
		methods: {
			/* 选择类型 */
			typeSelectitem(index, item) {
				this.stockChangeType = item.value;
			},
			/* 选择领料人 */
			selectitem(index, item) {
				this.operateUser = item.value;
			},
			/* 选择仓库 */
			storeSelectitem(index, item) {
				this.plcStationCode = item.value;
				uni.setStorage({
					key: 'plcStationCode',
					data: item.value
				})
			},
			cfm() {
				let obj = {
					userId: this.operateUser,
					plcCode: this.plcStationCode,
					rfidItems: this.scanList.map(item => item.rfidCode),
					stockChangeType: this.stockChangeType,
					remark: this.remark
				}
				console.log(obj)
				this.$http.HandleMacStockInOut(obj).then(res => {
					console.log(res)
					if (res.code === 0) {
						this.$msg.showToast(res.msg || '操作成功！')
						this.timer = setTimeout(() => {
							this.$utils.back()
						}, 2000)
					}
				})
			},
			/* 扫描添加 */
			scan() {
				this.$utils.open('/pages/store-in-manage/store-in/store-in-scan')
			},
			getList() {
				this.$http.GetApplyStockOutTemp().then(res => {
					console.log(res)
					if (res.code === 0) {
						this.scanList = res.result
					}
				})
			},
			/* 添加扫描到的退货数据 */
			pushData(data) {
				console.log(this.scanList)
				let flag = true
				this.scanList.forEach(item => {
					if (item.rfidCode === data.rfidCode) {
						flag = false
					}
				})
				if (flag) {
					this.scanList.push(data)
				} else {
					setTimeout(() => {
						this.$msg.showToast('标签已存在！')
					}, 100)
				}
			},
			/* 获取用户列表 */
			getUserPageList() {
				uni.request({
					url: 'http://120.79.80.64:8050' + '/cloudApi/uUser/list',
					method: 'POST',
					header: {
						'Content-Type': 'application/json',
					},
					data: {
						deptIdList: ['A012', 'A008', 'A007', 'A017', 'A005'],
					},
					success: res => {
						console.log(res)
						let list = res.data.data.map((item) => {
							return {
								label: item.realname,
								value: item.id,
								name: item.realname,
							}
						})
						this.userList = list
						
					},
				});
				// this.$http.GetUserPageList({
				// 	pageIndex: 1,
				// 	pageSize: 999,
				// 	takeUser: true
				// }).then(res => {
				// 	if (res.code == 0) {
				// 		let list = res.result.list.map(item => {
				// 			return {
				// 				label: item.realName,
				// 				value: item.id
				// 			}
				// 		})
				// 		this.userList = list
				// 	}
				// })
			},
			/* 手持机获取仓库 */
			getStockHouse() {
				this.$http.GetStockHouse().then(res => {
					if (res.code === 0) {
						this.selectList = res.result.map(item => {
							return {
								label: item.name,
								value: item.plcCode
							}
						})
					}
				})
			}
		},
		onLoad(option) {
			this.getUserPageList()
			this.getStockHouse()
			console.log(option)
			uni.$on('scan-list', res => {
				console.log('?????????????')
				console.log(res)
				this.pushData(res)
			})
			uni.getStorage({
				key: 'plcStationCode',
				success: (res) => {
					this.plcStationCode = res.data
				}
			});
		},
		onUnload() {
			clearTimeout(this.timer)
			uni.$off('scan-list')
		}
	}
</script>

<style lang="scss" scoped>
	.container-wrap {
		overflow: hidden;

		.container {
			padding: 20rpx;
			height: calc(100vh - var(--status-bar-height) - 44px);
			overflow: auto;
			padding-bottom: 100rpx;

			.text {
				padding: 10rpx 0;
				display: flex;
				align-items: center;
				font-size: 24rpx;

				.label {
					width: 150rpx;
					text-align: right;
				}
			}

			.list {
				margin: 10rpx 0;

				.item {
					padding: 20rpx;
					margin-bottom: 10rpx;
					background-color: #FFFFFF;
					border: 1rpx solid rgba(215, 215, 215, 1);
					border-radius: 10rpx;

					.row {
						&.border {
							border-bottom: 1px solid rgba(215, 215, 215, 1);
						}

						.label {
							width: 80rpx;
						}

						.col {
							&:last-child {
								justify-content: flex-end;
							}
						}
					}

					.sub-row {
						align-items: flex-start;

						.label,
						.value {
							font-size: 24rpx;
							color: #7F7F7F;
						}
					}
				}
			}
		}
	}
</style>
