<template>
	<view>
		<uni-forms ref="form" :modelValue="formData" :rules="rules">
			<uni-row>
				<uni-col :span="24">
					<uni-forms-item label="生产工单" :labelWidth='90' name="pwoCode">
						<uni-easyinput suffixIcon="scan" @iconClick="scanBar" v-model="formData.pwoCode" type="text"
							@confirm="scanBarCode" />
					</uni-forms-item>
				</uni-col>
			</uni-row>
		</uni-forms>
		<u-button type="primary" @click="submit">关闭工单</u-button>
	</view>
</template>

<script>
	import {
		getPwo,
		listPwo,
		pwoAtificialClose
	} from "@/api/mes/pwoDraw.js";
	import {
		listWarehouse
	} from "@/api/wms/pdcIn.js";

	export default {
		mounted() {

		},
		data() {
			return {
				formData: {
					pwoCode: null,
				},
				id: null,
				value: 0,
				range: [],
				status: null,
				rules: {
					pwoCode: {
						rules: [{
							required: true,
							errorMessage: '请输入生产工单！'
						}]
					},
				}
			}
		},
		methods: {
			scanBarCode() {
				const _this = this;
				if (_this.formData.pwoCode) {
					let obj = {
						pwoCode: _this.formData.pwoCode
					}
					listPwo(obj).then(res => {
						if (res.rows.length > 0) {
							_this.id = res.rows[0].id;
							_this.status = res.rows[0].status;
						} else {
							_this.$modal.msg("未找到该工单！")
						}
					})
				}
			},
			scanBar() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.pwoCode = res.result;
						_this.scanBarCode();
					}
				});
			},
			submit() {
				const _this = this;
				_this.$refs.form.validate().then(res => {
					uni.showModal({
						title: '提示',
						content: '您确定关闭该生产工单吗？',
						success: function(res) {
							if (res.confirm) {
								//判断工单是否能关闭
								if (_this.status >= 7) {
									_this.$modal.msg("该工单无法关闭！")
								} else {
									_this.$modal.loading('提交中')
									pwoAtificialClose('"' + _this.id + '"').then(res => {
										_this.$modal.closeLoading();
										_this.$modal.msgSuccess("工单关闭成功！");
										setTimeout(() => {
											_this.$tab.switchTab("/pages/work/index");
										}, 500);
									});
								}
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				});
			},
		}
	}
</script>

<style>

</style>