<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" @click="ification=true">
				<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':''"  
			scroll-with-animation :scroll-top="scrolltop"  @scrolltolower="scrolltolower(1)">
				<view class="yjbox" v-for="(v,i) in Material" :key="i">
					<image :src="v.materialImg?serverUrl+v.materialImg:'/static/index/procurement/k.png'" mode="aspectFill"></image>
					<view>{{v.materialName}}</view>
					<text class="ytj" v-if="v.isAdd==1">已添加</text>
				</view>
			</scroll-view>
			<view class="kjlt more guodu" :style="qb?'width:11.2vw':''">
				<view @click="qb=!qb" class="righ0">
					{{!qb?'更多':'收起'}}
					<image class="guodu" :style="qb?'transform: rotate(180deg);':''" src="/static/index/procurement/zjt.png" mode="aspectFill"></image>
				</view>
				<view class="guodu" :class="qb?'':'righ0'" @click="Invenshow = true">查看全部</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">
			<carditem :InvoicingList="InvoicingList" @addcar="addcard" @addcartory="addcartory" @scrolltolower="scrolltolower" :status="status"></carditem>
		</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>
		<!-- num -->
		<view class="mengban" style="z-index: 119;" v-if="addflag" @click="qk"></view>
		<calculator :show="addflag" @jjnum="jjnum" :caigouobj="caigouobj" 
		:translateNumber="caigouobj?.Limitnum" :doOnce="caigouobj?.Limitnum
		" @colse="qk" @right="right"></calculator>
		<!-- 筛选 -->
		<view class="mengban" v-if="ification" @click="qk"></view>
		<classification @confirm="confirm" :show="ification"></classification>
		<!-- more -->
		<view class="mengban" v-if="Invenshow" @click="Invenshow = false"></view>
		<Inventory @addcartory="addcartory" :show="Invenshow" :InvoicingList="Material" 
		 :status="statustory" @tolower="scrolltolower(1)"></Inventory>
	</view>
</template>

<script setup lang="ts">
	import { ref, onMounted, reactive, onBeforeUnmount, computed, nextTick, defineProps ,defineComponent} from 'vue';
	import { queryInvoicingList,queryWlInfoByWlId,addShoppingCartList,queryShoppingCartList } from './api/lunpan.js'
	import { onShow, onLoad, onHide, onPageScroll } from "@dcloudio/uni-app"
	import calculator from './components/calculator.vue'
	import classification from './components/classification.vue'
	import Inventory from './components/Inventory.vue'
	import carditem from './components/carditem.vue'
	const qb = ref(false)
	const addflag = ref(false)
	const ification = ref(false)
	const Invenshow = ref(false)
	
	
	const InvoicingList = ref([]); 
	const caigouobj = ref({})
	const status = ref('loading')
	const statustory = ref('loading')
	
	const scrolltop = ref(0)
	const serverUrl = ref('')
	const form = reactive({
		nuId: uni.getStorageSync('nuId'),
		pageNo: 1,
		pageSize: 10,
		categoryId: '',
		typeId: '',
		medicationId: '',
		wlParamInfo: '',
		isWaring: 0
	})
	const warn = reactive({
		nuId: uni.getStorageSync('nuId'),
		pageNo: 1,
		pageSize: 10,
		categoryId: '',
		typeId: '',
		medicationId: '',
		wlParamInfo: '',
		isWaring: 1
	})
	onLoad(() => {
		serverUrl.value = uni.getStorageSync('serverUrl') + '/sys/common/static/';
		queryInvo();
	})
	onShow(()=>{
		shoppcar();
		getWaringMaterial()
	})
	const Material = ref([])
	const getWaringMaterial=()=>{
		queryInvoicingList(warn).then(res => {
			console.log(res.result)
			res.result.records.forEach(item=>{
				item.zk = false;
				item.scrollleft = 0;
			})
			Material.value.push(...res.result.records);
			statustory.value = (res.result.total == Material.value.length ? 'nomore' : 'loadmore')
			setout1.value = (res.result.total == Material.value.length ? false : true)
		})
	}
	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 setout1 = ref(true)
	const setout2 = ref(true)
	let times = null;
	const scrolltolower = (t:number) => {
		if(t==1){
			if (setout1.value==false) { return }
			setout1.value = false;
			statustory.value = 'loading'
			warn.pageNo++;
			getWaringMaterial()
		}else{
			if (setout2.value==false) { return }
			status.value = 'loading'
			setout2.value = false;
			form.pageNo++;
			queryInvo()
		}
		
		// times = setTimeout(() => {	
		// 	setout.value = true
		// }, 1000)
	}
	const qk = () =>{
		ification.value = false;
		addflag.value = false;
	}
	const confirm =(e:any)=>{
		form.categoryId = e.categoryId;
		form.typeId = e.typeId;
		form.medicationId = e.medicationId;
		form.pageNo=1;
		InvoicingList.value = [];
		queryInvo();
		qk()
	}
	const search = (x : number) => {
		if (x === 0) { form.wlParamInfo = '' }
		form.pageNo = 1;
		InvoicingList.value = [];
		queryInvo();
	}
	const queryInvo = () => {
		queryInvoicingList(form).then(res => {
			res.result.records.forEach(item=>{
				item.zk = false;
				item.scrollleft = 0;
			})
			InvoicingList.value.push(...res.result.records);
			status.value = (res.result.total == InvoicingList.value.length ? 'nomore' : 'loadmore')
			setout2.value = (res.result.total == InvoicingList.value.length ? false : true)
		})
	}
	const addcartory =(e:any)=>{
		console.log(e)
		 caigouobj.value = e;
		 addflag.value = true;
	}
	const addcar = (v:any,i:number)=>{
		addflag.value = true;
	}
	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 addcard=(n:any,nm:any)=>{
		caigouobj.value = n;
		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
		}
		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();
					if(caigouobj.value.yj){
						Material.value[caigouobj.value.index].isAdd = 1
					}else{
						InvoicingList.value[caigouobj.value.index].isAdd = 1
					}
				}
			},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: 70.3vh;
		margin-top: 1vw;
		
		.crdcroll {
			width: 100%;
			height: 100%;
			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: 0.5vw auto 0.5vw;
					display: block;
					border-radius: 1vw;
				}

				>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;
					text-align: center;
				}
			}
		}
	}

	.serchs {
		width: 46.5vw;
		height: 3.3vw;
		border-radius: 1.6vw;
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 0.7vw;
		.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>