<template>
	<view class="cart-pages">
		<view class="cart-bar">
			<view>
				<u-image height="40rpx" width="40rpx" src="/static/position.png"></u-image>
			</view>
			<view class="position">
				 <view class="address">陕西省西安市未央区华帝金座A座6楼</view>
				 <view>王明 1456465455</view>
			</view>
			<view @click="editAddress">
				<u-image height="50rpx" width="50rpx" src="/static/edit.png"></u-image>
			</view>
		</view>
		<view class="goods-list">
			<!-- 商品列表 -->
			<view class="goods-item" v-for="(item,index) in goodsList" :key="item.id">
				<view class="goods-imgs">
					<u-image border-radius="12" width="158rpx" height="140rpx" :src="item.goods.homeImage"></u-image>
				</view>
				<view class="goods-info">
					<view class="good-name">{{item.goods.name}}</view>
					<view class="goods-price">
						<view>
							<text>{{item.goods.sellGold}}</text>
							<u-image mode="scaleToFill" width="30rpx" height="30rpx" src="/static/ledou.png"></u-image>
						</view>
						<view>×{{item.buyQty}}</view>
					</view>
				</view>
			</view>
			<!-- 总计 -->
			<view class="goods-heji">
				<view>
					<view>商品合计：</view>
					<view class="heji">
						<text>{{totalPrice}}</text>
						<u-image mode="scaleToFill" width="35rpx" height="35rpx" src="/static/ledou.png"></u-image>
					</view>
				</view>
				<view>
					<view>运费：</view>
					<view>免运费</view>
				</view>
			</view>
		</view>
		<view class="cart-submit">
			<view>
			  <view>总计：<text>{{totalPrice}}</text></view>
			  <u-image mode="scaleToFill" width="40rpx" height="40rpx" src="/static/ledou.png"></u-image>
			</view>
			<view>
				<u-button size="mini" :loading="btnLoading" :custom-style="toOrderButton" type="error" @click="toSave">确认支付</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import { saveOrder, signPay} from '@/api/order.js'
	import {
		findAddressBycustomerNo
	} from '@/api/receiveAddress.js'
	export default {
		data() {
			return {
				toOrderButton: {
					borderRadius:'100rpx',
					fontSize:'30rpx',
					width: '180rpx',
					height: '60rpx'
				},
				goodsList: [],
				userInfo: {
					receiveAddress: '陕西省西安市未央区华帝金座',
					receiverName: '王明',
					receiverPhone: '14564654551',
				},
				btnLoading: false  // 提交按钮加载圈
			};
		},
		onLoad() {
			this.goodsList = this.$store.state.vuex_toOrderList
			this.getLocation()
		},
		computed: {
			totalPrice() {
				let total = 0
				this.goodsList.map(item => {
					total = total + item.buyQty * item.goods.sellGold
				})
				return total 
			}
		},
		methods: {
			// 获取用户位置
			getLocation() {
				 findAddressBycustomerNo({customerNo:'12313'}).then(res => {
					 console.log(res)
				 })
			},
			//修改收货地址信息
			editAddress(){
				uni.navigateTo({
					url:"/pages/toOrder/editAddress"
				})
			},
			// 支付  保存订单
			toSave(){
				let orderGoodsList = []
				this.goodsList.map((item,index)=>{
					orderGoodsList[index] = {
						goodsNo: item.goodsNo,
						buyQty: item.buyQty
					}
				})
				this.btnLoading = true
				let params = Object.assign(this.userInfo,{orderGoodsList:orderGoodsList})
				console.log(orderGoodsList,params,'orderGoodsList')
				saveOrder(params).then(res=>{
					console.log(res,'rrrrrrr')
					if(res.status==200) {
						let id = res.data.id
						this.toPay(id)
					} else {
						this.btnLoading = false
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					}
				})
			},
			// 支付
			toPay(id) {
				signPay({id:id}).then(res=>{
					this.btnLoading = false
					if(res.status == 200) {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
						// 跳转到支付完成界面
						uni.navigateTo({
							url:""
						})
					} else{
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					}
				})
			}
		},
	}
</script>

<style lang="scss">
page{
	height: 100%;
}
.cart-pages{
	width: 100%;
	height: 100%;
	display: flex;
	flex-direction: column;
	.cart-bar{
		background: #FFFFFF;
		border-bottom: 1px solid #F8F8F8;
		border-top: 10upx solid #F8F8F8;
		display: flex;
		align-items: center;
		padding:15upx 20upx;
		.position{
			flex-grow: 1;
		}
		> view{
			padding: 10upx;
			.address{
				padding: 5upx 0;
			}
			&:last-child{
				padding: 0 20upx;
			}
		}
	}
	.goods-list{
		padding: 20upx 0;
		flex-grow: 1;
		overflow: auto;
		.goods-class-box{
			background: #FFFFFF;
			box-shadow: 1px 1px 3px #eee;
			margin-bottom: 20upx;
		}
		
		.goods-item{
			padding: 20upx 40upx;
			display: flex;
			align-items: center;
			border-bottom: 1px solid #F8F8F8;
			background: #fff;
			&:last-child{
				border: 0;
			}
			.goods-imgs{
				position: relative;
			}
			.goods-info{
				flex-grow: 1;
				padding-left: 20upx;
				.good-name{
					font-weight: bold;
					word-break: break-all;
				}
			}
			.goods-price{
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding-top: 20upx;
				>view{
					&:first-child{
						display: flex;
						align-items: center;
						text{
							color: red;
							font-size: 35upx;
							margin-right: 6upx;
							font-weight: bold;
						}
					}
				}
			}
		}
	}
	.goods-heji{
		padding: 20upx 40upx;
		background: #FFFFFF;
		box-shadow: 1px 1px 3px #eee;
		margin-top: 20upx;
		>view{
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 10upx 0;
			.heji{
				display: flex;
				align-items: center;
				text{
					color: red;
					margin-right: 6upx;
					font-size: 35upx;
					font-weight: bold;
				}
			}
		}
	}
	.cart-submit{
		padding: 20upx 30upx;
		background: #FFFFFF;
		box-shadow: 1px 1px 3px #eee;
		border-top: 1px solid #eee;
		display: flex;
		align-items: center;
		justify-content: space-between;
		> view{
			&:first-child{
				display: flex;
				align-items: center;
				text{
					color: red;
					font-size: 40upx;
					margin-right: 6upx;
					font-weight: bold;
				}
			}
		}
	}
}
</style>
