<template>
	<view class="warpbox">
		<!-- 搜索 -->
		<view class="searbox">
			<view class="censear">
				<image src="/static/img/search.png" class="searimg" mode=""></image>
				<input type="text" :placeholder="i18n['请输入关键词搜索']" @input="queryData" class="lself">
			</view>
			<image src="/static/img/delete.png" class="deleimg" @click="del" mode=""></image>
		</view>
		
		<scroll-view class="scrollbox" scroll-y @scrolltolower="loadMorePart">
			<view class="part_warp">
				<view class="part_item" v-for="(item,index) in list" :key="index" @tap.stop="selectPart(index)">
					<radio value="r1" color="#7FC2FF" :checked="item.select" />
					<view class="item_infor">
						<image :src="item.parts.img" class="partimg" mode=""></image>
						<view class="info">
							<view class="info_box">
								<view class="title">{{item.parts.name}}</view>
								<view class="desc">{{i18n['规格']}}：{{item.parts.unit}}</view>
							</view>
							<view class="info_bot" @tap.stop.prevent>
								<view class="partmoney">￥{{item.parts.price}}</view>
								<uni-number-box v-model="item.num" @change="(e)=>{changeNum(e,index)}"></uni-number-box>
							</view>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
		
		<view class="botbox">
			<view class="botleft">
				<image src="/static/img/cart.png" class="carimg"></image>
				<view class="left_te">
					<view class="num" >{{i18n['已选']}}{{selectList.length}}{{i18n['件']}}</view>
					<view class="totalmoney">￥{{totalMoney}}</view>
				</view>
			</view>
			<!-- <view class="botright" @click="subOrder">{{i18n['提交订单']}}</view> -->
		</view>
	</view>
</template>

<script>
	import { netCartListnum, netChangePartNum, netDelCartNum } from '@/api/api.js'
	
	export default{
		data(){
			return{
				list:[],
				name:'',
				page:1,
				totalPage:1,
				totalMoney:0,
				selectList:[]
			}
		},
		computed:{
			i18n() {
				return this.$t("shopmall")
			}
		},
		onShow() {
			uni.setNavigationBarTitle({
				title: this.i18n['购物车']
			})
			this.list = []
			this.page = 1
			//购物车 列表
			this.getList()
		},
		methods:{
			queryData(e) {
				this.name = e.detail.value
				this.list = []
				this.page = 1
				setTimeout(()=>{
					this.getList()
				},200)
			},
			loadMorePart() {
				if(this.page < this.totalPage){
					this.page ++
					this.getList()
				}
			},
			getList() {
				let params = {
					page:this.page,
					name: this.name
				}
				netCartListnum(params).then(res=>{
					let data = res.data.data
					data.forEach(ele=>{
						ele.select = false
					})
					this.list = this.list.concat(data)
					this.totalPage = res.data.last_page
				})
			},
			selectPart(index) {
				let obj = this.list[index]
				obj.select = !obj.select
				this.$set(this.list,index,obj)
				//重新计算  money
				this.handleMoney()
				//筛选  已选择的数据
				this.handleSelected()
			},
			handleSelected() {
				let data = this.list
				let arr = []
				data.forEach(ele=>{
					if(ele.select) {
						arr.push(ele.id)
					}
				})
				this.selectList = arr
			},
			changeNum(e,index) {
				let num = e
				let obj = this.list[index]
				obj.num = num
				this.$set(this.list,index,obj)
				//修改数量
				this.changeCartnum(num,obj)
				//重新计算  money
				this.handleMoney()
			},
			changeCartnum(num,obj) {
				let params = {
					id: obj.id,
					num
				}
				netChangePartNum(params).then(res=>{})
			},
			handleMoney(){
				let data = this.list
				let money = 0
				data.forEach(ele=>{
					if(ele.select) {
						money += ele.num * Number(ele.parts.price)
					}
				})
				this.totalMoney = money
			},
			del(){
				let arr = this.selectList
				if(arr.length == 0){
					uni.showToast({
						title:'请选择需要删除的配件',
						icon:'none'
					})
					return
				}
				netDelCartNum({ids:arr.join(',')}).then(res=>{
					uni.showToast({
						title:res.msg,
						icon:'none'
					})
					setTimeout(()=>{
						this.list = []
						this.page = 1
						this.getList()
						this.totalMoney = 0
						this.selectList = []
					},2000)
				})
			},
			subOrder() {
				if(this.selectList.length == 0) {
					uni.showToast({
						title:'请选择要下单的配件',
						icon:'none'
					})
					return
				}
				let ids = this.selectList.join(',')
				uni.navigateTo({
					url:'/pages/serviceMall/orderDetail?ids='+ids
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.searbox{
		padding:10rpx 0 20rpx 0;
		display: flex;
		justify-content: center;
		align-items: center;
		
		.censear{
			display: flex;
			justify-content: flex-start;
			align-items: center;
			padding:0 24rpx;
			border-radius: 34rpx;
			background:#FFFFFF;
			width:564rpx;
			height:68rpx;
			border:1rpx solid #7FC2FF;
			.searimg{
				width:48rpx;
				height:48rpx;
			}
			.lself{
				width:420rpx;
				font-size:26rpx;
				color:rgba(2, 135, 255, 1);
			}
		}
		.deleimg{
			width:44rpx;
			height:44rpx;
			margin-left:35rpx;
		}
	}
	.scrollbox{
		width:750rpx;
		height:calc(100vh - 300rpx);
		.part_warp{
			.part_item{
				width:690rpx;
				margin:0 auto 24rpx;
				background:#fff;
				border-radius: 20rpx;
				padding:30rpx;
				display: flex;
				justify-content: flex-start;
				align-items: center;
				radio{
					transform: scale(0.7);
					flex-shrink: 0;
				}
				.item_infor{
					margin-left:24rpx;
					display: flex;
					justify-content: flex-start;
					align-items: center;
					.partimg{
						width: 130rpx;
						height: 130rpx;
						border-radius: 10rpx;
						margin-right:24rpx;
						flex-shrink: 0;
					}
					.info{
						width:380rpx;
						.info_box{
							.title{
								font-size:30rpx;
								color:#333333;
							}
							.desc{
								font-size:26rpx;
								color:#999999;
								margin-top:15rpx;
							}
						}
						.info_bot{
							width:100%;
							display: flex;
							justify-content: space-between;
							align-items: center;
							margin-top:15rpx;
							.partmoney{
								font-size:26rpx;
								color:#7FC2FF;
							}
						}
					}
				}
			}
		}
	}
	
	.botbox{
		width:690rpx;
		background:#fff;
		margin:24rpx auto;
		border-radius: 20rpx;
		padding:24rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		.botleft{
			display: flex;
			justify-content: flex-start;
			align-items: center;
			.carimg{
				width:48rpx;
				height:41rpx;
				margin-right:15rpx;
			}
			.left_te{
				display: flex;
				justify-content: center;
				align-items: center;
				.totalmoney{
					color:#7FC2FF;
					font-size:24rpx;
					margin-left:24rpx;
				}
				.num{
					font-size:24rpx;
					color:#999;
				}
			}
		}
		.botright{
			width:360rpx;
			height:88rpx;
			border-radius: 45rpx;
			background:#7FC2FF;
			font-size:30rpx;
			color:#fff;
			text-align: center;
			line-height: 88rpx;
		}
	}
</style>
