<template>
  <view class="container">
    <view class="list">
		<view class="item" v-for="(item, index) in list" :key="index" >
			<view class="dec">
			  <view>日计划编码：</view><view>{{ item.planDayCode }}</view>
			</view>
			<view class="dec">
			  <view>日任务编码：</view><view>{{ schedulingCodeStr }}</view>
			</view>
			<view class="dec">
			  <view>工序编码：</view><view>{{ item.processCode }}</view>
			</view>
			<view class="dec">
			  <view>物料号：</view><view>{{ item.repMaterialCode }}</view>
			</view>
			<view class="dec">
			  <view>计划数量：</view><view>{{ item.srcMaterialCounts * planCountStr }}</view>
			</view>
			<view class="dec" style="font-weight: bold;">
				<view >叫料数量：</view>
					<view ><u-number-box v-model="item.totalMaterialCounts"  style="width:150px" :max="(item.srcMaterialCounts * planCountStr)" :positive-integer="false"></u-number-box>
			<!-- 							<u-input v-model="item.srcMaterialCounts" type="number" placeholder="补料数量" border="true" clearable="true" style="width: 100px;"  @update:modelValue="calculatePass(item,queryForm.planCount)" /> -->
									 </view>
							</view>
<!-- 			<view>
				<u-form-item label="叫料数量" prop="callMaterialCounts">
					<u-input v-model="item.totalMaterialCounts" placeholder="请输入叫料数量"/>
				</u-form-item>
			</view> -->
		</view>
		<view >
		 	<u-button type="primary" @click="submitCMForm()">提交</u-button>
		</view>
		<view style="height: 94rpx;padding-top: 30rpx;">
				<u-loadmore :status="status" v-if="status != 'loadmore'" />
		</view>
    </view>
	
	<view style="height: constant(safe-area-inset-bottom); height: env(safe-area-inset-bottom);"></view>
  </view>
</template>

<script setup lang="ts">
import {
  onLoad,
  onShow,
  onReachBottom
} from '@dcloudio/uni-app'
import {
  ref,
  getCurrentInstance
} from 'vue'
import * as workSchedulingListApi from '@/api/mes/workScheduling/index.ts'

const {proxy} = getCurrentInstance()
const loading = ref(false)
const status = ref('loadmore') //是否显示没有更多了
const workingNodeStr = ref('')
const planMasterCodeStr = ref('')
const schedulingCodeStr = ref('')
const planCountStr = ref('')
const workroomCodeStr = ref('')
const list = ref([])


const form = ref({
  callMaterialCounts: '',
})

const params = ref({
  pageNo: 1,
  pageSize: 10,
  planDayCode: '',
  processCode: '',
  remark:''
})

const param = ref({
  remark:''
})

function getBomInspectList() {
	if (status.value == 'nomore') return
	status.value = 'loading'
	proxy.$modal.loading('加载中')
	params.value.planDayCode = planMasterCodeStr.value
	params.value.processCode = workingNodeStr.value
	workSchedulingListApi.callBasicItem(params.value).then((res) => {
		proxy.$modal.closeLoading()
		if (res.data.list.length > 0) {
			list.value = list.value.concat(res.data.list)
			params.value.pageNo++
			status.value = 'loadmore'
		} else {
		  status.value = 'nomore'
		}
	  }).catch(() => {
		  proxy.$modal.closeLoading()
	})
}

function submitCMForm () {
	for (var i = 0; i < list.value.length; i++) {
		console.log(list.value[i].repMaterialCode+":"+list.value[i].totalMaterialCounts);
		list.value[i].schedulingCode = schedulingCodeStr.value
		list.value[i].workroomCode= workroomCodeStr.value
		if(/^\d+$/.test(list.value[i].totalMaterialCounts)){
			console.log(/^\d+$/.test(list.value[i].totalMaterialCounts))
		}else{
			proxy.$modal.alert("叫料数量请输入正确的数字");
			return
		}
		if( parseInt(list.value[i].totalMaterialCounts) > parseInt(list.value[i].srcMaterialCounts * planCountStr.value)){
			proxy.$modal.alert("叫料数量不能大于计划数量");
			return
		}
		if( parseInt(list.value[i].totalMaterialCounts) == 0 ){
			proxy.$modal.alert("叫料数量不能为0");
			return
		} 
		
	}
	proxy.$modal.confirm('确定提交处理吗').then(() => {
		param.value.remark = JSON.stringify(list.value)
		workSchedulingListApi.callBasicItemForm(param.value).then((res) => {
			console.log(res)
			if (res.data) {
				proxy.$modal.showToast('成功')
				setTimeout(() => {
					proxy.$tab.navigateBack()
					loading.value = false
					proxy.$modal.closeLoading()
				}, 1000)
			}else{
				proxy.$modal.showToast('失败')
				loading.value = false
				proxy.$modal.closeLoading()
			}
		})
	})
	
}	

//滑动到底部展示
onReachBottom(() => {
  status.value = 'loadmore'	
  getBomInspectList()
})



onLoad((option) => {
  if (option.obj) {
	schedulingCodeStr.value = JSON.parse(option.obj).schedulingCode
  	workingNodeStr.value = JSON.parse(option.obj).workingNode
  	planMasterCodeStr.value = JSON.parse(option.obj).planMasterCode
	planCountStr.value = JSON.parse(option.obj).planCount
	workroomCodeStr.value = JSON.parse(option.obj).workroomCode
  }
})
onShow(() => {
  getBomInspectList()
})

</script>

<style lang="scss" scoped>
	.container{
		background: #f5f5f5;
		min-height: 100vh;
	}
	.list {
		background: #f5f5f5;
		margin-top: 20rpx;
		.item {
			padding: 30rpx 30rpx 0px 30rpx;
			margin-top: 20rpx;
			background: white;
			position: relative;
			.title {
				display: flex;
				align-items: center;
				padding-bottom: 20rpx;
				.title-txt {
					color: #409eff;
					font-weight: bold;
					font-size: 36rpx;
					width: 0px;
					flex: 1;
					word-wrap: break-word;
				}
				.time {
					color: #919191;
				}
			}
			.dec {
				padding-bottom: 20rpx;
				display: flex;
				align-items: center;
				view {
					&:nth-child(1){
						width: 200rpx;;
					}
					&:nth-child(2){
					color: #999999;
						flex: 1;
						width: 0px;
						word-wrap: break-word;
					}
				}
			}
			.last {
				padding-bottom: 30rpx;
			}
			.bottom {
				display: flex;
				justify-content: space-between;
				align-items: center;
				border-top: 1px solid #E4E4E4;
				padding: 20rpx 0px;
				height: 90rpx;
			}
		}
	}
</style>