<template>
	<view class="content">
		<scroll-view scroll-y="true" class="scroll-view">
			<view class="list-scroll-view">
				<view class="one">
					<view class="one_1">
						<swiper class="swiper" circular :indicator-dots="true" indicator-color="#ffffff"
							indicator-active-color="#007AFF" :interval="3000" :duration="1000">
							<swiper-item class="list" v-for="(item,index) in fileList" :key="index">
								<image class="image" :src="item.url" mode="aspectFit">
								</image>
							</swiper-item>
						</swiper>
					</view>
					<view class="one_2">
						<text class="money"><text>¥</text>{{info.money||0}}</text>
					</view>
					<view class="one_3">
						<text class="num">已售{{info&&info.sell_num||0}}件</text>
					</view>
					<view class="one_4">
						<view class="name">
							{{info&&info.name}}
						</view>
						<view class="brief">
							{{info&&info.brief||''}}
						</view>
					</view>
					<view class="one_5">
						<view class="list" v-for="(item,index) in fileList" :key="index">
							<image class="image" :src="item.url" mode="aspectFit">
							</image>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
		<view class="bottom">
			<uni-goods-nav :fill="true" :options="options" :button-group="buttonGroup" @click="onClick"
				@buttonClick="buttonClick" style="margin-top: 20px;" />
		</view>
		<!-- 规格弹窗 -->
		<uni-popup ref="popup" background-color="#fff" type="bottom" :is-mask-click="false">
			<view class="popup">
				<view class="close">
					<text @click="toClose" class="iconfont icon-icon-cross-empty"></text>
				</view>
				<!-- 规格 -->
				<view class="specs_1" v-if="popupShow=='1'">
					<view class="info_1">
						<scroll-view scroll-y="true" class="scroll-view">
							<view class="list-scroll-view">
								<view class="one">
									<view class="one_1">
										<view class="l">
											<image v-if="specsInfo.file" class="image"
												:src="specsInfo.file?specsInfo.file[0].url:''" mode="">
											</image>
											<image v-else class="image" :src="info.file?info.file[0].url:''" mode="">
											</image>
										</view>
										<view class="r">
											<view class="money">
												<text class="money_1"><text>¥</text>{{specsInfo.money}}</text>
											</view>
											<view class="other_1">
												<text>已选：</text>
												<text>{{specsInfo.name||'暂无'}}</text>
											</view>
											<view class="other_1">
												<text>库存：</text>
												<text>{{specsInfo.num||'0'}}</text>
											</view>
										</view>
									</view>
									<view class="one_2">
										<view class="one_2_1">
											规格
										</view>
										<view class="one_2_2">
											<view
												:class="['list',is_specs!=index&&item.num>0?'list':is_specs!=index&&item.num<=0?'huilist':is_specs==index&&item.num>0?'redlist':is_specs==index&&item.num<=0?'huiilist':'list']"
												v-for="(item,index) in specList" :key="index" @tap="toSpecs(item)">
												<text>{{item.name}}</text>
											</view>
										</view>
									</view>
									<view class="one_3">
										<view class="one_3_1">
											<text>数量</text>
										</view>
										<view class="one_3_1">
											<uni-number-box v-model="buy_num" @change="toCount" :min="1"
												:max="specsInfo.num" />
										</view>
										<view class="one_3_1">
											<text>库存{{specsInfo.num||0}}</text>
										</view>
									</view>
								</view>
							</view>
						</scroll-view>
					</view>
					<view class="info_2">
						<button :disabled="is_zero" class="button" @tap="toMarket">加入购物车</button>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 商品id
				id: '',
				// 当前用户信息
				user: {},
				// 商品详情
				info: {},
				// 轮播图
				fileList: [],
				// 规格弹框
				popupShow: '1',
				// 规格数组
				specList: [],
				specsInfo: {},
				// 规格信息
				is_specs: 0,
				// 购买数量
				buy_num: 1,
				is_zero: false,
				// 底部商品导航
				options: [{
						icon: 'shop',
						text: '店铺',
						type: '0'
					},
					{
						icon: 'cart',
						text: '购物车',
						infoBackgroundColor: '#007aff',
						infoColor: "#f5f5f5"
					}
				],
				buttonGroup: [{
					text: '加入购物车',
					backgroundColor: 'linear-gradient(90deg, #1E83FF, #0053B8)',
					color: '#fff'
				}]
			}
		},
		onLoad: async function(e) {
			const that = this;
			that.$set(that, `id`, e.id || '');
			that.searchToken();
			await that.search();
		},
		onShow() {
			const that = this;
			that.searchOther()
		},
		methods: {
			searchToken() {
				const that = this;
				try {
					const res = uni.getStorageSync('token');
					if (res) that.$set(that, `user`, res);
				} catch (e) {
					uni.showToast({
						title: err.errmsg,
						icon: 'error',
						duration: 2000
					});
				}
			},
			// 查询商品详情
			async search() {
				const that = this;
				let res;
				res = await that.$api(`/Good/${that.id}`, 'GET', {})
				if (res.errcode == '0') {
					const arr = await that.$api(`/Specs`, 'GET', {
						goods: res.data._id,
						is_use: '0'
					})
					if (arr.errcode == '0') {
						let data = arr.data.sort((a, b) => {
							return a.money - b.money
						})
						if (data) {
							res.data.money = data[0].money
							that.$set(that, `specsInfo`, data[0]);
							that.$set(that, `specList`, data);
						}
						that.$set(that, `info`, res.data);
						that.$set(that, `fileList`, res.data.file);
					}
				}
				res = await that.$api(`/Cart/num`, 'GET', {
					user: that.user._id
				})
				if (res.errcode == '0') that.$set(that.options[1], `info`, res.data);
			},
			// 点击店铺或者购物车
			onClick(e) {
				const that = this;
				if (e.content.text == '购物车') {
					uni.reLaunch({
						url: '/pages/market/index'
					})
				} else {
					uni.navigateTo({
						url: `/pagesGoods/shop/index?id=${that.info.supplier_id}`
					})
				}
			},
			// 加入购物车
			buttonClick(e) {
				const that = this;
				that.$set(that, `popupShow`, '1')
				that.$refs.popup.open();
			},
			// 加入购物车
			async toMarket(e) {
				const that = this;
				let user = that.user;
				let info = that.info;
				let specsInfo = that.specsInfo;
				if (user && user._id) {
					if (user.role == 'jdry' || user.role == 'sqry') {
						let res;
						let obj = {
							user: user._id,
							supplier_id: info.supplier_id,
							spec: specsInfo._id,
							goods: info._id,
							num: that.buy_num,
							money: specsInfo.money,
							total_money: that.$multiply(specsInfo.money, that.buy_num)
						}
						res = await that.$api(`/Cart`, 'POST', obj)
						if (res.errcode == '0') {
							uni.showToast({
								title: `加入购物车成功`,
								icon: 'none'
							})
							that.toClose();
						} else {
							uni.showToast({
								title: res.errmsg,
								icon: 'none'
							})
						}
					} else {
						uni.showToast({
							title: '角色不匹配 无法加入购物车',
							icon: 'none'
						})
					}
				} else {
					uni.navigateTo({
						url: `/pages/login/index`
					})
				}
			},
			// 加数量
			async toCount(e) {
				const that = this;
				that.$set(that, `buy_num`, e)
			},
			// 选择规格
			async toSpecs(e) {
				const that = this;
				let specs = that.specList;
				let dataIndex = specs.findIndex(i => i._id == e._id);
				that.$set(that, `is_specs`, dataIndex);
				that.$set(that, `specsInfo`, e)
				that.$set(that, `buy_num`, 1);
				if (e.num <= 0) that.$set(that, `is_zero`, true)
				else that.$set(that, `is_zero`, false)
			},
			async searchOther() {
				const that = this;
				const res = await that.$api(`/Cart/num`, 'GET', {
					user: that.user._id
				})
				if (res.errcode == '0') that.$set(that.options[1], `info`, res.data);
			},
			toClose() {
				const that = this;
				that.searchOther()
				that.$refs.popup.close();
			},
		}
	}
</script>

<style lang="scss">
	.content {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;

		.one {
			position: relative;
			flex-grow: 1;

			.one_1 {
				border-bottom: 0.5vw solid var(--f9Color);

				swiper {
					height: 44vh !important;
				}

				.list {
					border-radius: 5px;

					.image {
						width: 100%;
						height: 100%;
						border-radius: 5px;
						background-color: #fff;
					}
				}
			}

			.one_2 {
				border-bottom: 0.5vw solid var(--f9Color);
				padding: 2vw;

				.money {
					font-size: var(--font20Size);
					padding: 0 1vw 0 0;
					color: var(--fF0Color);
					font-weight: bold;

					text {
						font-size: var(--font14Size);
					}
				}
			}

			.one_3 {
				display: flex;
				flex-wrap: wrap;
				padding: 1vw;
				border-bottom: 0.5vw solid var(--f9Color);
			}


			.one_4 {
				border-bottom: 0.5vw solid var(--f9Color);
				padding: 2vw;

				.name {
					width: 100%;
					overflow: hidden;
					text-overflow: ellipsis;
					word-break: break-all;
					font-size: var(--font17Size);
					font-weight: bold;
					margin: 0 0 2vw 0;
				}

				.brief {
					font-size: var(--font14Size);
					color: var(--f85Color);
					margin: 0 0 1vw 0;
				}
			}

			.one_5 {
				text-align: center;
				margin: 0 0 15vw 0;

				.list {

					.image {
						border-radius: 5px;
						background-color: #fff;
					}
				}
			}
		}

		.bottom {
			width: 100vw;
			position: fixed;
			bottom: 0;
			left: var(--window-left);
			right: var(--window-right);
		}
	}

	.scroll-view {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;

		.list-scroll-view {
			display: flex;
			flex-direction: column;
		}
	}

	.uni-popup {
		z-index: 9999 !important;
	}

	.popup {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 60vh;

		.close {
			text-align: right;
			padding: 2vw;
		}

		.specs_1 {
			position: relative;
			display: flex;
			flex-direction: column;
			height: 54vh;

			.info_1 {
				position: relative;
				flex-grow: 1;

				.one {
					.one_1 {
						display: flex;
						margin: 0 0 2vw 0;
						padding: 0 0 2vw 2vw;
						border-bottom: 0.5vw solid var(--f9Color);

						.l {
							width: 25vw;
							height: 25vw;

							.image {
								width: 100%;
								height: 100%;
								border-radius: 5px;
							}
						}

						.r {
							width: 70vw;
							padding: 0 0 0 2vw;

							.money {
								margin: 0 0 2vw 0;

								.money_1 {
									font-size: var(--font20Size);
									color: var(--fF0Color);
									padding: 0 2vw 0 0;

									text {
										font-size: var(--font14Size);
									}
								}
							}

							.other_1 {
								font-size: var(--font15Size);
								color: var(--f85Color);

								text:last-child {
									color: var(--f00Color);
								}
							}
						}
					}

					.one_2 {
						margin: 0 0 2vw 0;
						border-bottom: 0.5vw solid var(--f9Color);

						.one_2_1 {
							font-size: var(--font14Size);
							margin: 0 0 2vw 0;
						}

						.one_2_2 {
							display: flex;
							flex-wrap: wrap;

							.list {
								background-color: var(--f5Color);
								margin: 0 2vw 2vw 0;
								padding: 0.5vw 1vw;
								border-radius: 5px;

								text {
									font-size: var(--font14Size);
									color: var(--f00Color);
								}
							}

							.huilist {
								background-color: var(--fDCColor);

								text {
									color: var(--f85Color);
								}
							}

							.redlist {
								background-color: var(--f3CColor);

								text {
									color: var(--mainColor);
								}
							}

							.huiilist {
								background-color: var(--f80Color);

								text {
									color: var(--fcColor);
								}
							}
						}
					}

					.one_3 {
						display: flex;

						.one_3_1 {
							margin: 0 2vw 0 0;

							text {
								font-size: 14px;
								color: var(--f85Color);
							}

							.limit {
								color: var(--fF0Color);
								padding: 0 0 0 2vw;
							}
						}
					}
				}
			}

			.info_2 {

				.button {
					background-color: var(--f3CColor);
					color: var(--mainColor);
					border-radius: 0;
				}
			}
		}
	}
</style>
