<template>
	<view>
		<uni-popup ref="popup" type="bottom" background-color="#fff">
			<view class="popup_title">添加产品</view>
			<view class="searbox">
				<image :src="BASE_IMG_URL+'search.png'" class="searchimg" mode=""></image>
				<input type="text" placeholder="输入产品编号/名称搜索" @input="queryList" class="selfinput">
			</view>
			<scroll-view scroll-y class="scrollbox"  lower-threshold="30">
				<view class="pop_list">
					<view class="pop_li" v-for="(item,index) in list" :key="index">
						<view class="pop_li_top">
							<view class="pop_title">{{item.name}}</view>
							<view class="pop_num">{{item.num}}</view>
						</view>
						<view class="pop_info" @tap.stop="changeSelect(index)">
							<view class="pop_radio">
								<radio color='#008EFF' :checked="item.select" ></radio>
							</view>
							<image :src="item.img ? item.img : BASE_IMG_URL+'img/index-4.png'" class="infoimg" mode=""></image>
							<view class="pop_info_cen">
								<view class="pop_info_text">单位：{{item.unit}}</view>
								<view class="pop_info_text">零售价：￥{{item.price}}</view>
								<view class="pop_info_text">批发价：￥{{item.wholesale}}</view>
								<view class="pop_bottom" @tap.stop.prevent v-if="showNumber">
									数量
									<view class="conright">
										<uni-number-box :min="1" v-model="item.number" @change="(e)=>{changeNumber(e,index)}"></uni-number-box>
									</view>
								</view>
							</view>
						</view>
					</view>
					<u-empty v-if="list.length == 0" text="暂无更多"></u-empty>
				</view>
			</scroll-view>
			<view class="popup_bottom">
				<view class="pp_left_box">
					<view class="pp_left">
						<radio color="#008EFF" :checked="isAll" @click="selectAll"></radio>全选
					</view>
					<view class="selectnum">已选{{selectArr.length}}项</view>
				</view>
				<view class="pp_right">
					<view class="right_btn remove" @click="closePopup">取消</view>
					<view class="right_btn sure" @click="sureSelectData">确定</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import { netContractProduct } from '@/api/kehu.js'
	import { BASE_IMG_URL } from '@/util/api.js'
	
	export default{
		props:{
			showProduct:{
				type:Boolean,
				default:false
			},
			showNumber:{
				type:Boolean,
				default:true
			}
		},
		data(){
			return{
				BASE_IMG_URL:BASE_IMG_URL,
				name:'',
				list:[],
				isAll:false,
				selectArr:[]
			}
		},
		methods:{
			queryList(e) {
				this.name = e.detail.value
				this.init()
			},
			init() {
				this.$refs.popup.open()
				this.list = []
				this.getProductList()
			},
			getProductList() {
				let params = {
					name:this.name
				}
				netContractProduct(params).then(res=>{
					let arr = res.data
					arr.forEach(ele=>{
						ele.number = 1
						ele.select = false
					})
					this.list = arr
				})
			},
			changeNumber(e,index) {
				let obj = this.list[index]
				if(e > 1){
					obj.select = true
				}
				obj.number = e
				this.$set(this.list,index,obj)
				this.handleSelect()
			},
			//选择
			changeSelect(index) {
				let obj = this.list[index]
				obj.select = !obj.select
				this.$set(this.list,index,obj)
				this.handleSelect()
			},
			//全选
			selectAll() {
				this.isAll = !this.isAll
				let arr = this.list
				if(this.isAll){
					arr.forEach(ele=>{
						ele.select = true
					})
				}else{
					arr.forEach(ele=>{
						ele.select = false
					})
				}
				this.list = arr
				this.handleSelect()
			},
			closePopup() {
				this.$refs.popup.close()
			},
			handleSelect() {
				let arr = this.list
				let newdata = []
				arr.forEach(ele=>{
					if(ele.select){
						newdata.push(ele.id)
					}
				})
				this.selectArr = newdata
			},
			//确定
			sureSelectData() {
				let arr = this.list
				let newarr = []
				arr.forEach(ele=>{
					if(ele.select){
						newarr.push(ele)
					}
				})
				this.closePopup()
				this.$emit('selectList',newarr)
			}
		}
	}
</script>

<style lang="scss" scoped>
	view{
		box-sizing: border-box;
	}
	.popup_title{
		text-align: center;
		font-size:34rpx;
		color:#333333;
		padding:24rpx;
	}
	.searbox{
		width:650rpx;
		height:68rpx;
		border-radius: 34rpx;
		margin:0 auto;
		border:1rpx solid $uni-text-color;
		display: flex;
		justify-content: flex-start;
		align-items: center;
		padding:0 15rpx;
		.searchimg{
			width:48rpx;
			height:48rpx;
			margin-right:15rpx;
		}
		.selfinput{
			width:450rpx;
			font-size:26rpx;
			color:$uni-text-color;
		}
	}
	.scrollbox{
		width:100%;
		height:700rpx;
		.pop_list{
			padding:24rpx;
			.pop_li{
				background:#fff;
				box-shadow: 1rpx 1rpx 8rpx 2rpx rgba(0,0,0,0.1);
				border-radius: 24rpx;
				margin-bottom:24rpx;
				.pop_li_top{
					padding:28rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;
					border-bottom:1rpx solid #f5f5f5;
					.pop_title{
						font-size:34rpx;
						color:#333333;
					}
					.pop_num{
						font-size:30rpx;
						color:#666666;
					}
				}
				.pop_info{
					display: flex;
					justify-content: flex-start;
					align-items: center;
					padding:24rpx 30rpx;
					.pop_radio{
						margin-right:32rpx;
						radio{
							transform: scale(0.7);
						}
					}
					.infoimg{
						width:140rpx;
						height:140rpx;
						border-radius: 20rpx;
						margin-right:60rpx;
					}
					.pop_info_cen{
						.pop_info_text{
							font-size:28rpx;
							color:#666666;
							margin-bottom:15rpx;
						}
						.pop_bottom{
							display: flex;
							justify-content: flex-start;
							align-items: center;
							font-size:28rpx;
							color:#666666;
							.conright{
								margin-left:10rpx;
								.stepbtn{
									width:38rpx;
									height:38rpx;
									border:1rpx solid $uni-text-color;
								}
							}
						}
					}
				}
			}
		}
	}
	.popup_bottom{
		width:100%;
		padding:0 24rpx;
		height:100rpx;
		border-top:1rpx solid #f5f5f5;
		display: flex;
		justify-content: space-between;
		align-items: center;
		.pp_left_box{
			display: flex;
			justify-content: flex-start;
			align-items: center;
			.pp_left{
				display: flex;
				justify-content: flex-start;
				align-items: center;
				font-size:26rpx;
				color:3333;
				radio{
					transform: scale(0.7);
				}
			}
			.selectnum{
				font-size:30rpx;
				color:#FE9440;
				margin-left:24rpx;
			}
		}
		
		.pp_right{
			display: flex;
			justify-content: flex-end;
			align-items: center;
			.right_btn{
				width:165rpx;
				height:60rpx;
				font-size:30rpx;
				text-align: center;
				border-radius: 30rpx;
			}
			.remove{
				border:1rpx solid $uni-text-color;
				color:$uni-text-color;
				line-height: 58rpx;
				margin-right:24rpx;
			}
			.sure{
				background:$uni-text-color;
				color:#fff;
				line-height: 58rpx;
			}
		}
	}
</style>
