<template>
	<view class="content">
		<!-- 商品列表 -->
		<view class="goods-list">
			<view class="item" v-for="(item,index) in carts" :key="index">
				<label style="margin-right: 30upx;">
					<checkbox :value="item.checked"  :checked="item.checked" @click="checkChange(item)" />
				</label>
				<image class="goods-img" :src="item.productAttrImage==null||item.productAttrImage==''?item.productImage:item.productAttrImage" mode="aspectFit"></image>
				<view class="info-box">
					<view>
						<view class="title-box">
							<view class="tag">{{utils.getDictLabelName("storeProductType",item.productType)}}</view>
							<view class="title ellipsis">{{ item.productName }}</view>
						</view>
						<view class="intro ellipsis">{{item.productAttrName}}</view>
					</view>
					<view class="price-num">
						<view class="price">
							<text class="unit">¥</text>
							<text class="text">{{item.price}}</text>
						</view>
						<view class="num-box">
							<view class="img-box" @click="delNum(item)">
								<image v-if="item.cartNum <= 1" src="../../static/images/jian.png" mode=""></image>
								<image v-else src="../../static/images/jian2.png" mode=""></image>
							</view>
							<input type="number"  @change="changeNum($event,item)" :value="item.cartNum" disabled/>
							<view class="img-box" @click="addNum(item)">
								<image src="../../static/images/add.png" mode=""></image>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view>
			<u-popup :show="dictshow" @close="closedict" mode="center" :closeOnClickOverlay="false" round="20">
				<view  class="w600 h500 radius20 ">
					<view class="p40 justify-end">
						<image src="/static/images/close24.png" class="w40 h40" @click="closedict"></image>
					</view>
					<view class="m20">
						<view class="justify-start align-center u-border radius20 p20">
							<view>订单类型：</view>
							<view class="color6 flex-1" @click="cardshow=true">{{cardvalue?cardvalue:'请选择订单类型'}}</view>
						</view>
						<view class="justify-start align-center u-border radius20 p20 mt20 mb60">
							<view>媒体类型：</view>
							<view class="color6 flex-1" @click="videoshow=true">{{videovalue?videovalue:'请选择媒体类型'}}</view>
						</view>
					</view>
					<view class="subdict " @click="submitgoods">确认</view>
				</view>
			</u-popup>
		</view>
		<view>
			<u-popup :show="cardshow" mode="bottom"  @close="cardshow=!cardshow" :closeOnClickOverlay="true" closeIconPos='top-right'>
				<view class=" h600">
					<view class="mb20" style="text-align: center;">
						<view style="display: inline-block; ">订单类型</view>
						<u-icon name="close-circle" color="#ccc" size="28"
						@click="cardshow=!cardshow" style="display: inline-block;float: left;"></u-icon>
					</view>
					<view class="justify-start wrap">
						<view class="p16 mlr4" v-for="(item,index) in cardlist" :key="index">
							<u-tag :text="item.dictLabel" :plain="!item.checked" type="primary" :name="index"
									@click="checkboxClick">
							</u-tag>
						</view>
						<view class="base-color-red fs24 bor-red p8 radius8 plr12" v-if="cardlist.length==0">暂无订单类型</view>
					</view>
					<view class="centerV">
						<view @click="getcardid" class="surebtn">确定</view>
					</view>
				</view>
			</u-popup>
		</view>
		<view>
			<u-popup :show="videoshow" mode="bottom"  @close="videoshow=!videoshow" :closeOnClickOverlay="true" closeIconPos='top-right'>
				<view class=" h600">
					<view class="mb20" style="text-align: center;">
						<view style="display: inline-block; ">媒体类型</view>
						<u-icon name="close-circle" color="#ccc" size="28"
						@click="videoshow=!videoshow" style="display: inline-block;float: left;"></u-icon>
					</view>
					<view class="justify-start wrap">
						<view class="p16 mlr4" v-for="(item,index) in videolist" :key="index">
							<u-tag :text="item.dictLabel" :plain="!item.checked" type="primary" :name="index"
									@click="checkboxClickA">
							</u-tag>
						</view>
						<view class="base-color-red fs24 bor-red p8 radius8 plr12" v-if="videolist.length==0">暂无媒体类型</view>
					</view>
					<view class="centerV">
						<view @click="getvideoid" class="surebtn">确定</view>
					</view>
				</view>
			</u-popup>
		</view>
		<!-- 底部按钮 -->
		<view class="btn-foot">
			<view class="left">
				<label>
					<checkbox  :checked="checkAll" @click="handleCheckAll()" />
				</label>
				<text class="text">全选</text>
				<text class="text" @click="delCart()">删除</text>
			</view>
			<view class="right">
				<view class="total">
					<text class="label">合计：</text>
					<view class="price">
						<text class="unit">¥</text>
						<text class="num">{{totalMoney.toFixed(2)}}</text>
					</view>
				</view>
				<view class="btn" @click="submit">制单</view>
			</view>
		</view>
		
	</view>
</template>

<script>
	import {getCarts,cartNum,delCart} from '@/api/product'
	import {createSalesOrder} from '@/api/companyOrder.js'
	export default {
		 
		data() {
			return {
				totalMoney:0.00,
				carts:[],
				checkAll:false,
				selectCartsid:[],
				dictshow:false,
				cardvalue:'',
				cardid:'',
				videovalue:'',
				videoid:'',
				cardshow:false,
				videoshow:false,
				videolist:{},
				cardlist:{}
			}	
		},
		onLoad() {
			this.getCarts();
			const dicts=JSON.parse(uni.getStorageSync('dicts'))
			console.log(dicts)
			this.videolist=dicts.storeOrderMedium.map(item => {
				return {
					...item,
					checked: false,
				}
			})
			this.cardlist=dicts.storeOrderType.map(item => {
				return {
					...item,
					checked: false,
				}
			})
			console.log(this.cardlist)
		},
		 
		methods: {
			checkboxClickA(name){
				this.videolist.map((item, index) => {
				  item.checked = index === name ? true : false;
				});
				console.log(name)
			},
			getvideoid(){
				this.videovalue = this.videolist.filter(item => item.checked)
				.map(v => v.dictLabel).join('')
				console.log(this.cardvalue)
				this.videoid = this.videolist.filter(item => item.checked).map(v => v.dictValue).join('')
				this.videoshow=false
			},
			checkboxClick(name){
				this.cardlist.map((item, index) => {
				  item.checked = index === name ? true : false;
				});
				console.log(name)
			},
			getcardid(){
				this.cardvalue = this.cardlist.filter(item => item.checked)
				.map(v => v.dictLabel).join('')
				console.log(this.cardvalue)
				this.cardid = this.cardlist.filter(item => item.checked).map(v => v.dictValue).join('')
				this.cardshow=false
			},
			closedict(){
				this.dictshow=!this.dictshow
			},
			changeNum(e,item) {
				item.cartNum = e.detail.value.replace(/\D/g, '')
				if (item.cartNum <= 1) {
				  uni.showToast({
				    title: "已经是底线啦!",
				    icon: "none",
				    duration: 2000
				  });
				  return;
				}
				if(item.cartNum < 1) {
					item.cartNum = 1
				}
				if(item.cartNum>=item.stock){
					item.cartNum=item.stock;
				}
				this.changeCartNum(item)
			},
			delCart(){
				var selectCarts=this.carts.filter(ele => ele.checked==true).map(ele => {
				  return ele.id
				});
				if(selectCarts.length==0){
					uni.showToast({
						icon:'none',
						title: "请选择商品删除",
					});
					return;
				}
				let data = {ids:selectCarts};
				delCart(data).then(
					res => {
						if(res.code==200){
							uni.showToast({
								icon:'success',
								title: "操作成功",
							});
							this.getCarts()
						}else{
							
							uni.showToast({
								icon:'none',
								title: res.msg,
							});
						}
					},
					rej => {}
				);
				console.log(selectCarts)
			},
			computedMoney(){
				var money=0;
				var that=this;
				this.carts.forEach((item,index,arr)=>{
					 if(item.checked){
						 money+=item.price*item.cartNum;
					 }
				})
				console.log(money);
				this.totalMoney=money;
			},
			handleCheckAll(){
				this.checkAll=!this.checkAll;
				var that=this;
				this.carts.forEach((item,index,arr)=>{
				     item.checked=that.checkAll;
				})
				this.computedMoney();
			},
			checkChange(item){
				item.checked=!item.checked;
				this.computedMoney();
			},
			changeCartNum(item){
				let data = {number:item.cartNum,id:item.id};
				cartNum(data).then(
					res => {
						if(res.code==200){
							uni.showToast({
								icon:'none',
								title: "操作成功",
							});
							this.computedMoney();
							
						}else{
							
							uni.showToast({
								icon:'none',
								title: res.msg,
							});
						}
					},
					rej => {}
				);
			},
			getCarts(){
				getCarts().then(
					res => {
						if(res.code==200){
							 this.carts=res.carts;
							 this.carts.forEach((item,index,arr)=>{
							      item.checked=false;
							 })
							 this.computedMoney();
						}else{
							uni.showToast({
								icon:'none',
								title: "请求失败",
							});
						}
					},
					rej => {}
				);
			},
			// 购物车减法
			delNum(item) {
				if (item.cartNum <= 1) {
				  uni.showToast({
				    title: "已经是底线啦!",
				    icon: "none",
				    duration: 2000
				  });
				  return;
				}
				item.cartNum --
				if(item.cartNum < 1) {
					item.cartNum = 1
				}
				 
				this.changeCartNum(item)
			},
			// 购物车加法
			addNum(item) {
				console.log(item)
				item.cartNum++
				if(item.cartNum>=item.stock){
					item.cartNum=item.stock;
				}
				this.changeCartNum(item)
			},
			submitgoods(){
				//选择后提交制单
				var data={
					token:uni.getStorageSync('CompanyUserToken'),
					cateIds:this.selectCartsid.toString(),
					orderType:this.cardid,
					orderMedium:this.videoid
					}
				createSalesOrder(data).then(
					res => {
						if(res.code==200){
							uni.navigateTo({
								url: './confirmCompanyOrder?orderKey='+res.orderKey
							})
						}else{
							uni.showToast({
								icon:'none',
								title: "请求失败",
							});
						}
					},
					rej => {}
				);
			},
			// 结算
			submit() {
				this.selectCartsid=this.carts.filter(ele => ele.checked==true).map(ele => {
				  return ele.id
				});
				if(this.selectCartsid.length==0){
					uni.showToast({
						icon:'none',
						title: "请选择商品",
					});
					return;
				}
				this.dictshow=true
			}
			
		}
	}
</script>

<style lang="scss">
	.surebtn{
		margin: 20rpx 0;
		background-color: #018C39;
		color: #fff;
		width: 80%;
		text-align: center;
		padding: 20rpx 0;
		border-radius: 40rpx;
	}
	.subdict{
		width: 300rpx;
		height: 80rpx;
		line-height: 80rpx;
		background-color: #018C39;
		border-radius: 200rpx;
		color: #fff;
		text-align: center;
		font-size: 28rpx;
		margin: 0 auto;
	}
	page {
		height: 100%;
	}
	.content{
		height: 100%;
		padding: 20upx;
		.goods-list{
			padding-bottom: 120upx;
			.item{
				box-sizing: border-box;
				height: 221upx;
				background: #FFFFFF;
				border-radius: 16upx;
				margin-bottom: 20upx;
				padding: 30upx;
				display: flex;
				align-items: center;
				&:last-child{
					margin-bottom: 0;
				}
				.goods-img{
					width: 160upx;
					height: 160upx;
					background: #FFFFFF;
					margin-right: 30upx;
					flex-shrink: 0;
				}
				.info-box{
					height: 160upx;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					width: calc(100% - 255upx);
					.title-box{
						width: 100%;
						display: flex;
						align-items: center;
						.tag{
							padding: 0 6upx;
							height: 30upx;
							line-height: 30upx;
							font-size: 22upx;
							font-family: PingFang SC;
							font-weight: bold;
							color: #FFFFFF;
							background: linear-gradient(90deg, #3def30 0%,#018C39 100%);
							border-radius: 4upx;
							margin-right: 10upx;
							flex-shrink: 0;
						}
						.title{
							flex: 1;
							font-size: 28upx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #111111;
							line-height: 1;
						}
					}
					.intro{
						font-size: 24upx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #999999;
						margin-top: 22upx;
						line-height: 1;
					}
					.price-num{
						display: flex;
						align-items: center;
						justify-content: space-between;
						.price{
							display: flex;
							align-items: flex-end;
							.unit{
								font-size: 24upx;
								font-family: PingFang SC;
								font-weight: 500;
								color: #FF6633;
								line-height: 1.2;
								margin-right: 4upx;
							}
							.text{
								font-size: 32upx;
								font-family: PingFang SC;
								font-weight: bold;
								color: #FF6633;
								line-height: 1;
							}
						}
						.num-box{
							display: flex;
							align-items: center;
							.img-box{
								width: 60upx;
								height: 60upx;
								// border-radius: 4upx;
								border: 1px solid #dddddd;
								display: flex;
								align-items: center;
								justify-content: center;
								image{
									width: 25rpx;
									height: 25rpx;
								}
							}
							input{
								width: 60upx;
								height: 60upx;
								line-height: 60upx;
								font-size: 28upx;
								font-family: PingFang SC;
								font-weight: 500;
								color: #111111;
								// border-radius: 4upx;
								border-top: 1px solid #dddddd;
								border-bottom: 1px solid #dddddd;
								text-align: center;
								// margin: 0 16upx;
							}
						}
					}
				}
			}
		}
		.btn-foot{
			box-sizing: border-box;
			width: 100%;
			height: 121upx;
			background: #FFFFFF;
			padding: 16upx 30upx 16upx 60upx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			position: fixed;
			left: 0;
			bottom: 0;
			z-index: 99;
			.left{
				display: flex;
				align-items: center;
				.text{
					margin-left: 14upx;
					font-size: 28upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #666666;
					line-height: 1;
				}
			}
			.right{
				display: flex;
				align-items: center;
				.total{
					display: flex;
					align-items: flex-end;
					margin-right: 36upx;
					.label{
						font-size: 26upx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #999999;
						line-height: 1.5;
					}
					.price{
						display: flex;
						align-items: flex-end;
						.unit{
							font-size: 32upx;
							font-family: PingFang SC;
							font-weight: bold;
							color: #FF6633;
							line-height: 1.2;
							margin-right: 10upx;
						}
						.num{
							font-size: 30upx;
							font-family: PingFang SC;
							font-weight: bold;
							color: #FF6633;
							line-height: 1;
						}
					}
				}
				.btn{
					width: 200upx;
					height: 88upx;
					line-height: 88upx;
					text-align: center;
					font-size: 30upx;
					font-family: PingFang SC;
					font-weight: bold;
					color: #FFFFFF;
					background:#018C39;
					border-radius: 44upx;
				}
			}
		}
	}
</style>
