<template>
	<view class="page">
		<navbar title="工序卡1" leftClick @leftClick="$utils.navigateBack" />
		<view class="info">
			
			<view class="Work-List">
				<view class="label">
					中试作业员：
				</view>
				<view class="text">
					<input type="text" v-model="StepOne.operator"/>
				</view>
			</view>
			
			<view class="Work-List">
				<view class="label">
					气缸料号1：
				</view>
				<view class="text">
					<input type="text" v-model="StepOne.cylinder1"/>
				</view>
			</view>
			
			<view class="Work-List">
				<view class="label">
					气缸料号2：
				</view>
				<view class="text">
					<input type="text" v-model="StepOne.cylinder2"/>
				</view>
			</view>
			
			<view class="Work-List">
				<view class="label">
					活塞料号1：
				</view>
				<view class="text">
					<input type="text" v-model="StepOne.piston1"/>
				</view>
			</view>
			
			<view class="Work-List">
				<view class="label">
					活塞料号2：
				</view>
				<view class="text">
					<input type="text" v-model="StepOne.piston2"/>
				</view>
			</view>
			
			<view class="Work-List">
				<view class="label">
					滑片料号1：
				</view>
				<view class="text">
					<input type="text" v-model="StepOne.scribing1"/>
				</view>
			</view>
			
			<view class="Work-List">
				<view class="label">
					滑片料号2：
				</view>
				<view class="text">
					<input type="text" v-model="StepOne.scribing2"/>
				</view>
			</view>
		</view>
		
		<view class="list">
			
			<view class="item-box"
				:key="stepIndex"
				v-for="(step, stepIndex) in stepList">
				<view class="title">
					{{ step.name }}
				</view>
				
				<view class="list-box"
				:key="cIndex"
				v-for="(c, cIndex) in step.workorderItemCheckList">
					<view class="title">
						{{ c.name }}
					</view>
					
					<view class="item-input"
					:key="wi"
					v-for="(w, wi) in c.workorderParamSteponeList"
					>
						<view class="Work-List"
						:key="xi"
						v-for="(x, xi) in 20"
						>
							<view class="label">
								规格{{ xi + 1 }}：
							</view>
							<view class="text">
								<input type="text" 
								v-model="w['param' + (xi + 1)]"/>
							</view>
							<view class="text"
							v-if=""
							style="margin-left: 20rpx;">
								{{ w.specs }}
							</view>
						</view>
					</view>
					
				</view>
			</view>
			
		</view>
		
		<view class="uni-color-btn"
		@click="submit">
			保存
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: 0,
				StepOne: {},
				stepList : [],
				index : 0,
			}
		},
		onLoad(arg) {
			this.id = arg.id
			this.index = arg.index || 0
			this.queryStepOne()
			this.queryTemplateById()
		},
		methods: {
			queryStepOne() {
				this.$api('queryStepOne', {
					templateId: this.id
				}, res => {
					if (res.code == 200) {
						this.StepOne = res.result.workorderGeneralStepone
					}
				})
			},
			// 根据id获取工单详情
			queryTemplateById(){
				this.$api('queryTemplateById', {
					templateId : this.id
				}, res =>{
					if(res.code == 200){
						let p = res.result.workorderStepList[this.index]
						this.stepList = p.workorderProcessList
					}
				})
			},
			//工单信息-修改工序参数-工序卡1(选配)
			updateParamStepOne(item){
				return new Promise((success, error) => {
					
					let data = JSON.parse(JSON.stringify(item))
					
					delete data.updateBy
					delete data.updateTime
					delete data.createBy
					delete data.createTime
					
					this.$api('updateParamStepOne', data, res => {
						if(res.code == 200){
							success(res)
						}else{
							error(res)
						}
					})
				})
			},
			async submit(){
				console.log(this.stepList);
				
				let StepOne = JSON.parse(JSON.stringify(this.StepOne))
				
				delete StepOne.updateBy
				delete StepOne.updateTime
				delete StepOne.createBy
				delete StepOne.createTime
				
				// 修改常规参数
				this.$api('updateGeneralStepOne', StepOne, res => {
					if(res.code == 200){
						success(res)
					}else{
						error(res)
					}
				})
				
				uni.showToast({
					title: '保存中...',
					icon: 'none'
				})
				
				// 任务列表
				let tasks = []
				
				for(let i = 0;i < this.stepList.length;i++){
					let step = this.stepList[i]
					for(let j = 0;j < step.workorderItemCheckList.length;j++){
						let check = step.workorderItemCheckList[j]
						
						for(let k = 0;k < check.workorderParamSteponeList.length;k++){
							let params = check.workorderParamSteponeList[k]
							// 将请求添加到任务
							tasks.push(this.updateParamStepOne(params))
						}
					}
				}
				
				// 等待任务所有完成
				await Promise.all(tasks)
				
				uni.showToast({
					title: '保存成功',
					icon: 'none'
				})
				
				setTimeout(uni.navigateBack, 1000, -1)
			},
		}
	}
</script>

<style scoped lang="scss">
	.page{
		padding-bottom: 200rpx;
		.Work-List{
			display: flex;
			padding: 16rpx 0rpx;
			align-items: center;
			.label{
				width: 200rpx;
				flex-shrink: 0;
			}
			.text{
				input{
					background-color: #eee;
					padding: 10rpx 20rpx;
					border-radius: 20rpx;
				}
			}
		}
		.info{
			padding: 30rpx;
			input{
				width: 430rpx;
			}
		}
		.list{
			padding: 0 30rpx;
			.list-box{
				padding: 20rpx 0;
				padding-left: 40rpx;
				.item-input{
					padding-left: 40rpx;
					.label{
						width: 130rpx;
					}
				}
			}
			input{
				width: 320rpx;
			}
		}
	}
</style>