<template>
	<view >
		<i-loading v-if="loading || address_loading"></i-loading>
		<view class="flex align-center justify-center bg-white p-3" @click="toAddress" v-if="!address_id">
			<view class="text-center">
				<uni-icons type="plusempty" size="20" color="#999999"></uni-icons>
				<view class="text-muted font28 mt-2">请选择收货地址</view>
			</view>
		</view>
		<view class="bg-white p-3 flex align-center" @click="toAddress" v-else>
			<uni-icons type="location" size="20" color="#333333"></uni-icons>
			<view class="flex-1 mx-2">
				<view class="flex align-center font32">
					<text>{{address.user_name}}</text>
					<text class="ml-3">{{address.user_phone}}</text>
				</view>
				<view class="mt-1 font30 line15">{{address.region_prov}}{{address.region_city}}{{address.region_area}}{{address.region_addr}}</view>
			</view>
			<uni-icons type="right" size="16" color="#999999"></uni-icons>
		</view>
		<view class=" mt-2 bg-white">
			<view class="flex align-center justify-between p-3 border-bottom">
				<text class="font28 text-muted">订单编号</text>
				<view class="">{{order.order_no}}</view>
			</view>
		</view>
		<view class=" mt-2 bg-white">
			<view class="flex align-center justify-between p-3 border-bottom">
				<text class="font28 text-muted">商品积分</text>
				<view class="cny_jf font-weight-bold">{{order.amount_goods}}</view>
			</view>
		</view>
		<view class="mt-2 bg-white p-3" v-if="false">
			<view class="font30 mr-2">备注信息</view>
			<textarea placeholder="请输入备注信息" class="font30 mt-2 w-100" style="height: 100rpx;"></textarea>
		</view>
		<view class="cu-tabbar-height"></view>
		<view class="fixed-bottom bg-white py-2 border-top">
			<view class="flex align-center justify-between px-3">
				<view class="flex align-center font32">
					<text class="text-muted">消耗：</text>
					<view class="cny_jf font-weight-bold">{{order.amount_total}}</view>
				</view>
				<view class="bg-danger text-white px-4 rounded-circle py-2 font30" @click="toPay">立即兑换</view>
			</view>
		</view>
	</view>
</template>

<script>
import { TaAjax } from '../../common/ajax'

	export default {
		data() {
			return {
				address_loading: false,
				address_id: 0,
				address: {},
				loading: false,
				order_no: "",
				order: {},
			}
		},
		onLoad(e) {
			this.order_no = e.order_no
			if (!this.order_no) {
				uni.showToast({
					title: '参数错误',
					icon: 'none',
					complete() {
						uni.navigateBack()
					}
				})
			}
			this.address_id = e.address_id
			if (this.address_id) {
				this.loadAddress()
			}
			this.loadData()
		},
		methods: {
			loadData() {
				this.loading = true
				TaAjax.get('/points_mall/api.auth.order/get', {
					order_no: this.order_no,
				}).then(res => {
					const data = res.data
					this.order = data.list[0]
				}).finally(() => {
					this.loading = false
				})
			},
			loadAddress() {
				this.address_loading = true
				TaAjax.get('/points_mall/api.auth.address/info', {
					id: this.address_id,
				}).then(res => {
					this.address = res.data
				}).finally(() => {
					this.address_loading = false
				})
			},
			toPay(){
				if (!this.address_id) {
					return uni.showToast({
						title: '请选择收货地址',
						icon: 'none',
					})
				}
				uni.showLoading({
					title: '兑换中',
				})
				TaAjax.post('/points_mall/api.auth.order/perfect', {
					order_no: this.order_no,
					address_id: this.address_id,
				}).then(res => {
					if (res.code == 1) {
						TaAjax.post('/points_mall/api.auth.order/payment', {
							order_no: this.order_no,
						}).then(payRes => {
							if (payRes.code == 1) {
								uni.hideLoading()
								uni.showToast({
									title: "兑换成功",
									icon: 'success',
								})
							} else {
								uni.hideLoading()
								uni.showToast({
									title: payRes.info,
									icon: 'error',
								})
							}
						})
					} else {
						uni.hideLoading()
						uni.showToast({
							title: res.info,
							icon: 'error',
						})
					}
				}).catch(err => {
					uni.hideLoading()
					uni.showToast({
						title: "网络异常",
						icon: 'none',
					})
				})
			},
			toAddress(){
				uni.redirectTo({
					url:'/pages/user/address?order_no='+this.order_no
				})
			},
		}
	}
</script>

<style>

</style>
