<template>
	<!-- 扫码报工 -->
	<view>
		<uni-card v-if="!formData.workorder">
			<text>{{ '线别:' + lineCode }}</text>
			<text style="margin-left: 20px">{{ '组别' + 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 label="已报工数" name="finishedNum">
					<span style="font-size: 26px;">{{ formData.finishNum }}</span>
				</uni-forms-item>
			</uni-forms>
		</uni-card>
		<view class="bottom-box row-box" v-if="formData.workorder">
			<button :disabled="loading" type="primary" @click="doStartWorkOrder">开始工单</button>
			<button :disabled="loading" type="warn" @click="doFinfishWorkOrder">完成工单</button>
		</view>
	</view>
</template>

<script>
import { errorProofingAndReportingWork } from '@/api/workorder/proreportwork.js';
import { GetWorkOrderDetail,startWorkOrder,finishWorkOrder } from '@/api/workorder/proworkorder.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
			}
		};
	},
	methods: {
		// 扫工单码结果
		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.code !== 200) {
				uni.showToast({
					icon: 'error',
					title: '标签异常'
				});
				this.loading = false;
				return;
			}
			if (res1.data.lineCode !== this.lineCode || res1.data.groupCode !== this.groupCode) {
				uni.showToast({
					icon: 'none',
					title: '扫码结果异常,该工单不属于此线别或组别'
				});
				this.loading = false;
				return;
			}
			this.formData = res1.data;
			this.loading = false;
		},
		// 扫产品码结果
		async scanConfirm2(val) {
			if (val === '' || val === null) {
				return;
			}
			const params = {
				workorder: this.formData.workorder,
				labelContext: val
			};
			let res1 = await errorProofingAndReportingWork(params);
			if (res1.code !== 200) {
				uni.showToast({
					icon: 'error',
					title: '标签异常'
				});
				return;
			}
			if (res1.data === -1) {
				uni.showToast({
					icon: 'none',
					title: '此产品不属于此工单'
				});
				return;
			}
			if (res1.data === 0) {
				uni.showToast({
					icon: 'none',
					title: '报工失败'
				});
				return;
			}
			if (res1.data === 1) {
				uni.showToast({
					icon: 'success',
					title: '报工成功'
				});
				this.updateWorkOrderDetail(params)
				return;
			}
		},
		// 更新工单信息
		updateWorkOrderDetail(params){
			GetWorkOrderDetail(params).then(res=>{
				if(res.code === 200){
					this.formData = res.data;
				}
			})
		},
		// 开始工单
		doStartWorkOrder() {
			this.loading = true
			const params = {
				workorder:this.formData.workorder
			}
			startWorkOrder(params).then(res=>{
				if(res.code === 200){
					uni.showToast({
						icon: 'success',
						title: '工单开始成功'
					});
					this.updateWorkOrderDetail(params)
					this.loading = false
				}else{
					this.loading = false
				}
			})
		},
		// 完成工单
		doFinfishWorkOrder() {
			const params = {
				workorder:this.formData.workorder
			}
			finishWorkOrder(params).then(res=>{
				if(res.code === 200){
					uni.showToast({
						icon: 'success',
						title: '工单完成成功'
					});
					
					this.updateWorkOrderDetail(params)
					this.loading = false
				}else{
					this.loading = false
				}
			})
		}
	}
};
</script>

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