<template>
	<block>
		<view class="choose-product-item"
		v-for="(item, dix) in list"
		:key="item.id" 
		>
			<view class="choose-product-item-check" @click="checkItem(item)">
				<uni-icons v-if="!item.edit && (!item.status||item.dealerScopeFlag==0)" type="circle-filled" size="24" color="#ccc"></uni-icons>
				<uni-icons v-else :type="item.checked?'checkbox-filled':'circle'" size="24" :color="item.checked?'#dd0000':'#666'"></uni-icons>
			</view>
			 <view>
				 <view class="choose-product-item-img">
					<image mode="aspectFit" :src="item.productImage?item.productImage+'?x-oss-process=image/resize,p_30':'/static/def_imgs.png'" style="width: 100%;height: 180rpx;"></image>
					<view class="back-price xiajia" v-if="!item.status">下架</view>
					<view class="back-price xiajia" v-else-if="item.dealerScopeFlag==0">售罄</view>
					<view class="choose-product-item-left-info-code">
						<text max-lines="1" overflow="ellipsis">{{item.productCode}}</text>
					</view>
				 </view>
				 <view class="choose-product-item-info">
					<view class="choose-product-item-left-info-name">{{item.productName}}</view>
					<view class="choose-product-item-left-info-guige">
						<text max-lines="2" overflow="ellipsis">{{item.productOrigCode}}</text>
					</view>
					<view class="choose-product-item-left-info-price">
						<view class="price-txt" v-if="item.status==1">￥<text>{{item.price}}</text></view>
						<view class="price-txt" v-else></view>
						<view>
							<view v-if="item.status==1&&item.dealerScopeFlag==1" :style="{opacity:!item.edit?1:0,width:!item.edit?'auto':0}">
								<view class="flex align_center qty-box">
									<view class="qty-btn" @click="changeQty(item.qty-1,item,1)" :class="(item.qty <= 1)?'qty-disabled':''">-</view>
									<view class="qty-num" :style="{width:Number(item.qty).toString().length*8+10+'px'}"><input type="number" @input="inputQty(item)" @blur="changeQty(item.qty,item,0)" v-model="item.qty"/></view>
									<view class="qty-btn" @click="changeQty(item.qty+1,item,1)" :class="(item.qty >= 999)?'qty-disabled':''">+</view>
								</view>
							</view>
							<view v-if="!item.edit && (!item.status||item.dealerScopeFlag==0)" class="edit-qty">
								X <text>{{item.qty}}</text>
							</view>
							<view v-if="item.edit" class="edit-qty">
								X <text>{{item.qty}}</text>
								<image style="width: 24px;height: 24px;" mode="aspectFill" :src="'../static/ashbin.png'" @click="remove(item.cartSn)"></image>
							</view>
						</view>
					</view>
				 </view>
			 </view>
		</view>
	</block>
</template>

<script>
	export default {
		name: 'productItem',
		props: {
			list: {
				type: Array,
				default: () => []
			},
		},
		data() {
			return {
				isChange: false
			}
		},
		methods: {
			inputQty(item){
				this.isChange = true
			},
			changeQty(value,item,type) {
				const max = 999
				const v = value > max ? max : (value==''||value==0?1:value)
				if(type == 0){
					if(!this.isChange){
						return
					}
					this.updateNums(value,item.cartSn)
					this.$nextTick(()=>{
						setTimeout(()=>{
							this.updateNums(v,item.cartSn)
							this.isChange = false
						},100)
					})
				}else{
					if(value<=max && value >= 1){
						this.updateNums(v,item.cartSn)
					}
				}
			},
			updateNums(e, cartSn) {
				this.$emit('changeQty', Number(e), cartSn)
			},
			remove(cartSn){
				this.$emit('remove', [cartSn], false)
			},
			checkItem(item){
				if(item.edit || (item.status==1&&item.dealerScopeFlag==1)){
					this.$emit('check',item.cartSn)
				}
			}
		}
	}
</script>

<style lang="less" scoped>
	.qty-box{
		display: flex;
		.qty-btn{
			display: flex;
			align-items: center;
			justify-content: center;
			width: 22px;
			height: 22px;
			border-radius: 50px;
			font-size: 18px;
			background-color: #066cff;
			color: #fff;
			&.qty-disabled{
				background-color: #c0c0c0;
			}
		}
		.qty-num{
			width: 24px;
			height: 22px;
			line-height: 22px;
			padding:0 3px;
			input{
				width: 100%;
				height: 100%;
				text-align: center;
				color: #333;
			}
		}
	}
	.choose-product-item{
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 10px;
		background: #fff;
		margin: 8px 8px 0 8px;
		border-radius: 10px;
		.price-txt{
			color: red;
			display: flex;
			align-items: center;
			text{
				font-size: 36rpx;
			}
		}
		> view{
			 display: flex;
			 align-items: center;
			 justify-content: space-between;
			 &:last-child{
				 flex-grow: 1;
				 width: 80%;
			 }
		}
		.choose-product-item-check{
			padding: 0 20rpx 0 0;
			width: 24px;
		}
		.choose-product-item-img{
			margin-right: 20rpx;
			width: 30%;
			position: relative;
			overflow: hidden;
			border: 1px solid #eee;
			padding: 10rpx;
			.back-price{
				zoom: calc(0.8);
				padding: 6rpx 65rpx;
				position: absolute;
				top: 18rpx;
				right: -45rpx;
				z-index: 2;
				font-size: 20rpx;
				text-align: center;
				transform: rotate(40deg);
				display: flex;
				align-items: center;
				text{
					font-size: 28rpx;
					margin: 0 5rpx;
				}
			}
			.xiajia{
				background: #ececec;
				color: #5f5f5f;
			}
			.choose-product-item-left-info-code{
				color: #666;
				position: absolute;
				width: 100%;
				left:0;
				bottom:0;
				background: rgba(255,255,255,0.7);
				text-align: center;
			}
		}
		.choose-product-item-info{
			width: 70%;
			flex-grow: 1;
			.choose-product-item-left-info-price{
				display: flex;
				align-items:center;
				justify-content: space-between;
				>view{
					&:last-child{
						display: flex;
						align-items:center;
					}
				}
				.edit-qty{
					display: flex;
					align-items:center;
					justify-content: space-between;
					color: #999;
					font-size: 12px;
					> text{
						color: #666;
						font-size: 14px;
						margin-right: 20px;
					}
				}
			}
			.choose-product-item-left-info-name{
				font-size: 28rpx;
				color: #666;
			}
			.choose-product-item-left-info-guige{
				color: #2196F3;
				margin: 10rpx 0;
				text{
					margin-right: 10rpx;
				}
			}
		}
	}
</style>