<template>
	<view class="page">
		<navbar title="下单支付" leftClick @leftClick="$utils.navigateBack" />

		<view class="box">
			<!-- 地址 -->
			<view class="address" @click="openAddress">
				<image src="../static/address/icon1.png" mode=""></image>
				<view class="">
					{{ address.name }}
				</view>
				<view class="">
					{{ address.addressDetail }}
				</view>
				<view class="icon">
					<uv-icon size="30rpx" name="arrow-right"></uv-icon>
				</view>
			</view>

			<view class="productList">
				<view class="item"
				:key="index"
				v-for="(item, index) in productList">
					<view class="item-image">
						<image :src="item.image || 'https://img95.699pic.com/photo/50058/1378.jpg_wh860.jpg'"
							mode="aspectFill"></image>
					</view>

					<view class="info">
						<view class="title">
							{{ item.title }}
						</view>
						<view class="desc">
							{{ item.sku }}
						</view>
						<view class="price">
							<view class="">
								<uv-number-box 
								v-model="num"
								></uv-number-box>
							</view>
							<view class="">
								￥{{ item.price }}
							</view>
						</view>
					</view>
				</view>
			</view>
			
			<view class="submit-box">
				<view class="peis">
					<view class="">
						配送方式
					</view>
					<view class="">
						商家自行配送
					</view>
				</view>
				<view class="priceInfo">
					<view class="">
						付款金额
					</view>
					<view class="totalPrice">
						￥{{ totalPrice }}
					</view>
				</view>
				<view class="remark">
					<input type="text" 
					placeholder="请输入备注"
					v-model="remark"/>
				</view>
			</view>
			
			
			<view class="uni-color-btn">
				确认下单
			</view>
		</view>




		<!-- 地址选择 -->
		<uv-popup ref="addressPopup" :round="30">
			<addressList ref="addressList" height="60vh" @select="selectAddress" />
		</uv-popup>

	</view>
</template>

<script>
	import addressList from '../components/address/addressList.vue'
	export default {
		components: {
			addressList,
		},
		data() {
			return {
				pids: [],
				productList: [],
				address: {
					name: '请选择地址',
					addressDetail: '',
				},
				addressTotal: 0,
				remark : '',
				num : 1,
			}
		},
		computed : {
			totalPrice(){
				let price = 0
				
				this.productList.forEach(n => {
					price += n.price * this.num
				})
				
				return Number(price).toFixed(2)
			},
		},
		onLoad({
			pid
		}) {
			this.pids = pid ? pid.split(',') : [],
			this.getRiceProductDetail()
		},
		onShow() {
			this.getAddressList()
		},
		methods: {
			// 获取商品
			getRiceProductDetail() {
				this.pids.forEach(id => {
					this.$api('getRiceProductDetail', {
						id
					}, res => {
						if (res.code == 200) {
							this.productList.push(res.result)
						}
					})
				})
			},
			// 打开
			getAddressList() {
				// 获取地址列表
				this.$refs.addressList.getAddressList().then(res => {
					this.addressTotal = res.total
					if (this.addressTotal != 0) {
						this.address = res.records[0]
					}
				})
			},
			// 打开选择地址
			openAddress() {
				if (this.addressTotal == 0) {
					return uni.navigateTo({
						url: '/pages_order/mine/address?type=back'
					})
				}
				this.$refs.addressPopup.open('bottom')
			},
			// 选择地址
			selectAddress(e) {
				this.address = e
				this.$refs.addressPopup.close()
			},
		}
	}
</script>

<style scoped lang="scss">
	.page {
		.box {
			padding: 20rpx;

			.address {
				display: flex;
				padding: 20rpx;
				background-color: #fff;
				align-items: center;
				border-radius: 20rpx;

				image {
					width: 40rpx;
					height: 40rpx;
					margin: 20rpx;
				}

				view {
					margin: 20rpx;
					overflow: hidden; //超出的文本隐藏
					text-overflow: ellipsis; //溢出用省略号显示
					white-space: nowrap; //溢出不换行
				}

				.icon {
					margin-left: auto;
				}
			}

			.productList {
				margin-top: 20rpx;
				background-color: #fff;
				border-radius: 20rpx;
				.item {
					padding: 10rpx 20rpx;
					align-items: center;
					margin-bottom: 20rpx;
					display: flex;
					width: 100%;
					box-sizing: border-box;
					.item-image {
						width: 140rpx;
						height: 140rpx;
						flex-shrink: 0;

						image {
							height: 100%;
							width: 100%;
							border-radius: 20rpx;
						}
					}

					.info {
						padding: 20rpx;
						color: #555;
						flex: 1;

						.title {
							font-size: 28rpx;
							font-weight: 900;
						}

						.desc {
							font-size: 22rpx;
							color: #777;
							margin-top: 10rpx;
						}

						.price {
							display: flex;
							justify-content: space-between;
							color: #f40;
							font-size: 30rpx;
							font-weight: 900;
						}
					}
				}
			}
			
			.submit-box{
				background-color: #fff;
				padding: 20rpx;
				border-radius: 20rpx;
				&>view:nth-child(2){
					margin-top: 20rpx;
				}
				&>view{
					display: flex;
					justify-content: space-between;
					align-items: center;
					font-size: 28rpx;
					&>view:nth-child(1){
						font-weight: 900;
					}
					&>view:nth-child(2){
						color: #999;
						font-size: 24rpx;
					}
					.totalPrice{
						color: #f40 !important;
						font-size: 34rpx !important;
						font-weight: 900;
					}
				}
				.remark{
					margin-top: 30rpx;
					input{
						background-color: #f3f3f3;
						padding: 14rpx 20rpx;
						border-radius: 20rpx;
						flex: 1;
					}
				}
			}
		}
	}
</style>