<template>
	<view>
		<uni-collapse>
			<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-forms-item label="状态" :labelWidth='90'>
							<uni-tag :text="pwotag(status)" type="warning"></uni-tag>
						</uni-forms-item>
					</uni-col>
				</uni-row>
			</uni-forms>
			<uni-row style="margin-top: 20px;" :gutter="10">
				<uni-col :span="8" style="text-align: center;">
					<u-button type="warning" @click="selectPwoStock(formData.pwoCode)">物料核销</u-button>
				</uni-col>
				<uni-col :span="8" style="text-align: center;">
					<u-button type="success" @click="submitIn" :disabled="disabledIn">入库</u-button>
				</uni-col>
				<uni-col :span="8" style="text-align: center;">
					<u-button type="primary" @click="submitClose" :disabled="disabledClose">关闭工单</u-button>
				</uni-col>
			</uni-row>

		</uni-collapse>
		<view class="cu-card article ">
			<view class="cu-item shadow borderBottom" v-for="(item,index) in pwoJobList" :key="index">
				<view class="content">
					<view class="desc">
						<view class="text-content" style="font-size: 15px;">
							<view><strong>生产工单作业编码</strong> : {{item.code}}</view>
							<view><strong>制程编码</strong> : {{item.opCode}}</view>
							<view><strong>制程名称</strong> : {{item.opTitle}}</view>
							<view><strong>开工状态</strong> :
								<uni-tag :text="jobtag(item.status)" type="success"></uni-tag>
							</view>
							<view><strong>目标产量</strong> : {{item.targetNum}}</view>
							<view><strong>已完成数量</strong> : {{item.finishQty}}</view>
							<view><strong>合格总数</strong> : {{item.totalOutput}}</view>
							<view><strong>不良总数</strong> : {{item.totalDefect}}</view>
							<view><strong>报废总数</strong> : {{item.totalScrap}}</view>
							<view><strong>损耗总数</strong> : {{item.totalLoss}}</view>
							<uni-row>
								<uni-col :span="18">
									<view><strong>在制数量</strong> : {{item.makingNum}}</view>
									<view><strong>结存数量</strong> : {{item.surplusNum}}</view>
								</uni-col>
								<uni-col :span="6"> <u-button type="primary" @click="report(item.code)"
										v-show="item.status<5">报工</u-button></uni-col>
							</uni-row>
						</view>
					</view>
				</view>
			</view>
		</view>
		<u-popup :show="show" @close="close" @open="getMaterialStock" :closeable="true">
			<view class="cu-card article ">
				<view class="cu-item shadow borderBottom" v-for="(item,index) in materialStockList" :key="index">
					<view class="content">
						<view class="desc">
							<view class="text-content" style="font-size: 15px;">
								<view><strong>物料</strong> : {{item.materialCode+':'+item.materialName}}</view>
								<view><strong>批号</strong> : {{item.batchNo}}</view>
								<view><strong>类型</strong> :
									<uni-tag :text="typetag(item.type)" :type="colortag(item.type)"></uni-tag>
								</view>
								<view><strong>数量</strong> : {{item.number}}</view>
								<view><strong>仓库</strong> :
									<picker @change="val => {warehouseConfirm(val,item);}" :value="tyindex"
										:range="range" range-key="warehouseName">
										<view>选择:{{item.whName}}</view>
									</picker>
								</view>
							</view>

						</view>
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		getPwo,
		listPwo,
		pwoAtificialClose,
		handleIn,
		listCirculateBalance
	} from "@/api/mes/pwoDraw.js";
	import {
		listWarehouse
	} from "@/api/wms/pdcIn.js";
	import {
		listStock
	} from "@/api/mes/pwoIn.js";
	export default {
		mounted() {
			listWarehouse().then(async res => {
				this.range = res.rows
				// for (var i = 0; i < res.rows.length; i++) {
				// 	this.range[0][i].value = res.rows[i].warehouseCode;
				// 	this.range[0][i].text = res.rows[i].warehouseName;
				// }
			})
		},
		data() {
			return {
				tyindex: null,
				index: 0,
				show: false,
				materialStockList: [],
				wmsOpTransferOutStockList: [],
				disabledIn: false,
				disabledClose: false,
				formData: {
					pwoCode: null,
				},
				id: null,
				value: null,
				range: [],
				status: null,
				pwoJobList: [],
				rules: {
					pwoCode: {
						rules: [{
							required: true,
							errorMessage: '请输入生产工单！'
						}]
					},
				}
			}
		},
		methods: {
			typeCancel() {
				this.warehouseshow = false;
			},
			typeConfirm(e, item) {
				item.warehouseName = this.range[e.detail.value].warehouseName;
				item.warehouseCode = this.range[e.detail.value].warehouseCode;
				this.tyindex = e.detail.value
			},
			typeClose() {
				this.warehouseshow = false;
			},
			close() {
				this.show = false;
			},
			/** 获取物料信息 */
			getMaterialStock(ids) {
				// 清空物料库存信息
				this.materialStockList = [];
				listStock({
					pwoBelongCode: this.formData.pwoCode
				}).then((resp) => {
					this.materialStockList = this.materialStockList.concat(resp.rows);
				});
			},
			stockConvertion(code) {
				this.selectPwoStock(code)

			},
			selectPwoStock(pwoCode) {
				listStock({
					pwoBelongCode: pwoCode
				}).then((res) => {
					if (res.rows.length > 0) {
						this.wmsOpTransferOutStockList = res.rows.map(obj => {
							return {
								stockId: obj.id,
								materialCode: obj.materialCode,
								materialName: obj.materialName,
								batchNo: obj.batchNo,
								lotNo: obj.lotNo,
								pieceNo: obj.pieceNo,
								storageLocationCode: obj.storageLocationCode,
								type: obj.type,
								num: obj.number
							}
						});
						console.log(this.wmsOpTransferOutStockList)
						if (this.wmsOpTransferOutStockList && this.wmsOpTransferOutStockList != []) {
							uni.navigateTo({
								url: '/pages/mes/jobCv/stockConvertion?wmsOpTransferOutStockList=' +
									encodeURIComponent(
										JSON.stringify(this.wmsOpTransferOutStockList))
							});
						}
					} else {
						this.$modal.msg("未查询到该工单工序消耗信息！")
					}
				});
			},
			colortag(type) {
				switch (Number(type)) {
					case 1:
						return 'success';
						break;
					case 2:
						return 'warning';
						break;
					case 3:
						return 'error';
						break;
					case 4:
						return 'primary';
						break;
					default:
						return '无';
						break;
				}
			},
			typetag(type) {
				switch (Number(type)) {
					case 1:
						return '合格品';
						break;
					case 2:
						return '不良品';
						break;
					case 3:
						return '报废品';
						break;
					case 4:
						return '待退料';
						break;
					default:
						return '无';
						break;
				}
			},
			jobtag(status) {
				switch (Number(status)) {
					case 1:
						return '计划中';
						break;
					case 2:
						return '材料转入';
						break;
					case 3:
						return '作业开工';
						break;
					case 4:
						return '作业暂停';
						break;
					case 5:
						return '作业完工';
						break;
					case 6:
						return '完成品转出';
						break;
					case 7:
						return '取消';
						break;
					default:
						return '无';
						break;
				}

			},
			pwotag(status) {
				switch (Number(status)) {
					case 1:
						return '计划中';
						break;
					case 2:
						return '已生成作业';
						break;
					case 3:
						return '已投料';
						break;
					case 4:
						return '已领料';
						break;
					case 5:
						return '已开工';
						break;
					case 6:
						return '暂停';
						break;
					case 7:
						return '已完成';
						break;
					case 8:
						return '结案';
						break;
					case 9:
						return '取消';
						break;
					case 10:
						return '待入库';
						break;
					case 11:
						return '待转出';
						break;
					case 12:
						return '已转出';
						break;
					case 13:
						return '其他';
						break;
					default:
						return '无';
						break;
				}
			},
			scanBarCode() {
				const _this = this;
				_this.disabledClose = false;
				_this.disabledIn = false;
				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;
							if (_this.status >= 7) {
								_this.disabledClose = true;
							} else if (_this.status != 10) {
								_this.disabledIn = true;
							}
						} else {
							_this.$modal.msg("未找到该工单！")
						}
					});
					_this.$modal.loading('提交中')
					listCirculateBalance(obj).then((response) => {
						_this.$modal.closeLoading();
						_this.pwoJobList = response.rows;
					});
				}
			},

			scanBar() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.pwoCode = res.result;
						_this.scanBarCode();
					}
				});
			},
			submitClose() {
				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('用户点击取消');
							}
						}
					});
				});
			},
			report(code) {
				uni.navigateTo({
					url: '/pages/mes/jobReport/onceReport?code=' + code
				});
			},
			async submitIn() {
				this.materialStockList = [];
				this.$modal.loading('提交中')
				await listStock({
					pwoBelongCode: this.formData.pwoCode
				}).then((resp) => {
					this.$modal.closeLoading();
					this.materialStockList = this.materialStockList.concat(resp.rows);
					uni.navigateTo({
						url: '/pages/mes/jobCv/pwoWarehousing?pwoCode=' + this.formData.pwoCode +
							'&materialStockList=' + encodeURIComponent(JSON.stringify(
								this.materialStockList))
					});
				});

				// const _this = this;
				// this.$refs.form.validate().then(res => {
				// 	uni.showModal({
				// 		title: '提示',
				// 		content: '您确定入库该生产工单吗？',
				// 		success: function(res) {
				// 			if (res.confirm) {
				// 				if (_this.status != 10) {
				// 					console.log(_this.status);
				// 					_this.$modal.msg("该工单不是‘待入库’状态,请重新输入！")
				// 				} else {
				// 					_this.formData.warehouseCode = _this.value
				// 					console.log(_this.formData)
				// 					handleIn(Number(_this.formData.pwoCode.slice(4)), _this.formData
				// 						.warehouseCode).then(res => {
				// 						_this.$modal.msgSuccess("入库成功！");
				// 						setTimeout(() => {
				// 							_this.$tab.switchTab("/pages/work/index");
				// 						}, 500);
				// 					});
				// 				}
				// 			} else if (res.cancel) {
				// 				console.log('用户点击取消');
				// 			}
				// 		}
				// 	});
				// });
			},
		}
	}
</script>

<style>
	.cu-card.article>.cu-item .content .text-content {
		height: 100% !important;
	}

	.cu-card>.cu-item {
		margin: 13px 0;
	}
</style>