<template>
	<!-- 列表展示标准模版 -->
	<view class="container">
		<view class="list">
			<view class="item" v-for="(item,index) in list" :key="index">
				<view class="title">
					<view class="title-txt">
						{{item.repMaterialCode}}
					</view>
					<view class="time">
						{{`${$time.formatDate(item.createTime)}`}}
					</view>
				</view>
<!-- 				<view class="dec">
					<view>计划编码：</view><view>{{item.planDayCode}}</view>
				</view>
				<view class="dec">
					<view>批次编码：</view><view>{{item.planBatchCode}}</view>
				</view> -->
				<view class="dec">
					<view>产品编码：</view><view>{{item.productCode}}</view>
				</view>
				<view class="dec">
					<view>工序编码：</view><view>{{item.processCode}}</view>
				</view>
				<view class="dec">
					<view>物料规格：</view><view>{{item.repMaterialModel}}</view>
				</view>
				<view class="dec">
					<view>物料数量：</view>
						<view> {{item.repMaterialCounts * queryForm.planCount}}</view>
				</view>
				<view class="dec" style="font-weight: bold;">
					<view >补料数量：</view>
						<view > <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 class="dec">
					<view>
						<u-button size="medium" type="error" @click="deleteItem(item)" style="width: 20px;">删除物料</u-button>
					</view>
<!-- 					<view >
						<u-button size="medium" type="success" @click="setupNum(item)" style="margin-left: 100px;width: 20px;">选择数量</u-button>
					</view> -->
				</view>
			</view>
		</view>
		<view class="footer">
			<view class="btns" v-if="listSize!=0">
				<u-button type="success" @click="handleSubmit" :loading='loading' :disabled='loading'>提交</u-button>
			</view>
			<view class="btns" v-if="listSize == 0">
				<u-button type="success" @click="navigateBack" :loading='loading' :disabled='loading'>返回</u-button>
			</view>
		</view>
	</view>

</template>

<script setup lang="ts">
		/*初始化*/
	import {
		onLoad,
		onShow,
		onReachBottom
	} from '@dcloudio/uni-app'
	import {
		ref,
		getCurrentInstance
	} from 'vue'
	/*引入API*/
	import * as addBasicItemApi from "@/api/mes/workScheduling/addBasicItem/index.ts"
	import * as dictApi from "@/api/mes/dict/dictApi.ts"
	const { proxy } = getCurrentInstance()
	const loading = ref(false)
	const showProduct = ref(false)
	const paramData = ref()
	const typeList = ref([])
	const list = ref([])
	const listSize = ref(1)
	const productList = ref([{label:'产品1',value: '111',name:'产品'},{label:'产品2',value: '222',name:'产品'}])
	
	const queryForm = ref({
		planNoDay:'',
		productCode:'',
		workroomCode:'',
		workBillNo:'',
		lineCode:'',
		processrouteCode:'',
		batchCode:'',
		processCode:'',
		planCount:''
	})
	const saveData = ref({
		planDayCode:'',
		workBillNo:'',
		batchCode:'',
		workstationCode:'',
		productCode:'',
		processCode:'',
		requestType:'2',
		list:[]
	})
	
	//删除物料
	function deleteItem(val) {
	  const index = list.value.findIndex(item => item.repMaterialCode === val.repMaterialCode);
	  if (index !== -1) {
	    list.value.splice(index, 1);
		listSize.value -= 1
		console.log(listSize.value)
	  }
	}
	
	function calculatePass(item,planCount){
		const count = item.repMaterialCounts * planCount
		if(item.srcMaterialCounts > count){
			proxy.$modal.showToast('数量不能超过最大物料数')
			setTimeout(() => {
				item.srcMaterialCounts=count
				proxy.$modal.closeLoading()
			}, 500)
		}
		if(item.srcMaterialCounts <= 0 ){
			proxy.$modal.showToast('数量必须大于0')
			setTimeout(() => {
				item.srcMaterialCounts=1
				proxy.$modal.closeLoading()
			}, 500)
		}
	}
	
	// 提交事件
	function handleSubmit(){
		saveData.value.planDayCode = queryForm.value.planNoDay
		saveData.value.workBillNo = queryForm.value.workBillNo
		saveData.value.batchCode = queryForm.value.batchCode
		saveData.value.workstationCode = queryForm.value.workroomCode
		saveData.value.productCode = queryForm.value.productCode
		saveData.value.processCode = queryForm.value.processCode
		saveData.value.list = list.value
		// 提交接口
		proxy.$modal.confirm('确定提交处理吗').then(() => {
			proxy.$modal.loading('加载中')
			loading.value = true
			addBasicItemApi.addBasicItem(saveData.value).then((res) => {
				proxy.$modal.closeLoading()
				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()
				}
			}).catch(() => {
				proxy.$modal.closeLoading()
				loading.value = false
			})
		})
	}
	function navigateBack() {
		proxy.$tab.navigateBack()
	}
	
	// 获取类型字典函数
	async function getDictInfo(){
		await dictApi.getDict("uom").then((res) => {
			proxy.$modal.closeLoading()
			if (res) {
				typeList.value = res 
			} else {
			}
		}).catch(() => {
		})
	}
	async function getBasicItem() {
		proxy.$modal.loading('加载中')
		await addBasicItemApi.getByOrder(queryForm.value).then((res) => {
			proxy.$modal.closeLoading()
			if (res.data.length > 0) {
				list.value = list.value.concat(res.data);
				listSize.value = res.data.length
			} else {
			}
		}).catch(() => {
			proxy.$modal.closeLoading()
		})
	}

	/*通用方法*/
	onLoad((option) => {
		if (option.obj) {
		    paramData.value = JSON.parse(decodeURIComponent(option.obj)); // 将字符串转换为对象并存入paramData
			queryForm.value.planNoDay = paramData.value.planMasterCode
			queryForm.value.productCode = paramData.value.productCode
			queryForm.value.workroomCode = paramData.value.workroomCode
			queryForm.value.lineCode = paramData.value.lineCode
			queryForm.value.processrouteCode = paramData.value.prouteCode
			queryForm.value.batchCode = paramData.value.batchCode
			queryForm.value.processCode = paramData.value.workingNode
			queryForm.value.workBillNo = paramData.value.schedulingCode
			queryForm.value.planCount = paramData.value.planCount
		}
	})
	onShow(() => {
		getDictInfo()
		getBasicItem()
	})
	onReachBottom(() => {

	})
	
</script>

<style lang="scss" scoped>
	// .footer {
	//   position: fixed;
	//   bottom: 0;
	//   left: 0;
	//   width: 100%;
	//   background-color: #fff; /* 设置 footer 的背景颜色 */
	//   /* 其他样式属性，例如 padding、border 等 */
	// }
	.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: 160rpx;;
					}
					&: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>