<template>
	<page-meta root-font-size="16px"></page-meta>
	<uni-popup ref="popupResult" type="center">
		<view class="uni-popup-view">
			<view class="uni-list">
				<view class="uni-list-cell">
					<view class="uni-list-cell-left">
						<view class="uni-label">ERP料号</view>
					</view>
					<view class="uni-list-cell-db">
						<view class="uni-flex uni-row space-between">
							<input class="uni-input" type="text" :disabled="true" v-model="itemDetail.itemCode" />
							<view class="state-style type-style" :class="itemDetail.inspectType | inspectTypeStyle">
								{{itemDetail.inspectType | inspectTypeDesc}}
							</view>
						</view>
					</view>
				</view>

				<view class="uni-list-cell">
					<view class="uni-list-cell-left">
						<view class="uni-label">数量</view>
					</view>
					<view class="uni-list-cell-db">
						<view class="uni-flex space-between">
							<input class="qty-input" type="text" :disabled="true" v-model="qty" />
							<input class="qty-input" type="text" :disabled="true" v-model="uom"
								style="width: 30%; padding:15rpx 5rpx" />
						</view>
					</view>
				</view>

				<!--<view class="uni-list-cell">
					<view class="uni-list-cell-left">
						<view class="uni-label">单位</view>
					</view>
					<view class="uni-list-cell-db">
						<input class="uni-input" type="text" :disabled="true" v-model="uom" />
					</view>
				</view> -->

				<view class="uni-list-cell">
					<view class="uni-list-cell-left">
						<view class="uni-label">检验数量</view>
					</view>
					<view class="uni-list-cell-db">
						<input class="uni-input" type="text" :disabled="true" v-model="itemDetail.inspectQty" />
					</view>
				</view>
				<view class="uni-list-cell">
					<view class="uni-list-cell-left">
						<view class="uni-label">不合格数量</view>
					</view>
					<view class="uni-list-cell-db">
						<uni-easyinput :inputBorder="false" class="pda-easyinput" type="number" placeholder="请输入数字"
							v-model="itemDetail.failedQty" @confirm="failedQtyConfirm" @blur="failedQtyConfirm" />
					</view>
				</view>
				<view class="uni-list-cell">
					<view class="uni-list-cell-left">
						<view class="uni-label">不合格原因</view>
					</view>
					<view class="uni-list-cell-db">
						<uni-data-picker class='uni-data-picker' :border="false" placeholder="请选择原因" popup-title="不合格原因"
							:localdata="failedReasonArray" v-model="itemDetail.failedReason">
						</uni-data-picker>
						<!-- 	<picker @change="failedReasonChange" :value="failedReasonIndex" :range="failedReasonArray"
							range-key="name" placeholder="请选择原因">
							<view class="uni-input">{{failedReasonArray[failedReasonIndex].name}}</view>
						</picker> -->
					</view>
				</view>
				<view class="uni-list-cell">
					<view class="uni-list-cell-left">
						<view class="uni-label">报废数量</view>
					</view>
					<view class="uni-list-cell-db">
						<uni-easyinput :inputBorder="false" placeholder="请输入数字" type="number"
							v-model="itemDetail.crackQty">
						</uni-easyinput>
					</view>
				</view>
				<view class="uni-list-cell">
					<view class="uni-list-cell-left">
						<view class="uni-label">下一步动作</view>
					</view>
					<view class="uni-list-cell-db">
						<uni-data-picker class='uni-data-picker' :border="false" placeholder="请选择下一步动作"
							popup-title="下一步动作" @change="nextStepChange" :localdata="nextStepArray" v-model="nextStep">
						</uni-data-picker>

						<!-- <picker :value="nextStepIndex" :range="nextStepArray" range-key="name" placeholder="请选择下一步动作"
							@change="nextStepChange">
							<view class="uni-input">{{nextStepArray[nextStepIndex].name}}</view>
						</picker> -->
					</view>
				</view>
				<view class="uni-list-cell">
					<view class="uni-list-cell-left">
						<view class="uni-label">最终不合格</view>
					</view>
					<view class="uni-list-cell-db">
						<uni-easyinput :inputBorder="false" placeholder="待计算" type="number" :disabled="true"
							v-model="itemDetail.notPassedQty">
						</uni-easyinput>
					</view>
				</view>
			</view>
			<view class="uni-popup-button-box">
				<button class="uni-popup-button" type="primary" @click="submit">确定</button>
			</view>
		</view>
	</uni-popup>
</template>

<script>
	import {
		getInspectTypeStyle,
		getInspectTypeDesc
	} from '@/common/basic.js';
	export default {
		data() {
			return {
				itemDetail: {},
				failedReasonIndex: 0,
				failedReasonArray: [{
					text: '外观不良',
					value: '外观不良'
				}, {
					text: '尺寸超差',
					value: '尺寸超差'
				}, {
					text: '功能失效',
					value: '功能失效'
				}, {
					text: '标签不符',
					value: '标签不符'
				}, {
					text: '检验报告异常',
					value: '检验报告异常'
				}, {
					text: '其他',
					value: '其他'
				}],
				nextStep: '',
				nextStepIndex: 0,
				nextStepArray: [{
					text: '部分不合格',
					value: '部分不合格'
				}, {
					text: '全部不合格',
					value: '全部不合格'
				}, {
					text: '挑选',
					value: '挑选'
				}],
				inspectType: '', //免检 全检 抽检 挑选
				failedQty: 0,
				qty: 0,
				uom: ''
			}
		},
		props: {
			itemCode: "",

		},
		filters: {
			inspectTypeStyle: function(val) {
				return getInspectTypeStyle(val);
			},
			inspectTypeDesc: function(val) {
				return getInspectTypeDesc(val);
			}
		},
		created() {

		},
		methods: {
			openPopup(itemdetail) {
				this.itemDetail = itemdetail;
				this.qty = itemdetail.receiveQty.qty;
				this.uom = itemdetail.receiveQty.uom;
				this.inspectType = getInspectTypeDesc(this.itemDetail.inspectType);
				this.$refs['popupResult'].open("center");
			},
			//不合格原因
			failedReasonChange: function(e) {
				// console.log('picker发送选择改变，携带值为：' + e.detail.value)
				// this.failedReasonIndex = e.detail.value
				// this.itemDetail.failedReason = this.failedReasonArray[this.failedReasonIndex].name;
			},
			//不合格数量确认事件
			failedQtyConfirm() {
				if (this.inspectType === '全检') //全检
				{
					this.itemDetail.notPassedQty = this.itemDetail.failedQty;
				}
			},
			//清空不合格数量
			failedQtyClear(value) {

			},
			//切换下一步动作
			nextStepChange: function(e) {
				// this.nextStepIndex = e.detail.value
				//如果是抽检有下一步动作
				if (this.inspectType === '抽检') //全检
				{
					// console.log('picker发送选择改变，携带值为：' + e.detail.value)
					let isSucce = true;
					let qty = this.itemDetail.receiveQty.qty //收货数量
					let crackQty = this.itemDetail.crackQty; //报废数量
					let failedQty = this.itemDetail.failedQty; //不合格数量

					//不合格数量
					if (failedQty < 0) {
						uni.showToast({
							title: '不合格数量不能小于0',
							icon: 'error'
						});
						isSucce = false;
					} else if (failedQty > 0) {
						if (failedQty > qty) {
							uni.showToast({
								title: '不合格数量' + failedQty + '不能大于收货数量' + qty,
								icon: 'error'
							});
							isSucce = false;
						}
					}

					//报废数量
					if (crackQty < 0) {
						uni.showToast({
							title: '报废数量不能小于0',
							icon: 'error'
						});
						isSucce = false;
					} else {
						if (crackQty > qty) {
							uni.showToast({
								title: '报废数量' + crackQty + '不能大于收货数量' + qty,
								icon: 'error'
							});
							isSucce = false;
						}
					}

					if (isSucce) {
						this.calcSampleFiledCount();
						// this.$emit("inspectResult", this.itemDetail);
						// this.$refs['popupResult'].close();
					}
				}

			},

			//计算最终不合格数量
			// 如果是抽检：用户填了不合格数量，如果下一步动作是挑选，最终不合格数量就是填写的不合格数量
			// 如果下一步动作是全部不合格，最终不合格数量等于 到货数量-报废数量
			// 如果下一步动作是部分不合格，最终不合格数量等于 用户填写的不合格数量

			//计算抽检的不合格数量
			calcSampleFiledCount() {
				//let nextStep = this.nextStepArray[this.nextStepIndex].name;
				// 如果下一步动作是挑选，最终不合格数量就是填写的不合格数量
				//如果下一步动作是全部不合格，最终不合格数量等于 到货数量-报废数量
				//如果下一步动作是部分不合格，最终不合格数量等于 用户填写的不合格数量
				if (this.nextStep === "挑选" || this.nextStep === "部分不合格") {
					this.itemDetail.notPassedQty = this.itemDetail.failedQty;
				} else if (this.nextStep === "全部不合格") {
					this.itemDetail.notPassedQty = this.itemDetail.receiveQty.qty - this.itemDetail.crackQty;
				}
			},
			//确定
			submit() {

				if (this.itemDetail.failedQty === '') {
					uni.showToast({
						title: '不合格数量不能为空',
						icon: 'error',
						duration: 2000
					})
				} else if (this.itemDetail.crackQty === '') {
					uni.showToast({
						title: '报废数量不能为空',
						icon: 'error',
						duration: 2000
					})
				} else if (this.itemDetail.failedReason === '') {
					uni.showToast({
						title: '不合格原因不能为空',
						icon: 'error',
						duration: 2000
					})
				} else {
					this.$emit("inspectResult", this.itemDetail);
					this.$refs['popupResult'].close();
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.qty-input {
		height: 50rpx;
		padding: 15rpx 25rpx;
		line-height: 50rpx;
		font-size: 28rpx;
		background: #FFF;
		// flex: 1;
	}

	/deep/ .input-value {
		font-size: 16px;
	}
</style>
