<template>
	<!-- 工单报工 -->
	<view>
		<uni-card>
			<uni-forms :modelValue="formData" label-width="80px" label-align="right">
				<uni-forms-item label="工单号" name="fkWorkorder">
					<span>{{formData.fkWorkorder}}</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="dispatchNum">
					<span>{{formData.dispatchNum}}</span>
				</uni-forms-item>
				<uni-forms-item label="报工数" name="finishedNum">
					<uni-easyinput type="number" v-model.number="formData.finishedNum" placeholder="请输入报工数" />
				</uni-forms-item>
			</uni-forms>
		</uni-card>
		<view class="bottom-box">
			<button :disabled="loading" type="primary" @click="submit">确认报工</button>
		</view>
	</view>
</template>

<script>
	import { reportWorkOrderFinishNum,manualGenerationOfReportWork,updateProReportwork } from '@/api/workorder/proreportwork.js';
export default {
	onLoad: function (option) {
		this.$nextTick(()=>{
			this.formData = JSON.parse(JSON.stringify(option));
			if(this.formData.id === null || this.formData.id === ''){
				this.loading = true
				manualGenerationOfReportWork({workorder:this.formData.fkWorkorder}).then(res=>{
					if(res.code === 200){
						this.formData.id = res.data
						this.loading = false
					}else{
						uni.showToast({
							icon: 'error',
							title: '数据异常',
						});
						this.loading = false
					}
				})
			}
		})
	},
	data() {
		return {
			loading:false,
			formData: {
				id:null,
				finishNum:0
			}
		};
	},
	methods: {
		// 报工提交
		submit() {
			this.loading = true;
			uni.showLoading({
				title:'报工中……'
			})
			const params = {
				id:this.formData.id,
				finishedNum:this.formData.finishedNum
			}
			updateProReportwork(params).then(res=>{
				if(res.code === 200){
					this.loading = false;
					uni.showToast({
						icon: 'success',
						title: '报工成功',
						duration:2000,
						success() {
							setTimeout(()=>{
								uni.navigateBack(-1)
							},2000)
							
						}
					});
					uni.hideLoading();
				}else{
					uni.hideLoading();
				}
			}).catch(()=>{
				uni.hideLoading();
			})
		}
	}
};
</script>

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