<template>
	<view class="app-container">
		<uni-nav-bar dark :fixed="true" shadow background-color="#007AFF" status-bar :title="title"
							left-text="刷新" @clickLeft="handleReload"
							:right-text="workstationName==null || workstationName=='' ? '工站' : workstationName" @clickRight="handleWork"/>
		<view class="scan-header" style="z-index: auto;">
			<uni-forms-item  name="barCode" label="条码" :label-width="lableWidth" labelAlign="right" 
					style="margin-bottom: 0px; padding: 0px 15px 0px 15px;" >
				<uni-easyinput type="text" :inputBorder="true" v-model="barCode"
					:clearable="false"></uni-easyinput>
				<view class="icon">
					<uni-icons type="scan" size="20"></uni-icons>
				</view>
			</uni-forms-item>
		</view>
		<view class="cpck-content">
			<scroll-view scroll-top="0" :show-scrollbar="true" scroll-y="true" class="scroll-Y">
				<uni-card v-for="(item, index) in taskList" :key="index" @click="shiftTask(item)" :style="item.taskId===taskData.taskId ? 'background: #fffbc7;' : ''" style="margin: 0px 15px 15px 15px">
					<text class="uni-body" style="font-size: 16px;">
						<p><span class="titleV">任务编码：</span><span class="detailV">{{item.taskCode}}</span></p>
						<p><span class="titleV">产品编码：</span><span class="detailV">{{item.itemCode}}</span></p>
						<p><span class="titleV">产品名称：</span><span class="detailV">{{item.itemName}}</span></p>
						<p><span class="titleV">规格：</span><span class="detailV">{{item.specification}}</span></p>
						<p><span class="titleV">单位：</span><span class="detailV">{{item.unitOfMeasure}}</span></p>
						<p><span class="titleV">任务数量：</span><span class="detailV">{{item.quantity}}</span></p>
						<p><span class="titleV">生产数量：</span><span class="detailV">{{item.quantityProduced}}</span></p>
						<p><span class="titleV">良品数量：</span><span class="detailV">{{item.quantityQuanlify}}</span></p>
						<p><span class="titleV">不良数量：</span><span class="detailV">{{item.quantityUnquanlify}}</span></p>
						<p><span class="titleV">排产时间：</span><span class="detailV">{{item.startTime}}</span></p>
						<p><span class="titleV">需求日期：</span><span class="detailV">{{item.requestDate}}</span></p>
					</text>
				</uni-card>
			</scroll-view>
		</view>
			
		<uni-popup ref="popup" type="top" :animation="true" style="" background-color="#ffffff">
			<div style="height:50px"/>
			<uni-forms-item  name="process" label="工序" :label-width="lableWidth" style="margin:5px;width:300px"  labelAlign="right">
				<uni-data-select
					v-model="processCode"
					:localdata="processList"
					@change="changeProcess"
				></uni-data-select>
			</uni-forms-item>
			<uni-forms-item  name="workstation" label="工作站" :label-width="lableWidth" style="margin:5px;width:300px"  labelAlign="right">
				<uni-data-select
					v-model="workstationCode"
					:localdata="workstationList"
					@change="changeWorksta"
				></uni-data-select>
			</uni-forms-item>
		</uni-popup>
		
		<uni-popup ref="feedbackPopup" type="top" :animation="true" background-color="#ffffff">
			<uni-forms-item name="stockQty" label="合格品数" :label-width="lableWidth" labelAlign="right">
				<uni-easyinput type="number" :inputBorder="true"
					v-model="feedbackForm.quantityQualify" @input="bqslInputHandle" ></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="stockQty" label="不良品数" :label-width="lableWidth" labelAlign="right">
				<uni-easyinput type="number" :inputBorder="true"
					v-model="feedbackForm.quantityUnqualify" @input="bqslInputHandle" ></uni-easyinput>
			</uni-forms-item>
			<button type="primary" plain="true" @click="handlefeedback">确定</button>
		</uni-popup>
		
		<view class="page-bottom">
			<view class="p-b-btn" @click="changeStatus('START')" v-if="taskData.status =='NORMAL'">
				<text class="box-text">开始</text> 
			</view> 
			<view class="p-b-btn" @click="changeStatus('PAUSE')" v-else-if="taskData.status =='START'">
				<text class="box-text">暂停</text> 
			</view> 
			<view class="p-b-btn" @click="changeStatus('START')" v-else-if="taskData.status =='PAUSE'">
				<text class="box-text">继续</text> 
			</view> 
			<view class="p-b-btn" v-else></view>
			
			<view class="p-b-btn" @click="changeStatus('FINISHED')" v-if="taskData.status!=null && taskData.status!=''">
				<text class="box-text">完成</text> 
			</view> 
			<view class="p-b-btn" v-else></view>
			
			<view class="p-b-btn" @click="doFeedback" v-if="taskData.status!=null && taskData.status!=''">
				<text class="box-text">报工</text> 
			</view> 
			<view class="p-b-btn" v-else></view>
			
			<view class="p-b-btn" @click="handleBack">
				<text class="box-text">返回</text> 
			</view> 
		</view>
	</view>
</template>

<script>
	import { processList,workstationList,taskList,changeTaskStatus,feedback } from '@/api/mes/pro.js'
	export default {
		onLoad(option) {
			this.processName = uni.getStorageSync('processName');
			this.processCode = uni.getStorageSync('processCode');
			this.processId = uni.getStorageSync('processId');
			this.workstationName = uni.getStorageSync('workstationName');
			this.workstationCode = uni.getStorageSync('workstationCode');
			this.workstationId = uni.getStorageSync('workstationId');
		},
		data() {
			return {
				title:"生产",
				lableWidth:"80px",
				barCode:'',//条码值
				//切换工作站
				showWorkstationFlag:false,
				processName:null,//当前工序
				processCode:null,
				processId:null,//当前工序id
				workstationName:null,//当前工作站
				workstationCode:null,//
				workstationId:null,//当前工作站id
				processList: [], //工序清单
				workstationList: [], //工作站清单
				currentFlag: 0,
				//任务卡片
				taskList: [],
				taskData:{},//选中的任务
				//报工
				feedbackForm:{quantityQualify:0,quantityUnqualify:0}
			}
		},
		methods: {
			//刷新按钮
			handleReload(){
				this.getTaskList();
				uni.showToast({
					title: '刷新成功',
					icon: 'none',
					duration: 1500
				});
			},
			handleWork(){
				//查询工序
				processList().then(res => {
					if(res.code === 200){
						this.processList = res.data.map(item => ({
							value: item.processCode,
							text: item.processName,
							id: item.processId
						}));
						//显示工站弹出窗
						this.$refs.popup.open('top')
					}
				});
				//如果有工序id，查询工作站
				if(this.processId !== null && this.processId !== ''){
					//查询工作站
					this.getWorkstationList();
				}
			},
			//工序变更，获取工作站
			changeProcess() {
				//赋值工序id
				let process = this.processList.find(item => item.value === this.processCode)
				this.processName = process==null ? null:process.text;
				this.processId = process==null ? null:process.id;
				uni.setStorageSync('processName', this.processName);
				uni.setStorageSync('processCode', this.processCode);
				uni.setStorageSync('processId', this.processId);
				//查询工作站
				this.getWorkstationList();
				//清空工作站
				this.workstationName=null
				this.workstationCode=null
				this.workstationId=null
				uni.setStorageSync('workstationName', '');
				uni.setStorageSync('workstationCode', '');
				uni.setStorageSync('workstationId', '');
			},
			//查询工作站
			getWorkstationList(){
				workstationList({processCode: this.processCode}).then(res => {
					if(res.code === 200){
						this.workstationList = res.data.map(item => ({
							value: item.workstationCode,
							text: item.workstationName,
							id: item.workstationId
						}));
					}
				});
			},
			//工作站变更
			changeWorksta(){
				let workstation = this.workstationList.find(item => item.value === this.workstationCode)
				this.workstationName = workstation==null ? null:workstation.text;
				this.workstationId = workstation==null ? null:workstation.id;
				uni.setStorageSync('workstationName', this.workstationName);
				uni.setStorageSync('workstationCode', this.workstationCode);
				uni.setStorageSync('workstationId', this.workstationId);
				//查询任务
				this.getTaskList();
			},
			//查询任务列表
			getTaskList() {
				taskList({workstationId: this.workstationId}).then(res => {
					if(res.code === 200){
						this.taskList = res.rows;
					}
				});
			},
			//选中任务
			shiftTask(task){
				this.taskData = task
			},
			//状态按钮
			changeStatus(status) {
				this.taskData.status = status;
				let params = {
					taskId: this.taskData.taskId,
					taskCode: this.taskData.taskCode,
					workstationId: this.workstationId,
					workstationCode: this.workstationCode,
					workstationName: this.workstationName,
					status: status
				};
				changeTaskStatus(params).then(res => {
					if (res.code == '200') {
						uni.showToast({
							title: '变更成功',
							icon: 'none'
						});
						this.getTaskList();//刷新任务
						//清空选中任务
						if(status=='FINISHED'){
							this.taskData={};
						}
					}
				});
			},
			//生产报工按钮
			doFeedback() {
				this.$refs.feedbackPopup.open('bottom')
			},
			//返回按钮
			handleBack(){
				uni.showModal({
					title: '提示',
					content: '确认返回?',
					success: function (res) {
						if (res.confirm) {
							uni.navigateBack();
						} else if (res.cancel) {
						
						}
					}
				});
			},
			//提交按钮
			handlefeedback(){
				//汇总数量
				let quantityQualify = this.feedbackForm.quantityQualify==null || this.feedbackForm.quantityQualify=='' ? 0:this.feedbackForm.quantityQualify
				let quantityUnqualify = this.feedbackForm.quantityUnqualify==null || this.feedbackForm.quantityUnqualify=='' ? 0:this.feedbackForm.quantityUnqualify
				let quantity = 0 + parseFloat(quantityQualify) + parseFloat(quantityUnqualify);
				if(quantityQualify<0 || quantityUnqualify<0){
					uni.showToast({
						title: "数量有误！",
						icon: 'none'
					});
					return true;
				}
				if(quantity===0){
					uni.showToast({
						title: "请输入合格品数量或者不良品数量！",
						icon: 'none'
					});
					return true;
				}
				
				let userName = uni.getStorageSync('u1')
				let nickName = uni.getStorageSync('name')
				let params = {
					taskId: this.taskData.taskId,
					quantityFeedback: quantity,
					quantityQualified: quantityQualify,
					quantityUnquanlified: quantityUnqualify,
					userName: userName,
					nickName: nickName,

					// 报工类型  : UNI  SELF
					feedbackType: 'SELF',

					//工序
					processId: this.processId,
					processName: this.processName,
					itemId: this.taskData.itemId,
					itemName: this.taskData.itemName,
					itemCode: this.taskData.itemCode
				}
				
				feedback(params).then(res => {
					if(res.code === 200){
						uni.showToast({
							title: '上报成功',
							icon: 'none'
						});
					}
					this.getTaskList();
				});
			},
			
			bqslInputHandle(value){ //数量输入完成校验
				if(value < 0){
					uni.showToast({
						title: "数量有误",
						icon: "none"
					})
					return;
				}
			},
		}
	}
</script>

<style>
	@import "@/static/scss/stock.css";
</style>