<template>
	<!-- 扫码报工 -->
	<view>
		<uni-card v-if="!formData.workorder">
			<!-- <text>{{ '线别:' + lineCode }}</text> -->
			<text>{{ '组别' + groupCode }}</text>
			<ScanInput @scanConfirm="scanConfirm" placeholder="请扫报工码开始报工"></ScanInput>
		</uni-card>
		<!-- 		<uni-card v-if="formData.workorder && formData.status === 2">
			<text>{{ '线别:' + lineCode }}</text>
			<text style="margin-left: 20px">{{ '组别' + groupCode }}</text>
			<ScanInput @scanConfirm="scanConfirm2" placeholder="请扫报工码结束报工"></ScanInput>
		</uni-card> -->
		<uni-card v-if="formData.workorder">
			<uni-forms :modelValue="formData" label-width="80px" label-align="right">
				<uni-forms-item label="工单号" name="workorder">
					<span>{{ formData.workorder }}</span>
				</uni-forms-item>
				<uni-forms-item label="产品编号" name="productionCode">
					<span>{{ formData.productionCode }}</span>
				</uni-forms-item>
				<uni-forms-item label="产品名称" name="productionName">
					<span>{{ formData.productionName }}</span>
				</uni-forms-item>
				<uni-forms-item label="规格" name="specification">
					<span>{{ formData.specification }}</span>
				</uni-forms-item>
				<uni-forms-item label="工单状态" name="status">
					<span style="color: grey" v-if="formData.status === 1">未开始</span>
					<span style="color: blue" v-if="formData.status === 2">进行中</span>
					<span style="color: green" v-if="formData.status === 3">已完成</span>
				</uni-forms-item>
				<uni-forms-item label="计划生产" name="deliveryNum">
					<span>{{ formData.deliveryNum }}</span>
				</uni-forms-item>
				<uni-forms-item v-if="formData.status !==1" label="报工数" name="finishedNum">
					<uni-number-box class="input-item" :width="200" v-model="finishedNum" placeholder="请输入报工数" :max="99999" ></uni-number-box>
				</uni-forms-item>
				<uni-forms-item v-if="formData.status !==1" label="">
					<span class="row-box">
						<button @click="addFinishedNum">增加</button>
						<button @click="reduceFinishedNum">减少</button>
						<!-- <button @click="doStartWorkOrder">暂存</button> -->
					</span>
				</uni-forms-item>
			</uni-forms>
		</uni-card>
		<view class="bottom-box" v-if="formData.workorder">
			<button v-if="formData.status === 1" :disabled="loading" type="primary" @click="doStartWorkOrder">开始工单</button>
			<button v-if="formData.status === 2 || formData.status === 3" :disabled="loading" type="warn" @click="doFinfishWorkOrder2">报工并完成工单</button>
		</view>
		<!-- 报工数自动修改 -->
		<view>
			<!-- 输入框示例 -->
			<uni-popup ref="inputDialog" type="dialog">
				<uni-popup-dialog
					ref="inputClose"
					mode="input"
					:title="numModel === 1?'增加报工数':'减少报工数'"
					:value="0"
					placeholder=""
					
					@confirm="dialogInputConfirm"
				></uni-popup-dialog>
			</uni-popup>
		</view>
		<!-- 初次进入校验 -->
		<view>
			<uni-popup ref="inputDialog2" type="dialog">
				<uni-popup-dialog
					ref="inputClose2"
					title="操作提醒"
					content="工单已完成报工,是否继续报工!"
					type="warn"
					cancelText="关闭" confirmText="继续"
					@confirm="dialogConfirm2"
					@close="dialogClose2"
				></uni-popup-dialog>
			</uni-popup>
		</view>
		<!-- 异常消息3 -->
		<view>
			<uni-popup ref="inputDialog3" type="dialog">
				<uni-popup-dialog
					ref="inputClose3"
					title="扫码异常"
					content="报工号异常,该工单不属于此组"
					type="error"
					cancelText="关闭" 
					@confirm="dialogConfirm3"
					@close="dialogClose3"
				></uni-popup-dialog>
			</uni-popup>
		</view>
	</view>
</template>

<script>
import { errorProofingAndReportingWork,updateProReportwork,noErrorProofingAndReportingReport } from '@/api/workorder/proreportwork.js';
import { GetWorkOrderDetail, startWorkOrder, finishWorkOrder } from '@/api/workorder/proworkorder.js';
import { getWorkOrderDetail,doStartWorkOrder,updateReport,doFinishWorkOrder } from '@/api/workorder/index.js';
export default {
	onLoad: function (option) {
		this.lineCode = option.lineCode;
		this.groupCode = option.groupCode;
	},
	data() {
		return {
			lineCode: '',
			groupCode: '',
			loading: false,
			formData: {
				id: null,
				finishNum: 0
			},
			finishedNum:0,
			// 按钮模式 1-加 2-减去
			numModel:1,
			inputDialog:null,
			inputClose:null,
			dialog2Check:false,
			inputDialog2:null,
			inputClose2:null,
		};
	},
	methods: {
		// finishedNumInputChange(e){
		// 	this.formData.finishedNum = e.detail.value;
		// },
		// 第一次扫工单码结果
		async scanConfirm(val) {
			this.loading = true;
			if (val === '' || val === null) {
				this.loading = false;
				return;
			}
			let strArray = val.split('^');
			const params = {
				workorder: strArray[0]
			};
			let res1 = await getWorkOrderDetail(params);
			if (res1 === null || res1.code !== 200) {
				uni.showToast({
					icon: 'error',
					title: '工单标签异常'
				});
				this.loading = false;
				return;
			}
			if (res1.data.groupCode !== this.groupCode) {
				this.$refs.inputDialog3.open()
				this.loading = false;
				return;
			}
			if(res1.data.status === 3){
				this.$refs.inputDialog2.open();
			}
			this.formData = res1.data;
			this.finishedNum = res1.data.finishNum ?? 0;
			this.loading = false;
		},
		// 更新工单信息
		updateWorkOrderDetail(params) {
			GetWorkOrderDetail(params).then((res) => {
				if (res.code === 200) {
					this.formData = res.data;
					
					// this.formData.finishedNum = res.data.nu,
				}
			});
		},
		// 开始工单
		doStartWorkOrder() {
			this.loading = true;
			const params = {
				workorder: this.formData.workorder
			};
			doStartWorkOrder(params).then((res) => {
				if (res.code === 200) {
					uni.showToast({
						icon: 'success',
						title: '工单开始成功'
					});
					this.updateWorkOrderDetail(params);
					this.loading = false;
				} else {
					this.loading = false;
				}
			});
		},

		// 报工数增加指定数
		addFinishedNum() {
			this.numModel = 1
			this.$refs.inputDialog.open()
		},
		// 报工数减少指定数
		reduceFinishedNum() {
			this.numModel = 2
			this.$refs.inputDialog.open()
		},
		// 提交1
		dialogInputConfirm(val){
			let oldNum = this.finishedNum ?? 0
			if(this.numModel === 1){
				this.finishedNum = (oldNum * 1) + (val * 1)
			}else{
				this.finishedNum = (oldNum * 1) - (val * 1)
				if(this.finishedNum < 0){
					this.finishedNum = 0
				}
			}
		},
		// 提示2
		dialogConfirm2(){
		},
		dialogClose2(){
			uni.navigateBack(-1)
		},
		// NODO 打开扫码报工
		openDialog2() {
			this.$refs.inputDialog2.open()
		},
		// NODO 提交2
		dialogInputConfirm2(val){
			const that = this
			if (val === '' || val === null) {
				uni.showToast({
					icon: 'error',
					title: '报工号异常'
				});
				return;
			}
			let strArray = val.split('^');
			if(strArray[0] != this.formData.workorder){
				uni.showToast({
					icon: 'error',
					title: '报工号异常'
				});
				this.loading = false;
				return;
			}
			this.loading = true;
			uni.showLoading({
				title:'报工中……'
			})
			// 不防错报工
			const params = {
				workorder:this.formData.workorder,
				reportNum:this.finishedNum
			}
			
			updateReport(params).then(res=>{
				if(res.code === 200){
					uni.showToast({
						icon: 'success',
						title: '报工成功',
						duration:2000,
						success() {
							const params2 = {
								workorder: that.formData.workorder
							};
							doFinishWorkOrder(params2).then((res) => {
								if (res.code === 200) {
									uni.showToast({
										icon: 'success',
										title: '完成工单'
									});
									that.loading = false;
									uni.hideLoading();
									setTimeout(()=>{
										uni.navigateBack(-1)
									},2000)
								} 
							});
						}
					});
				}
			}).finally(()=>{
				this.loading = true;
				uni.hideLoading();
			})
		},
		// NODO 完成工单 
		doFinfishWorkOrder() {
			const params = {
				workorder: this.formData.workorder
			};
			doFinishWorkOrder(params).then((res) => {
				if (res.code === 200) {
					uni.showToast({
						icon: 'success',
						title: '工单完成成功'
					});

					this.updateWorkOrderDetail(params);
					this.loading = false;
				} else {
					this.loading = false;
				}
			});
		},
		
		// 不放错 报工 + 完成工单
		doFinfishWorkOrder2(){
			let that = this
			this.loading = true;
			uni.showLoading({
				title:'报工中……'
			})
			// 不防错报工
			const params = {
				workorder:this.formData.workorder,
				reportNum:this.finishedNum
			}
			
			updateReport(params).then(res=>{
				if(res.code === 200){
					uni.showToast({
						icon: 'success',
						title: '报工成功',
						duration:2000,
						success() {
							const params2 = {
								workorder: that.formData.workorder
							};
							doFinishWorkOrder(params2).then((res) => {
								if (res.code === 200) {
									uni.showToast({
										icon: 'success',
										title: '完成工单'
									});
									that.loading = false;
									uni.hideLoading();
									setTimeout(()=>{
										uni.navigateBack(-1)
									},2000)
								} 
							});
						}
					});
				}
			}).finally(()=>{
				this.loading = true;
				uni.hideLoading();
			})
		},
		dialogConfirm3(){
			
		},
		dialogClose3(){
			
		}
	}
};
</script>

<style scoped>
.row-box {
	display: flex;
}
span {
	height: 100%;
	display: flex;
	align-items: center;
	justify-content: flex-start;
}
.input-item {
	height: 100%;
	display: flex;
	align-items: center;
	justify-content: flex-start;
}
</style>
