<!-- 报损申请 -->
<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">
				<u-input v-model="applyAccount" type="textarea" :border="true" height="200"/>
			</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 class="col"></view>
					</view>
					<view class="row sub-row">
						<view class="col">
							<view class="label">规格：</view>
							<view class="value">{{ item.spec }}</view>
						</view>
					</view>
					<view class="row">
						<view class="col">
							<view class="label">合同批次：</view>
							<view class="value">{{ item.batchNo }}</view>
						</view>
					</view>
					<view class="row">
						<view class="col">
							<view class="label">数量：</view>
							<view class="value">{{ item.quantity }}</view>
						</view>
						<view class="col">
							<view class="label">金额：</view>
							<view class="value">{{ item.amount || 0  }}元</view>
						</view>
						<view class="col"></view>
					</view>
					<view class="row border">
						<view class="col">
							<view class="label">申请原因：</view>
							<view class="value">{{ item.applyReason }}</view>
						</view>
					</view>
					<view class="row" style="justify-content: center;">
						<span style="color: red;font-weight: bold;" @tap="del(index)">删除</span>
					</view>
				</view>
			</view>
			<my-fixed-button :customClick="true" @click="scan" text="添加明细" type="default" bottom="90rpx"></my-fixed-button>
			<my-fixed-button :customClick="true" @click="cfm" text="提交申请"></my-fixed-button>
		</view>
	</view>
</template>

<script>
	// #ifdef APP-PLUS
	// #endif
	export default {
		data() {
			return {
				applyAccount: '',
				isReading: false,
				scanList: []
			};
		},
		methods: {
			cfm(){
				this.$http.AddApplyStockOutBill({
					// applyAccount: this.applyAccount,
					stockOutType: 1,
					materialList: this.scanList
				}).then(res => {
					console.log(res)
					if(res.code === 0) {
						this.$msg.showToast(res.msg || '操作成功！')
						setTimeout(() => {
							this.$utils.back()
						}, 2000)
					}
				})
			},
			/* 扫描添加 */
			scan() {
				this.$utils.open('/pages/store-out-manage/breakage-apply/breakage-apply-scan')
			},
			pushData(data) {
				let flag = true
				let info = ''
				this.scanList.forEach(item => {
					if(item.rfidCode === data.rfidCode) {
						info = '标签已存在！'
						flag = false
					} else if(item.supplierName !== data.supplierName) {
						info = '供应商不相同！'
						flag = false
					} else if(item.materialCode !== data.materialCode) {
						info = '物料不相同！'
						flag = false
					}
				})
				if(flag) {
					this.scanList.push(data)
				} else {
					console.log(info)
					setTimeout(() => {
						this.$msg.showToast(info)
					}, 100)
				}
			}
		},
		onLoad(option) {
			console.log(option)
			uni.$on('scan-list', res => {
				console.log(res)
				this.pushData(res)
			})
		},
		onUnload() {
			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: hidden;
		display: flex;
		flex-direction: column;
		.text {
			margin: 10rpx 0;
			background-color: #FFFFFF;
		}
		.list {
			flex: 1;
			margin: 10rpx 0;
			overflow: auto;
			margin-bottom: 160rpx;
			.item {
				padding: 20rpx;
				margin-bottom: 10rpx;
				background-color: #FFFFFF;
				border: 1rpx solid rgba(215, 215, 215, 1);
				border-radius: 10rpx;
				.row {
					align-items: flex-start;
					&.border {
						padding-bottom: 20rpx;
						border-bottom: 1px solid rgba(215, 215, 215, 1);
					}
					.label {
						width: auto;
					}
					.col {
						align-items: flex-start;
					}
				}
				.sub-row {
					align-items: flex-start;
					.label, .value {
						font-size: 24rpx;
						color: #7F7F7F;
					}
				}
			}
		}
	}
}
</style>

