<template>
	<view class="content">
		<view class="box">
			<block v-if="address_id == ''">
				<view class="hflex acenter jbetween" @click="addAddr">
					<view class="title">新增收货地址</view>
					<u-icon name="arrow-right" color="#ABABAB" size="11"></u-icon>
				</view>
			</block>
			<block v-else>
				<view class="hflex acenter jbetween" @click="addAddr">
					<view class="hflex acenter">
						<u-icon name="map-fill" color="#506DFF" size="22"></u-icon>
						<view class="vflex" style="padding-left: 16rpx;">
							<view class="hflex acenter">
								<view class="text_style1" style="padding-right: 20rpx;">{{address.name}}</view>
								<view class="text_style1">{{address.phone}}</view>
							</view>
							<view class="text_style2">{{address.address}}</view>
						</view>
					</view>
					<u-icon name="arrow-right" color="#ABABAB" size="11"></u-icon>
				</view>
			</block>
		</view>
		<view class="box">
			<view class="hflex acenter box_item">
				<view>
					<image :src="pageData.goods_item[0].goods_cover" mode="aspectFill" class="img"></image>
				</view>	
				<view class="img_right">
					<view class="hflex acenter jbetween">
						<view class="name text_hide">{{pageData.goods_item[0].goods_name}}</view>
						<view class="price">￥{{pageData.goods_item[0].price_selling}}</view>
					</view>
					<view class="hflex jbetween" style="padding-top: 12rpx;">
						<view class="norm text_hide">{{pageData.goods_item[0].goods_spec || ''}}</view>
						<view class="vflex">
							<view class="num">X{{pageData.goods_item[0].stock_sales}}</view>
						</view>
					</view>
				</view>
			</view>
			<view class="hflex acenter jbetween">
				<view class="title">订单备注</view>
				<u-input v-model="remarks" inputAlign="right" border="none" placeholder="建议留言前先与商家沟通确认"></u-input>
			</view>
			<!-- <view class="scroll_img" v-else>
				<scroll-view scroll-x="true" style="white-space: nowrap;" class="imgs hflex">
					<block v-for="(item2,index2) in item.goods_item" :key="index2">
						<view class="imgs_item">
							<image :src="item2.goods_cover" class="img"></image>
						</view>
					</block>
				</scroll-view>
				<view class="img_leng">共{{item.goods_item.length}}件</view>
			</view> -->
		</view>
		<!-- <view class="hflex acenter jend cell" v-if="item.payment_status == 2">
			<view class="price">实付款：￥{{item.payment_amount}}</view>
		</view> -->
		
		
		<view class="box">
			<view class="hflex acenter jbetween">
				<view class="left_text">商品金额</view>
				<view class="right_text">￥{{pageData.payment_amount}}</view>
			</view>
			<!-- <view class="hflex acenter jbetween cell2">
				<view class="left_text">运费</view>
				<view class="right_text">￥{{freight}}</view>
			</view> -->
			<view class="hflex acenter jend item_bottom">
				<view class="title" style="font-size: 22rpx;">合计：<span class="text_red">￥{{pageData.payment_amount}}</span></view>
			</view>
		</view>
		<view class="box hflex acenter jbetween" style="margin-bottom: 186rpx;">
			<view class="hflex acenter">
				<image src="/static/images/shop/wx_pay.png" class="pay_icon"></image>
				<view class="text_style1">微信支付</view>
			</view>
			<u-radio-group>
				<u-radio activeColor="#506DFF"></u-radio>
			</u-radio-group>
		</view>
		<view class="bottom hflex acenter jend">
			<view class="title" style="font-size: 22rpx;" v-if="pageList.length == 0">合计：<span class="text_blue">￥{{pageData.payment_amount}}</span></view>
			<view class="title" style="font-size: 22rpx;" v-else>合计：<span class="text_blue">￥{{pageData.payment_amount}}</span></view>
			<view class="btn" @click="submit">提交订单</view>
		</view>
	</view>
</template>

<script>
	import $api from '@/static/js/api.js'
	var that = ''
	export default {
		data() {
			return {
				address: {
				},
				pageData: {},
				pageList: [],
				order_id: '',
				address_id: '',
				pay_no:'',
				total: 0,
				freight: '0.00',
				item_id: '',
				cart_ids: '',
				buy_num: 1,
				remarks: '',
			}
		},
		onLoad(options) {
			that = this
			console.log(options);
			that.pageData = JSON.parse(options.data)
			// that.cart_ids = options.cart_ids
			that.item_id = options.item_id
			// that.address_id = uni.getStorageSync('address_id')
		},
		onShow() {
			that.getAddr()
		},
		methods: {
			getData() {
				$api.req({
					url: '/data/api.Order/order_info',
					method: 'POST',
					data: {
						order_id: that.order_id,
					}
				}, function(res) {
					if(res.code == 1) {
						$api.info(res.info)
						that.pageData = res.data
					}
				})
			},
			count() {
				for(var i=0;i<=that.pageList.length;i++) {
					for(var j=0;j<=that.pageList[i].goods_item.length;j++) {
						if(that.pageList[i].goods_item[j].num) {
							that.total += Number(that.pageList[i].goods_item[j].price_selling) * Number(that.pageList[i].goods_item[j].num)
						} else {
							that.total += Number(that.pageList[i].goods_item[j].price_selling) * Number(that.pageList[i].goods_item[j].stock_sales)
						}
					}
				}
				that.total = that.total.toFixed(2)
			},
			getAddr() {
				that.address_id = uni.getStorageSync('address_id')
				if(that.address_id == '') {
					return
				}
				$api.req({
					url: '/data/api.User/address_list'
				}, function(res) {
					if(res.code == 1) {
						for(var i=0;i<res.data.data.length;i++) {
							if(res.data.data[i].id == that.address_id) {
								that.address = res.data.data[i]
							}
						}
					}
				})
			},
			addAddr() {
				$api.jump('/page_shop/pages/address/list?select=true')
			},
			submit() {
				if(!that.address_id) {
					$api.info("请先选择地址")
					return
				}
				var openid = uni.getStorageSync('openid1')
				/* if(!openid) {
					$api.info("登录验证有误，请重新登录")
					return
				} */
				if(that.item_id) {
					$api.req({
						url: '/data/api.Order/order_create',
						method: 'POST',
						data: {
							admin_id: that.pageData.admin_id,
							item_id: that.item_id,
							num: that.pageData.goods_item[0].stock_sales,
							address_id: that.address_id,
							order_remark: that.remarks
						}
					}, function(res) {
						if(res.code == 1) {
							that.order_id = res.data.order_id
							that.pay_no = res.data.pay_no
							$api.req({
								url: '/data/api.auth.Center/channel',
								method: 'POST',
								data: {
									type: 2,
									from: 1
								}
							}, function(res) {
								if(res.code == 1) {
									var pay_code = res.data[0].code
									$api.req({
										url: '/data/api.Order/order_pay',
										method: 'POST',
										data: {
											pay_code: pay_code,
											pay_no: that.pay_no,
											type: 'wechat'
										}
									}, function(res) {
										if(res.code == 1) {
											wx.requestPayment({
											  timeStamp: res.data.pay.timeStamp,
											  nonceStr: res.data.pay.nonceStr,
											  package: res.data.pay.package,
											  signType: 'MD5',
											  paySign: res.data.pay.paySign,
											  success (res) { 
												$api.jump('/page_shop/pages/order/paySuccess?id=' +that.order_id ,2)
											  },
											  fail (res) { }
											})
										}
									})
								}
							})
						}
					})
				} else {
					$api.req({
						url: '/data/api.Order/crat_create',
						method: 'POST',
						data: {
							cart_ids: that.cart_ids,
							address_id: that.address_id,
							order_remark: that.remarks
						}
					}, function(res) {
						if(res.code == 1) {
							that.pay_no = res.data
							$api.req({
								url: '/data/api.auth.Center/channel',
								method: 'POST',
								data: {
									type: 2,
									from: 1
								}
							}, function(res) {
								if(res.code == 1) {
									var pay_code = res.data[0].code
									$api.req({
										url: '/data/api.Order/order_pay',
										method: 'POST',
										data: {
											pay_code: pay_code,
											pay_no: that.pay_no,
											type: 'wechat'
										}
									}, function(res) {
										if(res.code == 1) {
											wx.requestPayment({
											  timeStamp: res.data.pay.timeStamp,
											  nonceStr: res.data.pay.nonceStr,
											  package: res.data.pay.package,
											  signType: 'MD5',
											  paySign: res.data.pay.paySign,
											  success (res) { 
												$api.jump('/page_shop/pages/order/paySuccess?id=' +that.order_id ,2)
											  },
											  fail (res) { }
											})
										}
									})
								}
							})
						}
					})
				}
				
			}
		},
	}
</script>

<style lang="scss" scoped>
	.content::v-deep {
		background-color: #F4F4F4;
		
		.box {
			background: #FFFFFF;
			border-radius: 10px;
			margin: 20rpx auto 0;
			width: 690rpx;
			box-sizing: border-box;
			padding: 34rpx 20rpx;
			.title {
				font-size: 32rpx;
				font-weight: 500;
				color: #222222;
				line-height: 44rpx;
			}
			.text_style1 {
				font-size: 28rpx;
				font-weight: 500;
				color: #222222;
				line-height: 40rpx;
			}
			.text_style2 {
				font-size: 26rpx;
				font-weight: 400;
				color: #888888;
				line-height: 36rpx;
			}
			.cell {
				padding: 20rpx 0 32rpx;
			}
			.cell2 {
				padding: 32rpx 0 20rpx;
			}
			.img {
				width: 160rpx;
				height: 160rpx;
				border-radius: 24rpx;
			}
			.img_right {
				width:calc(100% - 160rpx);
				padding-left: 20rpx;
				.item_name {
					font-size: 30rpx;
					font-weight: 400;
					color: #222222;
					line-height: 42rpx;
				}
				.norm {
					max-width: 288rpx;
					width: max-content;
					background: #F5F5F5;
					border-radius: 12rpx;
					box-sizing: border-box;
					padding: 12rpx 12rpx 5rpx;
					font-size: 20rpx;
					font-weight: 400;
					color: #888888;
					line-height: 28rpx;
					text-overflow: ellipsis;
					overflow: hidden;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					box-orient: vertical;
					line-clamp: 2;
					-webkit-line-clamp: 2;
				}
				
			}
			.left_text {
				font-size: 24rpx;
				font-weight: 500;
				color: #888888;
				line-height: 28rpx;
			}
			.right_text {
				font-size: 24rpx;
				font-weight: bold;
				color: #222222;
				line-height: 28rpx;
			}
			.item_bottom {
				padding-top: 22rpx;
				border-top: 1rpx solid #F4F4F4;
			}
			.text_red {
				font-weight: bold;
				color: #EE5850;
			}
			.pay_icon {
				width: 48rpx;
				height: 48rpx;
				margin-right: 20rpx;
			}
			.check_icon {
				width: 32rpx;
				height: 32rpx;
			}
			.box_item {
				background: #FFFFFF;
				border-radius: 20rpx;
				width: 100%;
				margin-top: 20rpx;
				box-sizing: border-box;
				padding: 0 20rpx;
				.cell {
					padding: 20rpx 0;
				}
				.title {
					font-size: 30rpx;
					font-weight: 500;
					color: #333333;
					line-height: 42rpx;
				}
				.text-blue {
					font-size: 26rpx;
					font-weight: 400;
					color: #506DFF;
					line-height: 36rpx;
				}
				.img {
					width: 160rpx;
					height: 160rpx;
					border-radius: 16rpx;
					
				}
				.img_right {
					margin-left: 16rpx;
					width: calc(100% - 176rpx);
					.name {
						width: 392rpx;
						font-size: 28rpx;
						font-weight: 500;
						color: #222222;
						line-height: 40rpx;
					}
					
					.norm {
						width: max-content;
						// height: max-content;
						max-width: 400rpx;
						// height: 84rpx;
						background: #F5F5F5;
						border-radius: 4rpx;
						font-size: 22rpx;
						font-weight: 400;
						color: #888888;
						line-height: 32rpx;
						box-sizing: border-box;
						padding: 12rpx;
					}
					.num {
						font-size: 24rpx;
						font-weight: 500;
						color: #888888;
						line-height: 28rpx;
					}
					.text_red {
						font-size: 26rpx;
						font-weight: 400;
						color: #FF2626;
						line-height: 36rpx;
						padding-top: 18rpx;
					}
					
				}
				.scroll_img {
					position: relative;
					.imgs {
						width: 100%;
						height: 170rpx;
						overflow: hidden;
						white-space: nowrap;
						.imgs_item {
							width: 160rpx;
							height: 160rpx;
							border-radius: 16rpx;
							margin-right: 20rpx;
							display: inline-block;
						}
						
					}
					.img_leng {
						position: absolute;
						right: 0;
						top: 0;
						width: 130rpx;
						height: 160rpx;
						border-radius: 12rpx;
						font-size: 24rpx;
						font-weight: 500;
						color: #222222;
						line-height: 160rpx;
						text-align: center;
						background: linear-gradient(270deg, #FFFFFF 0%, rgba(255,255,255,0.78) 100%);
					}
				}
				
				.price {
					font-size: 30rpx;
					font-weight: 400;
					color: #222222;
					line-height: 24rpx;
				}
				.btn1 {
					width: 180rpx;
					height: 68rpx;
					border-radius: 36rpx;
					border: 1px solid #D0D0D0;
					text-align: center;
					font-size: 28rpx;
					font-weight: 400;
					color: #222222;
					line-height: 68rpx;
				}
				.btn2 {
					border: 1px solid #506DFF;
					color: #506DFF;
					margin-left: 32rpx;
				}
			}
		}
		.u-radio-group {
			flex: 0 !important;
		}
		.bottom {
			width: 100%;
			height: 166rpx;
			background: #FFFFFF;
			z-index: 9;
			position: fixed;
			bottom: 0;
			box-sizing: border-box;
			padding: 8rpx 50rpx 74rpx;
			.title {
				font-size: 24rpx;
				font-weight: 400;
				color: #222222;
				line-height: 34rpx;
			}
			.text_blue {
				font-size: 44rpx;
				color: #5270FF;
			}
			.btn{
				margin-left: 16rpx;
				width: 260rpx;
				height: 84rpx;
				background: #506DFF;
				border-radius: 42rpx;
				text-align: center;
				font-size: 36rpx;
				font-weight: 500;
				color: #FFFFFF;
				line-height: 84rpx;
			}
		}
	}
</style>