<template>
	<view>
		<uni-forms ref="form" :modelValue="formData">
			<uni-forms-item label="合并标签码" :labelWidth='90' name="singleReportSign">
				<uni-easyinput suffixIcon="scan" @iconClick="scanBar" @confirm="scanBarCode"
					v-model="formData.singleReportSign" type="text" />
			</uni-forms-item>
			<uni-swipe-action style="height: 88vh;overflow: scroll;">
				<uni-swipe-action-item :rightOptions="rightOptions" :key="index"
					@click="(data) => clickDetail(index,data)" v-for="(item, index) in reportList">
					<uni-card :is-shadow="false" is-full>
						<div><strong>物料编码</strong>:{{item.ptNoTar}}</div>
						<div><strong>物料名称</strong>:{{item.ptTitleTar}}</div>
						<div><strong>物料规格</strong>:{{item.specification1}}</div>
						<div><strong>数量</strong>:{{item.reportNumber}}</div>
						<div><strong>仓库</strong>:{{item.whName?item.whName:item.whCode}}</div>
						<div><strong>合并标签码</strong>:{{item.singleReportSign}}</div>
					</uni-card>
				</uni-swipe-action-item>
			</uni-swipe-action>
		</uni-forms>
		<u-button type="primary" @click="submit">提交</u-button>
	</view>
</template>

<script>
	import {
		listMesJobSingleReportSign,
		advListMesJobSingleReportSign,
		getMesJobSingleReportSign,
		delMesJobSingleReportSign,
		addMesJobSingleReportSign,
		updateMesJobSingleReportSign,
		reportConfirm,
		qualityConfirm,
		whInConfirm
	} from "@/api/mes/mesJobSingleReportSign";
	import {
		listWarehouse
	} from "@/api/wms/pdcIn.js";
	export default {
		mounted() {
			listWarehouse().then(res => {
				this.whList = res.rows
			})
		},
		data() {
			return {
				whList: [],
				reportList: [],
				formData: {
					singleReportSign: null
				},
				rightOptions: [{
					text: '删除',
					style: {
						backgroundColor: '#ff2a17'
					}
				}, ],
			}
		},
		methods: {
			deleteDetail(index) {
				this.reportList.splice(index, 1);
			},
			clickDetail(itemIndex, {
				position,
				index
			}) {
				if (index == 0) {
					this.deleteDetail(itemIndex);
				}
			},
			submit() {
				let ids = this.reportList.map(item => item.id)
				reportConfirm(ids)
				this.$modal.msg("报工确认已提交！");
				this.reportCode = null;
				this.reportList = [];
			},
			scanBarCode() {
				listMesJobSingleReportSign(this.formData).then(res => {
					if (res.rows.length === 0) {
						this.$modal.msg("未查询到相关记录！");
						return;
					}
					const row = res.rows[0];
					if (row.status !== 0) {
						this.$modal.msg("该记录当前不在'未上传报工'状态！");
					} else {
						if (this.reportList.some(item => item.id == row.id)) {
							this.$modal.msg("数据已存在，无需重复添加。");
							return;
						}

						let obj = this.whList.find(item => item.warehouseCode == row.whCode)
						if (obj) {
							row.whName = obj.warehouseName
						}
						this.reportList.push(row);
					}
					this.formData.singleReportSign = null;
				})
			},
			scanBar() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.singleReportSign = res.result;
						_this.scanBarCode();
					}
				});
			},
		}
	}
</script>

<style>
	// 搜索框
	.search-bar {
		width: 100%;
		height: 100rpx;
		margin-top: 2%;
	}

	.search-bar-box {
		display: flex;
		margin: 0 auto;
		width: 620rpx;
		height: 74rpx;
		border: 5rpx solid #00a8cc;
		border-radius: 50rpx;
	}

	.search-text {
		width: 100%;
		margin-top: 10rpx;
		margin-left: 20rpx;
		font-size: 30rpx;
		color: #7f7f81;
	}

	.search-btn {
		background-color: #00a8cc;
		/* Green */
		color: white;
		text-align: center;
		display: inline-block;
		font-size: 35rpx;
		width: 240rpx;
		height: 70rpx;
		line-height: 65rpx;
		border-radius: 30rpx;
		letter-spacing: 3rpx;
	}
</style>