<template>
	<view>
		<view style="color: #fff;">
			<tn-nav-bar backgroundColor="#26B3A0" :bottomShadow="false" @rightClick="jumpScoreRed">课程购买记录
			</tn-nav-bar>
			<view :style="{ height: tobheight + 'px' }"></view>
		</view>

		<!-- <view class="" v-if="scoreOrderList.length==0">
			<empty style=""></empty>
		</view> -->

		<view class="recordBox">
			<view class="wallet-log-box u-p-b-30" v-if="scoreOrderList.length!=0">
				<view class="log-list" v-for="item in scoreOrderList" :key="item.id" @click="goScoreDetail(item)">
					<view class="head u-flex u-col-center u-row-between" style="color: black;">
						<view class="title" style="color: black;">课程名称：{{item.course_name }}</view>
						<view class="num" style="color: black;">价格：{{item.amount}}元</view>
					</view>
					<view class="time-box item  u-flex u-col-center u-row-between">
						<text class="item-title">订单编号</text>
						<view class="time">{{item.code}}</view>
					</view>
					<view class="status-box item  u-flex u-col-center u-row-between">
						<view class="item-title">支付状态</view>
						<view class="status-text">{{item.status_text }}</view>
					</view>
					<view class="time-box item  u-flex u-col-center u-row-between">
						<text class="item-title">支付时间</text>
						<view class="time">{{item.updated_at}}</view>
					</view>
				</view>
			</view>
			<empty v-else style="margin-top: 50%;"></empty>
		</view>
	</view>
</template>

<script>
	import {
		mapMutations,
		mapActions,
		mapState,
		mapGetters
	} from 'vuex';
	import {
		apiurl
	} from '@/common/request/request';
	import {
		courseDetail,
		shopCourse
	} from '@/api/score/index.js'
	import {
		myScoreOrder,
		myScoreOrderDetail
	} from '@/api/my/index.js'
	import {
		wxPayPamars,
		wxConfigPamars,
	} from '@/api/public/index.js'
	import wxsdk from '@/common/wechat/sdk'
	import empty from '@/components/empty/empty.vue'
	export default {
		components: {
			empty
		},
		data() {
			return {
				loginType: 'h5', //app.globalData.loginType
				tobheight: 45,

				scoreOrderList: [],
				course_id: ''
			};
		},
		computed: {
			...mapGetters(['appInfo', 'isLogin', 'userInfo'])
		},
		async onLoad() {
			// let res = await myScoreOrder()
			// if (res.code == 0) {
			// 	this.scoreOrderList = res.data
			// }
			// console.log('我的课程订单返回值', res);
		},
		async onShow() {
			let res = await myScoreOrder()
			if (res.code == 0) {
				this.scoreOrderList = res.data
			}
			console.log('我的课程订单返回值', res);
		},
		methods: {
			async goScoreDetail(score) {
				this.course_id = score.course_id

				if (score.status) {
					uni.navigateTo({
						url: '/pages/user/score/index?id=' + score.id
					})
				} else {
					uni.showModal({
						title: '提示',
						content: '当前订单状态为待支付，确认继续支付？',
						confirmColor: 'rgb(38, 179, 160)',
						success: async (res) => {
							if (res.confirm) {
								let res1 = await myScoreOrderDetail({
									course_order_id: score.id
								})
								if (res1.code == 0) {
									let res2 = await wxPayPamars({
										order_id: res1.data.id
									})
									let url = window.location.href.split('#')[0]
									let res3 = await wxConfigPamars({
										url: url
									})

									console.log('res2返回值', res2);
									console.log('res3返回值', res3);
									if (res2.code == 0 && res3.code == 0) {
										wxsdk.wxpay(res3.data, res2.data, (res) => {
											if (res.errMsg == "chooseWXPay:ok") {
												setTimeout(() => {
													uni.navigateTo({
														url: '/pages/index/scorePaySuc/index?id=' +
															this
															.course_id
													})
												}, 1500)
											} else {
												uni.showToast({
													title: '支付失败,请稍后重试！',
													icon: 'error',
													duration: 1500
												})
											}
										});
									} else {
										if (res2.code != 0) {
											uni.showToast({
												title: res.msg,
												icon: 'none',
												duration: 1500
											})
										} else if (res3.code != 0) {
											uni.showToast({
												title: res.msg,
												icon: 'none',
												duration: 1500
											})
										}
									}
								} else {
									uni.showToast({
										title: res.msg,
										icon: 'none'
									})
								}
							} else if (res.cancel) {

							}
						}
					});
				}
			},

			...mapActions(['logout']),
			/**
			 * 小程序设置
			 */
			Setting: function() {
				uni.openSetting({
					success: function(res) {}
				});
			},
		}
	}
</script>

<style lang="scss">
	@import './index.scss';
</style>