<template>
	<view class="page">
		<view class="box-info">
			<view class="title margin-bottom">兑换详情</view>
			<view class="flex top">
				<image :src="detail.goodsCover" style="width: 140rpx;height: 140rpx;"></image>
				<view class="flex-1">
					<view class="title" style="margin-bottom: 60rpx;">{{ detail.goodsName}}</view>
					<view class="flex justify-between" style="width: 100%;">
						<view class="flex align-center">
							<view class="cuIcon-move btn" @click="handleMove"></view>
							<text>{{ form.goodsNum }}</text>
							<view class="cuIcon-add btn" @click="handleAdd"></view>
						</view>
						<view class="flex align-center">
							<text class="cuIcon-rechargefill" style="color: #FEE034;margin-right:12rpx;font-size: 36rpx;"></text>
							{{detail.redeemedNum}}
						</view>
					</view>
				</view>
			</view>
			<view>
				<view style="padding: 40rpx 0 20rpx;">
					<view class="flex align-center justify-end margin-bottom">合计：<text class="cuIcon-rechargefill" style="color: #FEE034;margin-right:12rpx;font-size: 36rpx;">
					</text>{{ totalNum}}</view>
					<view class="flex align-center justify-end" style="font-size: 20rpx;color: #5E5F60;">
						<image src="/static/info-circle.png" style="width: 40rpx;height: 40rpx;"></image>
						兑换取消后啤酒币不可恢复
					</view>
				</view>
				
			</view>
		</view>
		<view class="fun-box">
			<view class="left">
				<view class="flex align-center">{{ totalNum}}
					<image src="@/static/beerCoin.png" style="width: 40rpx;height: 40rpx;margin-left: 20rpx;">
					</image>
				</view>
				<!-- <view class="flex align-center">当前可用品牌币：{{ coinBalance.balance}}<image src="@/static/beerCoin.png"
									style="width: 30rpx;height: 30rpx;margin-left: 20rpx;">
								</image>
							</view> -->

			</view>
			<view class="btn" @click="handleRedeem">
				确认兑换
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getGoodsInfo,
		createOrder
	} from "@/api/bar.js"
	export default {
		data() {
			return {
				goodsId: '',
				detail: {},
				
				// totalNum: 0,
				form: {
					goodsNum: 1,
					goodsId: '',
				}
			};
		},
		onLoad({
			goodsId
		}) {
			this.goodsId = goodsId
			this.form.goodsId = goodsId
			this.getDetailFun()
			// this.getBreweryInfoFun()
			// this.getBreweryCoinBalanceFun()
		},
		computed: {
			totalNum() {
				return this.form.goodsNum * this.detail.redeemedNum
				},
			},
		methods: {
			// 获取详情
			getDetailFun() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				getGoodsInfo(this.goodsId).then(res => {
					uni.hideLoading()
					if (res.code == 200) {
						this.detail = res.data
					}
				}).catch(err => {
					uni.hideLoading()
				})
			},
			handleMove() {
				if (this.form.goodsNum > 0) {
					this.form.goodsNum--
					// this.totalNum = this.form.goodsNum * this.detail.redeemedNum
				}
			},
			handleAdd() {
				this.form.goodsNum++
				// this.totalNum = this.form.goodsNum * this.detail.redeemedNum
			},
			handleRedeem() {
				if(this.form.goodsNum == 0) {
					uni.showToast({
							title: '请选择兑换数量',
							icon: 'none'
						})
					return
				}
				uni.showModal({
					title: '提示',
					content: '确认兑换？',
					success: (res) => {
						if (res.confirm) {
							uni.showLoading({
								mask: true
							})
							createOrder(this.form).then(res => {
								if (res.code == 200) {
									uni.hideLoading()
										uni.showToast({
											title: '兑换成功',
											icon: 'none'
										})
									uni.redirectTo({
										url:'/pagesCoin/orderInfo?orderId=' + res.data.orderId + '&goodsId=' + this.goodsId
									})
								}
							})
						} else if (res.cancel) {
							return
						}
					}
				})
				
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		background: #F2F2F2;
		padding: 32rpx;

		.box-info {
			background: #FFFFFF;
			padding: 40rpx 32rpx;
			border-radius: 20rpx;
			.title {
				font-size: 32rpx;
				font-weight: 500;
				color: #0B0E26;
				margin-left: 20rpx;
			}
			.top {
				padding: 20rpx 0;
				border-bottom: 1px dashed #D8D8D8;
				.btn {
					width: 50rpx;
					height: 50rpx;
					border-radius: 6rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 36rpx;
					color: #000000;
					margin: 0 20rpx;
					border: 1px solid #979797;
				}
			}
		}

		.fun-box {
			position: fixed;
			left: 0;
			width: 100%;
			bottom: 0;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 40rpx 32rpx 40rpx 64rpx;
			padding-bottom: calc(24rpx + constant(safe-area-inset-bottom));
			padding-bottom: calc(24rpx + env(safe-area-inset-bottom));

			.btn {
				width: 344rpx;
				height: 82rpx;
				background: #39E5B1;
				border-radius: 12rpx;
				font-size: 32rpx;
				color: #0B0E26;
				text-align: center;
				line-height: 82rpx;
			}
		}
	}
</style>