<template>
	<view>
		<!-- #ifdef H5 -->
		<view style="color: #fff;">
			<tn-nav-bar backgroundColor="#26B3A0" :bottomShadow="false" @rightClick="jumpScoreRed">课程详情
			</tn-nav-bar>
			<view :style="{ height: tobheight + 'px' }"></view>
		</view>
		<!-- #endif -->


		<view class="scoreBox">
			<view class="scoreInfo">
				课程信息
			</view>
			<view v-if="detail.course" class="scoreInfoCon">
				<image :src="detail.course.cover_image==''?'../../../static/posImg.png': detail.course.cover_image"
					mode=""></image>
				<view class="scoreTitle">
					<!-- Chat GPT应用场景及综合应用（第1期） -->
					{{detail.course.name}}
				</view>
			</view>
			<view class="scoreDes">
				<!-- 主要完成应用场景的填写，查找，上传，学习后将收获对open ai的了解更加多元。 -->
				<u-parse :content="detail.course ? detail.course.description : '暂无课程信息'"></u-parse>
			</view>

			<view class="topBtn" @click="downLoad(detail.status)">
				查看学习资料
			</view>

			<view class="divLine">

			</view>
			<view class="orderInfo">
				<view class="title">
					订单信息
				</view>
				<view class="orderNo item">
					订单编号：{{detail.code}}
				</view>
				<view class="createAt item">
					创建时间：{{detail.created_at }}
				</view>
				<view class="orderMoney item">
					订单金额：{{detail.amount}}
				</view>
				<view class="payWay item">
					支付方式：微信
				</view>
				<view class="orderStatus item">
					订单状态：{{detail.status_text}}
				</view>
			</view>
			<!-- <button @click="goPay">测试支付</button> -->
		</view>
	</view>
</template>

<script>
	import {
		myScoreOrderDetail
	} from '@/api/my/index.js'
	import {
		mapMutations,
		mapActions,
		mapState,
		mapGetters
	} from 'vuex';
	import {
		apiurl
	} from '@/common/request/request';

	import {
		downLoad2
	} from '@/utils/download2.js'

	import {
		saveAs
	} from '@/utils/FileSaver.js'
	import ShoproPay from '@/common/pay';
	export default {
		data() {
			return {
				loginType: 'h5', //app.globalData.loginType
				tobheight: 45,
				detail: {},

				orderId: ''
			};
		},
		computed: {

		},
		async onLoad(o) {
			// console.log('是否导入成功', saveAs);
			this.orderId = o.id
			let res = await myScoreOrderDetail({
				course_order_id: o.id
			})
			if (res.code == 0) {
				this.detail = res.data
			}
			console.log('课程详情', this.detail);
		},
		methods: {
			goPay() {
				let pay = new ShoproPay(this.orderId);
			},
			downLoad(status) {
				// uni.navigateTo({
				// 	url: '/pages/test/index'
				// })
				if (status == 0) {
					uni.showToast({
						title: '待支付课程不能查看学习资料',
						icon: 'none',
						duration: 1500
					})
					return
				}

				if (!this.detail.course.content) {
					uni.showToast({
						title: '当前课程暂无学习资料',
						icon: 'none',
						duration: 1500
					})
					return
				}

				if (this.detail.course.content.endsWith('.mp4')) {
					console.log('.mp4', this.detail.course.content);

					// row.fileAdd  文件地址   row.fileName  文件名称，需要加上文件后缀
					// fetch(this.detail.course.content)
					// 	.then(response => response.blob())
					// 	.then(blob => {
					// 		const url = URL.createObjectURL(blob);
					// 		const link = document.createElement('a');
					// 		link.href = url;
					// 		link.setAttribute('download', '123456.mp4');
					// 		document.body.appendChild(link);
					// 		link.click();
					// 	})
					// 	.catch(console.error);

					let plat = uni.getSystemInfoSync().platform

					if (plat == 'android' || plat == 'ios') {
						console.log("plat == 'android' || plat == 'ios'");

						// uni.downloadFile({
						// 	url: this.detail.course.content, //仅为示例，并非真实的资源
						// 	success: (res) => {
						// 		if (res.statusCode === 200) {
						// 			console.log('下载成功', res.tempFilePath);
						// 			downLoad2(res.tempFilePath, this.detail.course.name + '-学习资料')
						// 		}
						// 	}
						// });
						window.open(
							this.detail.course.content
						)
						// fetch(this.detail.course.content)
						// 	.then(response => {
						// 		return response.blob()
						// 	})
						// 	.then(blob => {
						// 		const url = URL.createObjectURL(blob);
						// 		const link = document.createElement('a');
						// 		link.href = url;
						// 		link.setAttribute('download', '123456.mp4');
						// 		document.body.appendChild(link);
						// 		link.click();
						// 	})
						// 	.catch(console.error);

					} else {
						console.log("pc");
						downLoad2(this.detail.course.content, this.detail.course.name + '-学习资料', 'video/mp4')
					}

					// saveAs(this.detail.course.content, '测试文件名')
				} else {
					let plat = uni.getSystemInfoSync().platform
					if (plat == 'android' || plat == 'ios') {
						console.log("plat == 'android' || plat == 'ios'");
						window.open(
							this.detail.course.content
						)
					} else {
						console.log("pc");
						downLoad2(this.detail.course.content, this.detail.course.name + '-学习资料', 'application/pdf')
					}
				}
			}
		}
	}
</script>

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