<template>
	<view>
		<view class="serchs">
			<view class="ipt">
				<input type="text" placeholder="物料名称/物料编码/物料简拼" v-model="form.wlParamInfo" />
				<image src="/static/index/procurement/x.png" mode="aspectFill" v-if="form.wlParamInfo"
					@click="search(0)"></image>
			</view>
			<view @click="search" class="scr">
				检索
			</view>
			<view class="shx">
				<image src="/static/index/procurement/sx.png" mode="aspectFill"></image>
				筛选
			</view>
		</view>
		<view class="kcyj">
			<view class="kjlt">
				库存预警
			</view>
			<scroll-view scroll-x="true" class="kcscrol guodu" :style="qb?'width:46.4vw':''">
				<view class="yjbox" v-for="(v,i) in 15" :key="i">
					<image :src="'/static/index/procurement/k.png'" mode="aspectFill"></image>
					<view>纸尿裤-拉拉裤纸拉拉裤纸拉拉裤纸拉拉裤纸</view>
					<text class="ytj">已添加</text>
				</view>
			</scroll-view>
			<view class="kjlt more guodu" :style="qb?'width:11.2vw':''">
				<view @click="qb=!qb" class="righ0">
					{{!qb?'更多':'收起'}}
					<image src="/static/index/procurement/zjt.png" mode="aspectFill"></image>
				</view>
				<view class="guodu" :class="qb?'':'righ0'">查看全部</view>
				<view class="guodu" :class="qb?'':'righ0'">添加全部</view>

			</view>
			<view class="kaprght">
				<view class="ksfw">
					<view class="lcar">
						<image :src="'/static/index/procurement/k.png'" mode="aspectFill"></image>
						<view class="nm">纸尿裤-拉拉裤纸拉拉裤纸拉拉裤纸拉拉裤纸</view>
						<view class="jx">
							<image src="/static/index/procurement/l0.png" mode="aspectFill"></image>
							<text>待进行</text>
						</view>
					</view>
					<view class="rcar">

						<view class="tm">10:00 - 10:10</view>
						<view class="wul">物料采购</view>
						<view class="zhxng">
							<view>
								<image src="/static/index/procurement/xz.png" mode="aspectFill"></image>
								<text>协助执行</text>
							</view>
							<view>
								<image src="/static/index/procurement/ap.png" mode="aspectFill"></image>
								<text>王金福 / 李贵田</text>
							</view>
						</view>
						<view class="shjs">
							<view class="iacts">开始服务</view>
							<view>服务结束</view>
						</view>
					</view>

				</view>
			</view>

		</view>
		<view class="listcard">
			<scroll-view scroll-y="true" class="crdcroll"  scroll-with-animation :scroll-top="scrolltop"  
					@scrolltolower="scrolltolower">
					<view  class="fler">
						<view class="carditem" v-for="(v,index) in InvoicingList" :key="index" >
								<view class="speitem guodu" :class="v.zk?'spleft':''">
									<image :src="'/static/index/procurement/k.png'" mode="aspectFill"></image>
									<view class="cardp">
										<view v-if="v.categoryId_dictText">{{v.categoryId_dictText}}</view>
										<view v-if="v.typeId_dictText">{{v.typeId_dictText}}</view>
										<view v-if="v.medicationName">{{v.medicationName}}</view>
									</view>
								</view>
								<view class="msitem guodu" :style="v.zk?'margin-left:2.5vw':''">
									<view>
										<view>{{v.materialName}}</view><text>{{v.materialNo}}</text>
									</view>
									<view>
										<text> 规格型号: {{v.specificationModel}}</text>
									</view>
									<view>
										<text v-if="v.multiUnitType=='1'">采购单价: ￥{{v.oneUnitPrice}} </text>    
										<text v-if="v.multiUnitType=='1'">采购单位: {{ v.oneUnit}}</text>
										<text v-if="v.multiUnitType=='2'">采购单价: ￥{{v.twoUnitPrice}} </text>   
										<text v-if="v.multiUnitType=='2'">采购单位: {{ v.twoUnit}}</text> 
										<text v-if="v.multiUnitType=='3'">采购单价: ￥{{v.referenceUnitPrice}} </text>   
										<text v-if="v.multiUnitType=='3'">采购单位: {{ v.materialUnits}}</text> 
									</view>
									<view>
										<view>
											<text>{{v.kcsl}}</text>
											<text>库存数量</text>
										</view>
										<view>
											<text >{{v.upperLimit}}</text>
											<text>物料上限 ↑</text>
										</view>
										<view>
											<text >{{v.lowerLimit}}</text>
											<text>↓ 物料下限</text>
										</view>
									</view>
								</view>
								<view class="add" @click="clkzk(v,index)">
									<image class="guodu" :src="!v.zk?'/static/index/procurement/+.png':'/static/index/procurement/-.png'" mode="aspectFill"></image>
								</view>
								<view class="zkadd guodu" :class="v.zk?'':'zkf'">
									<view>出入库</view>
									<view @click="addcar(v,i)">加购物车</view>
								</view>
						</view>
					</view>
					<view style="height:3vw;width: 100%;display: flex;align-items: center;justify-content: center;">
						<u-loadmore :status="status" :loadText="{nomore:'暂无更多数据'}" v-if="InvoicingList.length>6"/>
					</view>
			</scroll-view>
		</view>
		<view class="fiedright">
			<view>
				<image src="/static/index/procurement/gc.png" mode="aspectFill"></image>
				<text v-if="carnum>0">{{carnum>99?'99':carnum}}</text>
			</view>
			<view>
				<image src="/static/index/procurement/cg.png" mode="aspectFill"></image>
			</view>
		</view>
		<view class="mengban" v-if="addflag" @click="addflag = false"></view>
		<calculator :show="addflag" @jjnum="jjnum" :caigouobj="caigouobj" :translateNumber="caigouobj?.Limitnum" :doOnce="caigouobj?.Limitnum" @colse="colse" @right="right"></calculator>
	</view>
</template>

<script setup lang="ts">
	import { ref, onMounted, reactive, onBeforeUnmount, computed, nextTick, defineProps ,defineComponent} from 'vue';
	import { queryInvoicingList,updateWarehouserEnabled, getMaterialTreeData, queryNuInfoByNuId, updateKfstatus,queryWlInfoByWlId,addShoppingCartList,queryShoppingCartList } from './api/lunpan.js'
	import { onShow, onLoad, onHide, onPageScroll } from "@dcloudio/uni-app"
	import calculator from './components/calculator.vue'
	const qb = ref(false)
	const addflag = ref(false)
	
	const InvoicingList = ref([]); 
	const caigouobj = ref({})
	const status = ref('loading')
	const scrolltop = ref(0)
	const serverUrl = ref('')
	const form = reactive({
		nuId: '',
		pageNo: 1,
		pageSize: 9,
		categoryId: '',
		typeId: '',
		medicationId: '',
		wlParamInfo: '',
		isWaring: 0
	})
	onLoad(() => {
		form.nuId = uni.getStorageSync('nuId');
		serverUrl.value = uni.getStorageSync('serverUrl') + '/sys/common/static/';
		queryInvo();
	})
	onShow(()=>{
		shoppcar()
	})
	const carnum = ref(0)
	const shoppcar=()=>{
		let c  = {
		nuId: form.nuId,
		pageNo: 1,
		pageSize: 10 
	}
		queryShoppingCartList(c).then(resr=>{
			console.log(resr.result.total)
			carnum.value = resr.result.total
		})
	}
	const setout = ref(true)
	let times = null;
	const scrolltolower = () => {
		if (setout.value==false) { return }
		status.value = 'loading'
		setout.value = false;
		form.pageNo++;
		queryInvo()
		// times = setTimeout(() => {	
		// 	setout.value = true
		// }, 1000)
	}
	const queryInvo = () => {
		queryInvoicingList(form).then(res => {
			res.result.records.forEach(item=>{
				item.zk = false;
			})
			InvoicingList.value.push(...res.result.records);
			console.log(res.result.records)
			status.value = (res.result.total == InvoicingList.value.length ? 'nomore' : 'loadmore')
			setout.value = (res.result.total == InvoicingList.value.length ? false : true)
		})
	}
	const clkzk =(v:any,i:number)=>{
		caigouobj.value = v;
		caigouobj.value.Limitnum =  Number(caigouobj.value?.upperLimit)-Number(caigouobj.value.kcsl);
		InvoicingList.value.forEach((item,k)=>{
			if(k==i){
				item.zk = !item.zk;
			}else{
				item.zk = false;
			}
		})
	}
	const addcar = (v:any,i:number)=>{
		addflag.value = true;
	}
	const colse = () => {
		addflag.value = !addflag.value
	}
	const jjnum = (e:number) =>{
		let n = Number(caigouobj.value.upperLimit)-Number(caigouobj.value.kcsl)
		if(n==caigouobj.value.Limitnum&&e==1){return}
		if(caigouobj.value.Limitnum==1&&e==-1){return}
		caigouobj.value.Limitnum+=e;
	}
	const right = (n:number,nm:any) => {
		let d = Number(caigouobj.value.upperLimit)-Number(caigouobj.value.kcsl)
		
		if(d>=n){
			caigouobj.value.Limitnum = n;
		}else{
			caigouobj.value.Limitnum = d;
		}
		if(n<1){
			caigouobj.value.Limitnum = 1;
		}
		connfig(nm)
	}
	const timers = ref(true)
	const connfig = (nm:any) =>{
		if(timers.value==false){return}
		if(caigouobj.value.izEnabled=='1'){
			uni.showToast({
				icon:'none',
				title:'该物料已关闭，无法采购~'
			})
			return
		}
		timers.value=false;
		let dt = {
			nuId:form.nuId,
			purchaseQuantity:caigouobj.value.Limitnum,
			suppliersId:nm.id,
			suppliersName:nm.name,
			wlId:caigouobj.value.wlId,
			kcsl:caigouobj.value.kcsl
		}
		console.log(dt)
		addShoppingCartList([dt]).then(res=>{
			uni.showToast({
				icon:res.success?'success':'none',
				title:res.message
			})
			setTimeout(()=>{
				timers.value=true;
				if(res.success){
					addflag.value = false;
					shoppcar()
				}
			},700)
		})
	}
</script>

<style scoped lang="less">
	.fiedright{
		width: 5vw;
		height: 12vw;
		position: fixed;
		z-index: 200;
		right: 2vw;
		bottom: 6vw;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		view{
			width: 5vw;
			height: 5vw;
			background: rgba(255, 255, 255, 0.86);
			border-radius: 50%;
			border: 1px solid #D9DADC;
			display: flex;
			justify-content: center;
			align-items: center;
			position: relative;
			text{
				min-width: 1.8vw;
				height: 1.8vw;
				border-radius: 0.8vw;
				border: 2px solid #E81D1D;
				display: flex;
				align-items: center;
				justify-content: center;
				padding: 0 0.5vw;
				font-weight: 400;
				font-size: 1.3vw;
				color: #E81D1D;
				position: absolute;
				top: -0.8vw;
				right:-0.8vw;
			}
			image{
				width: 3.3vw;
				height: 3.3vw;
			}
		}
	}
 
	.listcard {
		width: 100%;
		height: 73vh;
		margin-top: 1vw;

		.carditem {
			width: 47vw;
			height: 14.5vw;
			background: rgba(255, 255, 255, 0.5);
			border-radius: 1.6vw;
			position: relative;
			margin-top: 1.1vw;
			display: flex;
			overflow: hidden;
			justify-content: flex-start;
			.zkf{
				right:-10vw !important;
			}
			.zkadd{
				width: 10vw;
				height: 100%;
				display: flex;
				justify-content: flex-end;
				position: absolute;
				top: 0;
				right: 0;
				view{
					width: 5vw;
					height: 100%;
					font-weight: 400;
					font-size: 1.3vw;
					display: flex;
					justify-content: center;
					align-items: center;
					flex-wrap: wrap;
					&:nth-child(2){
						background: #1083F8;
						color: #fff;
						padding: 1.1vw;
					}
					&:nth-child(1){
						background: #C2C4C9;
						color: #555555;
					}
				}
			}
			.add{
				width: 6vw;
				height: 100%;
				display: flex;
				justify-content: center;
				align-items: center;
				image{
					width: 3.2vw;
					height: 3.2vw;
				}
			}
			.msitem{
				width: 25vw;
				height: 100%;
				padding: 1vw;
				>view{
					margin-top: 0.5vw;
					&:nth-child(4){
						width: 100%;
						height: 3.4vw;
						display: flex;
						justify-content: space-between;
						>view{
							width: 33.3%;
							height: 100%;
							display: flex;
							flex-direction: column;
							position: relative;
							align-items: center;
							&:nth-child(1){
								width: 30%;
								text-align: left;
							}
							&:nth-child(2){
								width: 40%;
								text-align: center;
							}
							&:nth-child(3){
								width: 30%;
								text-align: right;
							}
							&:nth-child(2)::after{
								content: '';
								position: absolute;
								right: 0;
								width: 100%;
								height: 2.2vw;
								border-left: 1px solid  #C9C9C9;
								border-right: 1px solid  #C9C9C9;
								top: 0.6vw;
							}
							text{
								&:nth-child(1){
									font-weight: bold;
									font-size: 1.7vw;
									color: #555555;
								}
								&:nth-child(2){
									font-weight: 400;
									font-size: 1.1vw;
									color: #999999;
								}
							}
						}
					}
					&:nth-child(2),&:nth-child(3){
						display: flex;
						justify-content: space-between;
						text{
							font-weight: 400;
							font-size: 1.4vw;
							color: #777777;
							margin-top: 0.35vw;
						}
					}
					&:nth-child(1){
						display: flex;
						justify-content: space-between;
						align-items: center;
						height: 2vw;
						>view{
							width: 19vw;
							height: 2vw;
							font-weight: bold;
							font-size: 1.6vw;
							color: #222222;
							text-overflow:ellipsis;
							overflow: hidden;
							white-space: nowrap;
						}
						text{
							width: 6vw;
							font-weight: 300;
							font-size: 1.4vw;
							color: #222222;
						}
					}
				}
			}
			.spleft{
				margin-left: -15vw !important;
			}
			.speitem{
				width: 15vw;
				height: 100%;
				display: flex;
				flex-direction: column;
				.cardp{
					width: 12.5vw;
					height: 4vw;
					display: flex;
					flex-wrap: wrap;
					margin: 0 auto;
					align-items: center;
					view{
						width: 5.5vw;
						height: 1.8vw;
						border-radius: 0.9vw;
						border: 1px solid #D2D2D2;
						margin: 0.5vw 0 0 0.5vw;
						display: flex;
						justify-content: center;
						align-items: center;
						font-weight: 400;
						font-size: 1vw;
						color: #555555;
					}
				}
				>image{
					width: 9vw;
					height: 8vw;
					margin: 1vw auto 0;
				}
			}
		}

		.crdcroll {
			width: 100%;
			height: 100%;

			.fler {
				display: grid;
				grid-template-columns: 1fr 1fr;
			}
		}
	}

	.kcyj {
		width: 100%;
		height: 10vw;
		display: flex;
		margin-top: 1vw;

		.kaprght {
			width: 32vw;
			height: 10vw;
			margin-left: 0.6vw;
			position: relative;

			.ksfw {
				width: 100%;
				height: 14.5vw;
				background: rgba(255, 255, 255, 0.6);
				border-radius: 1vw;
				border: 1px solid #D9DADC;
				padding: 0 1vw;
				position: absolute;
				bottom: 0;
				left: 0;
				overflow: hidden;
				display: flex;

				.lcar {
					width: 12.7vw;
					position: relative;

					image {
						width: 10vw;
						height: 10vw;
						margin: 1vw auto 0;
						display: block;
					}

					.nm {
						width: 100%;
						height: 1.4vw;
						font-weight: 400;
						font-size: 1.3vw;
						color: #212327;
						white-space: nowrap;
						text-overflow: ellipsis;
						overflow: hidden;
						padding: 0 1vw;
					}

					>.jx {
						width: 4.6vw;
						height: 1.9vw;
						position: absolute;
						top: 0.6vw;
						right: 1vw;
						font-weight: 400;
						font-size: 1.1vw;
						display: flex;
						justify-content: center;
						align-items: center;
						line-height: 1.9vw;

						>image {
							position: absolute;
							width: 100%;
							height: 100%;
							left: 0;
							top: 0;
							z-index: 1;
						}

						>text {
							position: relative;
							z-index: 2;
							color: #FFFFFF;
							margin-top: 2vw;
						}
					}
				}

				.rcar {
					width: 19vw;
				}

				.shjs {
					width: 100%;
					margin-top: 0.8vw;
					display: flex;
					justify-content: space-around;

					view {
						width: 7.2vw;
						height: 2.5vw;
						background: #E4E9F4;
						border-radius: 1.25vw;
						font-weight: 400;
						font-size: 1.1vw;
						color: #333333;
						display: flex;
						align-items: center;
						justify-content: center;
						border: 1px solid #E4E9F4;
					}

					.iacts {
						background: rgba(168, 212, 255, .75);
						border: 1px solid #A8D4FF;
						color: #249BFA;
					}

				}

				.zhxng {
					width: 100%;
					height: 2.8vw;
					margin-top: 0.4vw;
					display: flex;
					padding: 1.1vw 0 1.1vw 0;
					border-bottom: 2px solid #E5E5E5;

					view {
						display: flex;
						align-items: center;
						white-space: nowrap;

						image {
							width: 1.6vw;
							height: 1.6vw;
							margin-right: 0.3vw;
						}

						text {
							font-weight: 400;
							font-size: 1vw;
							color: #555555;
							margin-right: 0.5vw;
							white-space: nowrap;
						}
					}
				}

				.wul {
					width: 100%;
					height: 1.5vw;
					font-weight: bold;
					font-size: 1.5vw;
					color: #555555;
					margin: 1vw auto 0;
				}

				.tm {
					width: 100%;
					height: 1.8vw;
					font-size: 2.2vw;
					font-weight: bolder;
					color: #555555;
					margin-top: 2vw;
				}

			}

		}

		.kjlt {
			width: 3.2vw;
			height: 10vw;
			background: rgba(255, 255, 255, 0.5);
			border-radius: 1.1vw;
			writing-mode: vertical-rl;
			align-items: center;
			display: flex;
			justify-content: center;
			letter-spacing: 3px;
			font-size: 1.4vw;
		}

		.more {
			margin-left: 0.6vw;
			position: relative;
			background: RGBA(239, 240, 244, 1);

			.righ0 {
				right: 0 !important;
			}

			view {
				writing-mode: vertical-rl;
				align-items: center;
				display: flex;
				justify-content: center;
				letter-spacing: 3px;
				width: 3.2vw;
				height: 10vw;
				position: absolute;
				top: 0;
				border-radius: 1.1vw;
				font-size: 1.4vw;

				&:nth-child(1) {
					letter-spacing: 6px;
					padding-top: 2vw;
					z-index: 5;
					background: rgba(255, 255, 255, 1);

					image {
						width: 1.6vw;
						height: 1.6vw;
						margin-top: 10px;
					}
				}

				&:nth-child(2) {
					background: rgba(85, 166, 249, 1);
					color: #FFFFFF;
					right: 4.5vw;
					z-index: 3;
				}

				&:nth-child(3) {
					color: #888888;
					background: #fff;
					right: 8.5vw;
					z-index: 2;
				}
			}

		}

		.zkw {
			width: 46.4vw !important;
		}

		.kcscrol {
			width: 54.4vw;
			height: 10vw;
			white-space: nowrap;
			margin-left: 0.6vw;

			.yjbox {
				display: inline-block;
				width: 14vw;
				height: 10vw;
				background: rgba(255, 255, 255, 0.5);
				border-radius: 1.1vw;
				margin-right: 0.6vw;
				position: relative;

				.ytj {
					width: 4.5vw;
					height: 1.8vw;
					background: #E3EEF7;
					border-radius: 0.9vw;
					border: 1px solid rgba(3, 133, 250, 0.34);
					font-weight: 400;
					font-size: 1.1vw;
					color: #1083F8;
					text-align: center;
					line-height: 1.75vw;
					position: absolute;
					top: 0.8vw;
					right: 0.8vw;
				}

				image {
					width: 7vw;
					height: 7vw;
					margin: 1vw auto 0;
					display: block;
				}

				>view {
					width: 100%;
					height: 1.4vw;
					font-weight: 400;
					font-size: 1.3vw;
					color: #212327;
					white-space: nowrap;
					text-overflow: ellipsis;
					overflow: hidden;
					padding: 0 1vw;
				}
			}
		}
	}

	.serchs {
		width: 46.5vw;
		height: 3.3vw;
		border-radius: 1.6vw;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.ipt {
			display: flex;
			align-items: center;

			image {
				width: 2.2vw;
				height: 2.2vw;
				margin-left: 0.5vw;
			}

			input {
				width: 27vw;
				height: 3.3vw;
				font-size: 1.1vw;
				padding-left: 1.6vw;
				background: #E2E4E9;
				border-radius: 1.65vw;
			}
		}

		.scr {
			border-radius: 1.65vw;
			font-weight: 400;
			font-size: 1.5vw;
			color: #1083F8;
			display: flex;
			justify-content: center;
			align-items: center;
			width: 8vw;
			height: 3.3vw;
			background: linear-gradient(-45deg, rgba(223, 244, 252, 1), rgba(204, 228, 249, 0.43));
			border: 1px solid #1083F8;
		}

		.shx {
			width: 8.2vw;
			height: 3.3vw;
			background: #FFFFFF;
			border-radius: 1.65vw;
			border: 1px solid #D9DADC;
			font-weight: 400;
			font-size: 1.4vw;
			color: #222;
			display: flex;
			justify-content: center;
			align-items: center;
			box-shadow: 0.5rpx 5rpx 5rpx #dadee1;

			image {
				width: 1.5vw;
				height: 1.3vw;
				margin: 0 0.5vw;
			}
		}
	}

	.guodu {
		transition: .4s;
		-webkit-transform-style: preserve-3d;
		-webkit-overflow-scrolling: touch;
	}
	.mengban {
		width: 100vw;
		height: 100vh;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 100;
		background: RGBA(239, 240, 244, 0.55);
	}
</style>
<style>
	page {
		background: RGBA(239, 240, 244, 1);
		padding: 2vw;
		box-sizing: border-box;
	}
</style>