<template>
	<view class="shopping-cart">
		<view class="shopping-cart-item" v-for="seller in cart">
			<view class="shopping-cart-item-head">
				<view class="select">
					<checkbox class="checkbox" :checked="seller.checked" @tap="selectSeller(seller.seller_id)" />
				</view>
				<view class="factory-name">{{seller.seller_nickname?seller.seller_nickname:'省心直供（该厂家暂未设置昵称）'}}</view>
			</view>
			<view class="shopping-cart-info">
				<view class="product-item" v-for="product in seller.products">
					<view class="select">
						<checkbox class="checkbox" :checked="product.checked"
							@tap="select(product.id,product.spec_index)" />
					</view>

					<view class="product-image">
						<image class="image" :src="product.spec_image|imagesFilter" mode="scaleToFill"></image>
					</view>
					<view class="product-info">
						<view class="row row-1">
							<text class="title"><text class="sxzg-icon">省心直供</text>{{product.name}}</text>
						</view>
						<view class="row">
							<view class="spec">{{product.spec_name}}</view>
						</view>
						<view class="row row-2">
							<text class="org-price">￥{{product.spec_org_price}}</text>
						</view>
						<view class="row">
							<text class="sxj-icon">省心价</text>
							<text class="price">￥{{product.spec_price}}</text>
						</view>
					</view>
					<view class="counter">
						<view class="sub" @tap="subnum(product.id,product.num,product.spec_index)">－</view>
						<input class="num" type="text" v-model="product.num" />
						<view class="plus" @tap="plusnum(product.id,product.num,product.spec_index)">＋</view>
					</view>
				</view>
			</view>
		</view>



		<view class="footer">
			<view class="select-all">
				<label @tap="selectAll()">
					<checkbox color="#ff5d5b" class="checkbox" :checked="checkall" />全选
				</label>
			</view>
			<view class="total">合计 <text>￥{{total}}</text></view>
			<view class="settlement"><button class="settlement-btn" type="default">结算</button></view>
		</view>
	</view>

</template>

<script>
	import {
		mapState
	} from 'vuex'

	export default {
		data() {
			return {
				checkall: false
			}
		},
		computed: mapState({
			cart: state => state.cart,
			total() {
				return this.$store.getters['cart/total']
			}
		}),
		onLoad() {
			// uni.removeStorageSync("cart")
		},
		methods: {
			selectSeller(seller_id) {
				this.$store.commit("cart/selectSeller", {
					seller_id: seller_id
				})
			},
			select(id, spec_index) {
				this.$store.commit("cart/select", {
					id: id,
					spec_index: spec_index
				})
			},
			selectAll() {
				this.checkall = !this.checkall;
				this.$store.commit("cart/selectAll", {
					checked: this.checkall
				})
			},
			subnum(id, num, spec_index) {
				if (num === 0) {
					uni.showModal({
						title: "移除商品",
						content: "确认移除该商品？",
						success: (res) => {
							if (res.confirm) {
								this.$store.commit("cart/remove", {
									id: id,
									num: num,
									spec_index: spec_index

								})
							}
						}
					})
				} else {
					num--
				}
				this.$store.commit("cart/numchange", {
					id: id,
					num: num,
					spec_index: spec_index
				})
			},
			plusnum(id, num, spec_index) {
				num++
				this.$store.commit("cart/numchange", {
					id: id,
					num: num,
					spec_index: spec_index
				})
			}
		}
	}
</script>

<style lang="scss">
	.shopping-cart {
		padding-bottom: 80upx;
		overflow: hidden;
		position: relative;
	}

	.shopping-cart-item {
		padding: 20upx;
		background: white;
		// padding-left: 100upx;
		margin: 20upx;
		border-radius: 20upx;

		.shopping-cart-item-head {
			margin-bottom: 20upx;
			font-size: 28upx;
			font-weight: bold;
			position: relative;
			display: flex;


		}

		.select {
			align-self: center;
			position: relative;
			margin-top: -4upx;
			margin-right: 20upx;
		}

		.checkbox {
			transform: scale(0.7);

			/deep/ .uni-checkbox-input {
				border: 2upx solid #CCCCCC;

				&.uni-checkbox-input-checked {
					color: $primary-color !important;
				}
			}

			/deep/ .uni-checkbox-input:hover {
				border: 2upx solid #CCCCCC;
			}
		}

		.product-item {
			display: flex;
			position: relative;
			margin-bottom: 20upx;

			.checkbox::before {
				top: 70upx;
			}


			.product-image {
				margin-right: 20upx;
				width: 180upx;
				height: 180upx;

				.image {
					width: 180upx;
					height: 180upx;
					margin-top: 5upx;
				}
			}

			.title {
				font-size: 24rpx;
				display: inline-block;
				white-space: normal;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 2;
				overflow: hidden;
				height: 68upx;

			}

			.spec {
				font-size: 22upx;
				background: #CCCCCC;
				color: white;
				padding: 0 5upx;
				border-radius: 10upx;
				margin-top: 10upx;
				// display: inline-block;

			}

			.counter {
				position: absolute;
				bottom: 0upx;
				right: 10upx;
				display: flex;

				border: 2upx solid #CCCCCC;
				font-size: 28upx;
				align-items: center;
				border-radius: 10upx;

				.sub,
				.plus {
					// transform: scale(0.5);
					font-weight: 0;
					width: 42upx;
					text-align: center;

					&:active {
						background: rgba(0, 0, 0, .1);
					}
				}

				.num {
					text-align: center;
					font-size: 24upx;
					border-left: 2upx solid #EEEEEE;
					border-right: 2upx solid #EEEEEE;
					width: 50upx;
				}
			}
		}

	}

	.sxzg-icon {
		color: $primary-color;
		font-size: 16rpx;
		width: 80rpx;
		text-align: center;
		line-height: normal;
		border: 2rpx solid $primary-color;
		border-radius: 20rpx;
		display: inline-block;
		position: relative;
		top: -4rpx;
		margin-right: 10upx;
		// transform: scale(0.9);
	}

	.sxj-icon {
		background: $primary-color;
		color: white;
		font-size: 20upx;
		padding: 0 5upx;
		border-radius: 5upx;
		vertical-align: middle;
	}

	.row {
		display: flex;
		align-items: center;
	}

	.org-price {
		text-decoration: line-through;
		font-size: 26rpx;
		color: #cccccc;
	}

	.price {
		font-size: 26rpx;
		color: $primary-color;
		font-weight: bold;
	}

	.footer {
		display: flex;
		position: fixed;
		height: 100upx;
		background: $primary-color;
		bottom: 0;
		width: 100%;
		box-shadow: 0 0 10upx #999999;
		align-items: center;
		/* #ifdef H5 */
		bottom: 100upx;
		/* #endif */
		color: white;

		.select-all {
			width: 200upx;
			flex: 0 0 200upx;
			position: relative;
			margin-left: 20upx;

			.checkbox {
				transform: scale(0.7);
				position: relative;
				top: -4upx
			}

			/deep/ .uni-checkbox-input {
				border: none;
			}

			/deep/ .uni-checkbox-input:hover {
				border: none;
			}
		}

		.total {
			flex-grow: 1;
			text-align: right;
		}

		.settlement {
			flex-grow: 0;
			text-align: right;
			margin: 0 20upx;
		}

		.settlement-btn {
			background: #ffa82e;
			padding: 15upx 60upx;
			color: white;
			line-height: normal;
			font-size: 32upx;
			border-radius: 50upx;
		}
	}
</style>
