<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" v-if="Object.keys(taskData).length === 0">
			<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>
		
		
		<view class="cpck-content" v-if="Object.keys(taskData).length !== 0">
			<scroll-view scroll-top="0" :show-scrollbar="true" scroll-y="true" class="scroll-Y">
				<uni-card v-for="(item, index) in itemList" :key="index" @click="shiftItem(item)" :style="item.recordId===itemData.recordId ? '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.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.quantityAvailable}}</span></p>
					</text>
				</uni-card>
			</scroll-view>
		</view>
			
		
		<view class="page-bottom">
			<view class="p-b-btn" @click="searchIconClick('lld')" v-if="Object.keys(taskData).length !== 0">
				<text class="box-text">领料单</text> 
			</view> 
			<view class="p-b-btn" v-else></view>
			
			<view class="p-b-btn" @click="handleRemove" v-if="Object.keys(taskData).length !== 0">
				<text class="box-text">移除</text> 
			</view> 
			<view class="p-b-btn" v-else></view>
			
			<view class="p-b-btn" @click="handleConsume" v-if="Object.keys(taskData).length !== 0">
				<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>
		
		<uni-popup ref="consumeFormPopup" 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="consumeForm.quantityConsumed" @input="bqslInputHandle" ></uni-easyinput>
			</uni-forms-item>
			<button type="primary" plain="true" @click="handleConsumeForm">确定</button>
		</uni-popup>
		
		<!-- 领料单 弹窗 -->
		<lldPopup ref="lldPopup" :title="lldTitle" :idxFlag="lldFlag" :formData="formData" @sendData="getSendData"/> 
	</view>
</template>

<script>
	import lldPopup from './popup/commonpopup.vue' // 基础资料
	import { processList,workstationList,taskList,changeTaskStatus,feedback,addIssue,taskIssueList,removeTaskIssue,consumeTaskIssue } from '@/api/mes/pro.js'
	export default {
		components: {
			lldPopup
		},
		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');
			//进入加载任务
			this.getTaskList()
			this.formData.workstationId=this.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:{},//选中的任务
				taskId:null,//任务id
				//领料单弹窗
				lldTitle: "领料单",
				lldFlag: "lld",
				formData: {},//workstationId工作站id、workorderId任务id
				//投料卡片
				itemList: [],//投料清单
				itemData:{},//选中的投料
				//消耗弹窗
				consumeForm: {quantityConsumed:0},
			}
		},
		methods: {
			//任务按钮
			handleReload(){
				this.taskData = {}
				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
				this.formData.workorderId = task.workorderId
				this.taskId = task.taskId
				this.getIssueList()
			},
			//选中投料清单
			shiftItem(item){
				this.itemData = item
				console.log(item)
			},

			//添加领料单的某行到当前的投料清单
			handleAdd(row) {
				let params = {
					taskId: this.taskId,
					workstationId: this.workstationId,
					//孙一石 工站id修改
					sourceDocId: row.sourceDocId,
					sourceDocType: 'ISSUE',
					sourceLineId: row.sourceLineId,
					itemCode: row.itemCode,
					itemName: row.itemName,
					unitOfMeasure: row.unitOfMeasure,
					batchCode: row.batchCode,
					quantityIssued: row.quantityIssued,
					quantityAvailable: row.quantityIssued
				}

				addIssue(params).then(res => {
					if(res.code === 200){
						this.getIssueList();
						uni.showToast({
							title: '操作成功',
							icon:'none',
							duration: 1500
						})
					}
				});
			},
			
			// 从投料清单中删除一条
			handleRemove() {
			  let item = this.itemData;
				removeTaskIssue({recordId: item.recordId}).then(res => {
					if(res.code === 200){
						this.getIssueList();
						uni.showToast({
							title: '操作成功',
							icon:'none',
							duration: 1500
						})
					}
				});
			},
			
			// 从投料清单中消耗数量
			handleConsume() {
			  let item = this.itemData;
			  this.consumeForm.itemName = item.itemName
			  this.consumeForm.itemCode = item.itemCode
			  this.consumeForm.unitOfMeasure = item.unitOfMeasure
			  this.consumeForm.quantityConsumed = item.quantityAvailable
			  this.consumeForm.sourceDocId = item.sourceDocId
			  this.consumeForm.sourceLineId = item.sourceLineId
			  this.consumeForm.taskId = item.taskId
			  this.consumeForm.workstationId = item.workstationId
			  this.consumeForm.sourceBatchCode = item.batchCode
				this.$refs.consumeFormPopup.open('bottom')
			},
			handleConsumeForm(){
				let params= {
					sourceDocId: this.consumeForm.sourceDocId,
					sourceLineId: this.consumeForm.sourceLineId,
					itemName: this.consumeForm.itemName,
					itemCode: this.consumeForm.itemCode,
					unitOfMeasure: this.consumeForm.unitOfMeasure,
					quantityConsumed: this.consumeForm.quantityConsumed,
					taskId: this.consumeForm.taskId,
					workstationId: this.consumeForm.workstationId,
					sourceBatchCode: this.consumeForm.sourceBatchCode
				}
				consumeTaskIssue(params).then(res => {
					if(res.code === 200){
						this.getIssueList();
						uni.showToast({
							title: '操作成功',
							icon:'none',
							duration: 1500
						})
					}
				});
			},
			//获取当前工作台、当前生产任务的投料清单
			getIssueList() {
				taskIssueList({workstationId:this.workstationId}).then(res => {
					if(res.code === 200){
						this.itemList = res.data;
						console.log(this.itemList)
					}
				});
			},

			searchIconClick(type){// 显示基础资料
				switch (type){
					case "lld":  // 任务编码
						this.$refs.lldPopup.showPopup();
						break;
					default:
						break;
				}
			},
			getSendData(res){ //基础资料
				let cFlag = res.currentFlag; 
				let obj = {};
				switch (cFlag){
					case "lld": // 选择领料单
						this.handleAdd(res.selectData)
						break;
					default:
						break;
				}
			},
			
			//返回按钮
			handleBack(){
				uni.showModal({
					title: '提示',
					content: '确认返回?',
					success: function (res) {
						if (res.confirm) {
							uni.navigateBack();
						} else if (res.cancel) {
						
						}
					}
				});
			},
			
			bqslInputHandle(value){ //数量输入完成校验
				if(value < 0){
					uni.showToast({
						title: "数量有误",
						icon: "none"
					})
					return;
				}
			},
		}
	}
</script>

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