<template>
	<view>
		<!-- 购物车 -->
		<scroll-view scroll-y="true" class="crdcroll" scroll-with-animation @scrolltolower="scrolltolower()"
			enable-back-to-top>
			<view class="box">
				<view class="fler" v-for="(v,index) in InvoicingList" :key="index" @click="v.flag=!v.flag">
					<view class="jiao guodu">
						<image :src="v.flag?'/static/index/procurement/lxz.png':'/static/index/procurement/jxz.png'"
							mode="aspectFill" class="jao guodu"></image>
						<image src="/static/index/procurement/dh.png" mode="aspectFill" class="dui"></image>
					</view>
					<scroll-view class="carditem guodu " @touchend="handleTouchEnd($event,v,index)"
						@touchstart="touchstart($event,v,index)" scroll-with-animation scroll-x="true"
						:scroll-left="v.scrollleft" @scroll="scroll" :class="v.flag?'actve':''">
						<view class="speitem guodu">
							<view class="imghs">
								<image style="border-radius: 1vw;"
									:src="v.materialImg?serverUrl+v.materialImg:'/static/index/procurement/k.png'"
									mode="aspectFill">
								</image>
							</view>

							<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.medicationId_dictText">{{v.medicationId_dictText}}</view>
							</view>
						</view>
						<view class="msitem guodu">
							<view>
								<view>{{v.wlName}}</view>
							</view>
							<!-- <text>{{v.wlMaterialNo}}</text> -->
							<view>
								<text style="white-space: nowrap;"> 规格编号: {{v.wlMaterialNo}}</text>
							</view>
							<view>
								<text style="white-space: nowrap;"> 规格型号: {{v.wlSpecificationModel}}</text>
							</view>
							<view>
								<text v-if="v.wlUnits == v.materialUnits">采购单价:
									￥{{Number(v.unitPrice).toFixed(2) }}</text>
								<text v-if="v.wlUnits == v.twoUnit">采购单价: ￥{{Number(v.twoUnitPrice).toFixed(2) }}</text>
								<text v-if="v.wlUnits == v.oneUnit">采购单价: ￥{{Number(v.oneUnitPrice).toFixed(2) }}</text>

							</view>
							<view>
								<text>采购单位: {{v.wlUnits}}</text>
							</view>
							<view style="margin-bottom: 0;">
								<!-- <text>供应商: {{v.suppliersName}}</text> -->
							</view>
							<view>

								<view class="three-one">
									<text style="margin-left: 42rpx;">{{v.kcsl?v.kcsl:'0'}}</text>
									<text>库存数量</text>
									<view class="three-shu">

									</view>
								</view>
								<view class="three-one">
									<text style="text-align: center;">{{v.wlUpperLimit}}</text>
									<text>物料上限 ↑</text>
									<view class="three-shu">

									</view>
								</view>
								<view class="three-one">
									<text style="text-align: end;margin-right: 20rpx;">{{v.wlLowerLimit}}</text>
									<text>↓ 物料下限</text>
								</view>
							</view>
							<view class="gongyingshang">
								供应商: {{v.suppliersName}}
							</view>
							<!-- 供应商: {{v.suppliersName}} -->
						</view>
						<view class="add" @click.stop="clkzk(v,index)">
							<image class="guodu" :style="v.zk?'transform: rotate(180deg);':''"
								:src="'/static/index/procurement/l.png'" mode="aspectFill"></image>
						</view>
						<view class="right-top">
							<view style="font-size: 1.7vw;font-weight: 600;">
								{{v.purchaseQuantity}}
							</view>
							<view style="font-size: 1.3vw;">
								采购数量
							</view>
						</view>
						<view class="zkadd guodu" :class="v.isAdd==1?'zkf':''">
							<view @click.stop="addcar(v,index)" @touchend.stop>编辑</view>
							<view @click.stop="del(v,index)" v-if="v.isAdd!=1" @touchend.stop>
								<view>
									删除
								</view>
							</view>
						</view>
					</scroll-view>
				</view>
			</view>
			<view style="height:3vw;width: 100%;display: flex;align-items: center;justify-content: center;"
				v-if="InvoicingList.length>6">
				<u-loadmore :status="status" :loadText="{nomore:'暂无更多数据'}" />
			</view>
		</scroll-view>
	</view>
</template>

<script setup lang="ts">
	import { ref, onMounted, reactive, onBeforeUnmount, computed, nextTick, defineComponent } from 'vue';
	const props = defineProps({
		InvoicingList: {
			type: Array,
			required: true
		},
		status: {
			type: String
		},
		show: {
			type: Boolean,
			default: false,
			required: true,
		},
	})
	const emit = defineEmits(['addcartory', 'scrolltolower', 'delindex'])
	const serverUrl = ref('')
	onMounted(() => {
		serverUrl.value = uni.getStorageSync('serverUrl') + '/sys/common/static/';
	})
	const addcar = (v : any, i : number) => {
		caigouobj.value = v;
		caigouobj.value.index = i;
		emit('addcartory', caigouobj.value)
	}
	const del = (v, i) => {
		emit('delindex', v, i)
	}
	const caigouobj = ref({})
	const clkzk = (v : any, i : number) => {

		if (v.zk == true) {
			v.zk = false;
			v.scrollleft = 0
		} else {
			v.zk = true;
			v.scrollleft = 150;
		}
		solleft(v, i)

	}
	const pageX = ref(0);
	const pageY = ref(0);
	const touchstart = (event : any, v : any, i : number) => {
		pageX.value = event.changedTouches[0].pageX;
		pageY.value = event.changedTouches[0].pageY;
		solleft(v, i)
	}
	const handleTouchEnd = (event : any, v : any, i : number) => {
		let y = event.changedTouches[0].pageY;
		let x = event.changedTouches[0].pageX;
		let absx = Math.abs(x - pageX.value)
		let absy = Math.abs(y - pageY.value)
		if (absy > 30 && absx + 10 < absy && absx < 70) {
			if (v.zk == false) {
				v.scrollleft = 1
				setTimeout(() => {
					v.scrollleft = 0
				}, 20)
			}
			return
		}
		if (absx < 10 && absy < 10) {
			if (v.zk == false && absx > 2 && absy > 2) {
				v.scrollleft = 0;
				return
			}
			return
		}
		caigouobj.value = v;
		caigouobj.value.Limitnum = Number(caigouobj.value?.upperLimit) - Number(caigouobj.value.kcsl);
		caigouobj.value.index = i;
		caigouobj.value.yj = false;
		v.zk = x > pageX.value ? false : true;
		v.scrollleft = v.zk ? 150 : 0;
	}

	const solleft = (v : any, i : number) => {
		props.InvoicingList.forEach((item, k) => {
			if (k != i && item.zk == true) {
				item.zk = false;
				item.scrollleft = 0;
			}
		})
	}
	const scrolltolower = () => {
		emit('scrolltolower')
	}
	const scrollLeft = ref(0)
	const scroll = (e) => {
		// scrollLeft.value = e.detail.scrollLeft
		// console.log(e.detail.scrollLeft)
	}
</script>

<style scoped lang="less">
	.crdcroll {
		width: 91vw;
		// background-color: red;
		height: 88.5vh;
		margin-top: 1.5vh;

		.box {
			width: 89.4vw;
			display: grid;
			grid-template-columns: 1fr 1fr;
			padding-left: 1.1vw;
		}

		.fler {
			width: 44.3vw;
			height: 27.3vw;
			margin-bottom: 0.7vw;
			border-radius: 1.6vw;
			// background-color: red;
			margin-right: 0.7vw;

			border: 2px solid rgba(245, 246, 248, 1);
			position: relative;

			.jiao {
				width: 3.4vw;
				height: 3.4vw;
				position: absolute;
				top: -2px;
				left: 0;
				z-index: 99;

				.dui {
					position: absolute;
					width: 1.4vw;
					height: 1.4vw;
					top: 0.8vw;
					left: 0.8vw;
					z-index: 101;
				}

				.jao {
					width: 3.4vw;
					height: 3.4vw;
					position: absolute;
					top: 0;
					left: 0;
					z-index: 100;
				}
			}
		}
	}

	.actve {
		border: 2rpx solid #3BA1FF !important;
	}

	.carditem {
		width: calc(44.3vw - 4px);
		height: calc(27.3vw - 4px);
		background: #fff;
		position: relative;
		display: flex;
		overflow: hidden;
		white-space: nowrap;
		border-radius: 1.6vw;
		border: 2px solid rgba(245, 246, 248, 1);


		.zkadd {
			width: 10vw;
			height: 100%;
			display: inline-flex;
			justify-content: flex-end;
			position: absolute;
			top: 0;
			right: -14vw;

			>view {
				width: 7vw;
				height: 100%;
				font-weight: 400;
				font-size: 1.6vw;
				display: flex;
				justify-content: center;
				align-items: center;
				writing-mode: vertical-rl;
				// line-height: 2.2vw;
				text-orientation: upright;
				text-align: center;

				&:nth-child(2) {
					background: #1083F8;
					color: #fff;

					view {
						width: 3.8vw;
						height: 3.8vw;
						display: flex;
						justify-content: center;
						align-items: center;
					}
				}

				&:nth-child(1) {
					background: #dcdee1;
					color: #555555;
				}
			}
		}

		.zkf {
			width: 7vw;
			right: -7vw;
		}

		.add {
			position: absolute;
			right: 2vw;
			top: 50%;
			transform: translateY(-50%);
			// width: 6vw;
			// height: 17vw;
			// display: inline-flex;
			// justify-content: center;
			// align-items: center;
			// position: relative;
			// top: -2.5vw;
			// background-color: red;

			>view {
				width: 4.5vw;
				height: 1.9vw;
				background: #E3EEF7;
				border-radius: 0.9vw;
				border: 1px solid rgba(3, 133, 250, 0.34);
				font-weight: 400;
				font-size: 1.1vw;
				color: #1083F8;
				display: flex;
				justify-content: center;
				align-items: center;
				position: absolute;
				top: 0.8vw;
				right: 0.8vw;
			}

			image {
				width: 3vw;
				height: 3vw;

			}
		}

		.msitem {
			width: 25vw;
			// height: 80%;
			padding: 1vw 1vw 1vw;
			display: inline-block;

			// background-color: red;

			>view {
				// margin-top: 0.25vw;

				&:nth-child(7) {
					width: 27vw;
					display: flex;
					position: relative;
					left: -2vw;
					margin-bottom: 2vw;
					// top: 3.5vw;

					.three-one {
						width: 33%;
						// height: 100%;
						display: flex;
						flex-direction: column;
						position: relative;
						// align-items: center;
						// text-align: center;
						position: relative;

						.three-shu {
							position: absolute;
							right: 0;
							top: 50%;
							transform: translateY(-50%);
							height: 60%;
							width: 2rpx;
							background-color: #dddddd;
						}

						// background-color: red;
					}

					text {
						&:nth-child(1) {
							font-weight: bold;
							font-size: 1.8vw;
							color: #555555;
						}

						&:nth-child(2) {
							font-weight: 400;
							font-size: 1.3vw;
							color: #999999;
							text-align: center;
						}
					}
				}



				&:nth-child(2),
				&:nth-child(3),
				&:nth-child(4),
				&:nth-child(5),
				&:nth-child(6),

				{
				display: flex;
				justify-content: space-between;
				margin-bottom: 0.5vw;


				text {
					font-weight: 400;
					font-size: 1.4vw;
					color: #777777;
					white-space: nowrap;
					/* 不换行 */
					overflow: hidden;
					/* 隐藏溢出部分 */
					text-overflow: ellipsis;
					/* 显示省略号 */
				}


			}

			&:nth-child(6) {
				width: 25vw;
				height: 1.6vw;
				display: block !important;
				white-space: nowrap;
				text-overflow: ellipsis;
				overflow: hidden;
			}

			&:nth-child(1) {
				display: flex;
				justify-content: space-between;
				align-items: center;
				height: 1.8vw;
				margin-bottom: 1.2vw;
				margin-top: 1.5vw;

				// margin-top: -0.4vw;
				>view {
					width: 19vw;
					height: 2vw;
					font-weight: 800;
					font-size: 1.8vw;
					color: #222222;
					text-overflow: ellipsis;
					overflow: hidden;
					white-space: nowrap;
				}

				text {
					width: 6vw;
					font-weight: 400;
					font-size: 1.4vw;
					color: #222222;
				}
			}
		}
	}

	.gongyingshang {
		margin-top: -10rpx;
		  width: 500rpx;                /* 你原来的宽度 */
		  font-size: 1.4vw;         /* 建议不要用 vw 混用 */
		  line-height: 1.2;            /* 行高（数字形式也可以），用于计算 */
		  color: #a9a9a9;
		  font-weight: 400;
		
		  /* 多行省略核心 */
		  display: -webkit-box;
		  -webkit-box-orient: vertical;
		  -webkit-line-clamp: 2;
		  overflow: hidden;
		  text-overflow: ellipsis;     /* 单行兼容项 */
		
		  /* 兼容与保底 */
		  word-break: break-word;
		  white-space: normal;         /* 确保允许换行 */
		  min-width: 0;                /* 如果父容器是 flex 必需 */
		  max-height: calc(1.4 * 2 * 1em); /* 额外保底（可选） */
	}

	.speitem {
		width: 17vw;
		height: 100%;
		display: inline-flex;
		flex-direction: column;
		// justify-content: center;
		position: relative;
		top: -8.2vw;
		// background-color: red;

		.cardp {
			width: 100%;
			padding-left: 1vw;
			height: 4vw;
			display: flex;
			// margin-top: 3vw;
			// flex-direction: column;
			// align-items: center;
			flex-wrap: wrap;
			// margin: 0  auto 0;
			// align-items: center;

			view {
				min-width: 7vw;
				height: 2vw;
				border-radius: 0.9vw;
				border: 1px solid #D2D2D2;
				margin: 0.7vw 0 0 0.5vw;
				display: flex;
				justify-content: center;
				align-items: center;
				text-align: center;
				font-weight: 400;
				font-size: 1.2vw;
				color: #555555;

				// background-color: red;
				// padding: 0 0.5vw;
				&:nth-child(1),
				&:nth-child(2) {
					max-width: 6vw;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
					display: block;
					// line-height: 1.8vw;
					// text-align: center;
					display: flex;
					justify-content: center;
					align-items: center;

				}

				&:nth-child(3) {
					max-width: 12vw;
					// color: red;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
					display: block;
					// line-height: 1.8vw;
					// text-align: center;
					display: flex;
					justify-content: center;
					align-items: center;
					padding: 0 20rpx;
				}
			}
		}

		.imghs {
			width: 13vw;
			height: 13vw;
			margin: 0vw auto 0.25vw;
			padding: 0.2vw;
			background: #fff;
			border-radius: 1.1vw;

			>image {
				width: 100%;
				height: 100%;
				border-radius: 1.1vw;
			}
		}
	}
	}

	.guodu {
		transition: .4s;
		-webkit-transform-style: preserve-3d;
		-webkit-overflow-scrolling: touch;
	}

	.right-top {
		top: 2vw;
		right: 2vw;
		position: absolute;
		color: #1083F8;
		text-align: center;
	}
</style>