<template>
	<view class="page">
		<view class="order">
			<view class="order-info">订单编号：{{item.orderNo}}</view>
			<view class="order-info">下单时间：{{item.createTime}}</view>
			<view class="order-info">支付方式：线下认购</view>
			<view class="order-info">支付流水号：29830843045809</view>
		</view>
		<!-- bottom-flex-btn-wrap end -->
		<uni-popup ref="popup" type="dialog">
		    <uni-popup-dialog type="info"  :content="popupContent" :duration="2000" :before-close="true" @close="closepopup" @confirm="confirmpopup"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import uniPopup from '@/components/uni-popup/uni-popup.vue';
	import uniPopupDialog from '@/components/uni-popup/uni-popup-dialog.vue';
	export default {
		components: {
			uniPopup,
			uniPopupDialog,
		},
		data() {
			return {
				token:this.config.testToken,
				tokenhead:'',
				params: {
					orderid:'',
				},
				item:[],
			}
		},
		onShow() {
			let serf = this;
		},
		onLoad(option) {
			// console.log('option',option);
			this.params.orderid = option.orderid;
			let self = this;
			uni.getStorage({
				key:'token',
				success: function (res) {
					self.token = res.data;
				   // console.log(res.data);
				}
			});
			uni.getStorage({
				key:'tokenhead',
				success: function (res) {
					self.tokenhead = res.data;
				   // console.log(res.data);
				}
			});
			this.getdetails();

		},
		methods: {
			getdetails(){
				this.$api.http.get(this.config.apiBaseurl + '/carbon-h5/wap/order/info?orderId='+this.params.orderid,{
					header: {
					Accept:'application/json',
					Authorization: 'Bearer '+ this.token, //注意Bearer后面有一空格
				},
				}).then(res => {
					this.item = res.data.retBody;
					console.log('res',JSON.parse(JSON.stringify(this.item)))
				}).catch(err =>{
					console.log('err',err)
				});
			},
			cancelOrder(){
				this.$refs.popup.open();
				this.popupContent = '确认取消此订单吗?';
				this.popupType = 'cancel';
			},
			received(){
				this.$refs.popup.open();
				this.popupContent = '确认收货?';
				this.popupType = 'received';
			},
			closepopup(done){
				done()
			},
			confirmpopup(done,value){
				if(this.popupType == 'cancel'){
					this.$api.http.post(this.config.apiBaseurl + '/order/cancelUserOrder/?orderId='+this.item.id,{},{
						header: {
						Accept:'application/json',
						Authorization: 'Bearer '+ this.token, //注意Bearer后面有一空格
					},
					}).then(res => {
						this.getdetails();
						this.goOrderList(res.data.message);
						// console.log('res',res)
					}).catch(err =>{
						console.log('err',err)
					});
				}else if(this.popupType == 'received'){
					this.$api.http.get(this.config.apiBaseurl + '/order/confirmReceiveOrder/?orderId='+this.item.id,{},{
						header: {
						Accept:'application/json',
						Authorization: 'Bearer '+ this.token, //注意Bearer后面有一空格
					},
					}).then(res => {
						this.getdetails();
						this.goOrderList(res.data.message);
						// console.log('res',res)
					}).catch(err =>{
						console.log('err',err)
					});
				}
				
				done()
			},
			pay(){
				let userInfo = uni.getStorageSync("userInfo");
					let params = {
						orderId:this.item.id,
						payType:this.item.payType,
						openid:userInfo.wxUsers.openid,
						"tradeType":"test"
					}
					this.$pay.wxPay(params).then(res =>{
						this.getdetails();
						this.goOrderList(res.data.message);
						// console.log('payres',res);
					}).catch(err =>{console.log('err',err);});
			},
			// 跳转到订单列表
			goOrderList(msg){
				uni.showToast({
					icon:"none",
					title:msg,
					duration: 2000
				});	
				setTimeout(()=>{
					uni.navigateTo({
						url: '/pages/usercenter/myorder/myorder',
						fail:function(err){
							console.log(err)
						}
					});
				},1000)
			}

		}
	}
</script>

<style>
	@import url("./orderdetails.css");
</style>
