<template>
	<view class="container">
		<!-- #ifdef H5 -->
		<view style="color: #fff;">
			<tn-nav-bar backgroundColor="#26B3A0" :bottomShadow="false" @rightClick="jumpScoreRed">支付状态
				<!-- 	<template slot="right">
					课程购买记录
				</template> -->
			</tn-nav-bar>
			<view :style="{ height: tobheight + 'px' }"></view>
		</view>
		<!-- #endif -->


		<view class="content">
			<view class="topIcon">
				<!-- <image src="../../../static/posImg.png" mode=""></image> -->
				<icon type="success" size="54" color="#26B3A0" />
				<view class="txt">
					支付成功
				</view>
			</view>
			<view class="btn" @click="handleShowRes">
				{{resShow?'收起学习资料':'查看学习资料'}}
			</view>

			<view class="res" v-if="resShow&&this.detail.content">
				<view class="inner" v-for="(i,index) in this.detail.content" @click="downLoad(i)">
					<text style="text-decoration: underline;">
						{{'学习资料'+(index+1)}}</text><text>{{i.slice(i.lastIndexOf('.'))}}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		courseDetail
	} from '@/api/score/index.js'
	import {
		downLoad2
	} from '@/utils/download2.js'
	export default {
		components: {},
		data() {
			return {
				detail: {},

				tobheight: 45,
				resShow: false
			};
		},
		computed: {

		},
		watch: {

		},
		async onLoad(o) {
			let res = await courseDetail({
				course_id: o.id
			})
			if (res.code == 0) {
				this.detail = res.data
			}
			console.log('支付成功--课程详情返回值', res);
		},
		onReady() {

		},
		onShow() {

		},
		onUnload() {

		},
		methods: {
			handleShowRes() {
				this.resShow = !this.resShow
			},


			downLoad(fileName) {
				let _this = this
				//判断是否在微信
				var ua = navigator.userAgent.toLowerCase();
				var isWeixin = ua.indexOf('micromessenger') != -1;

				const detectDeviceType = () => /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
					navigator.userAgent) ? false : true;

				if (isWeixin) {
					if (fileName.endsWith('.pdf')) {
						uni.navigateTo({
							url: '/pages/user/score/prePdf/index?url=' + fileName
						})
					} else if (fileName.endsWith('.mp4')) {
						uni.navigateTo({
							url: '/pages/user/score/preMp4/index?url=' + fileName
						})
					} else if (fileName.endsWith('.jpg') || fileName.endsWith('.png') || fileName.endsWith('.jpeg')) {
						uni.navigateTo({
							url: '/pages/user/score/preImg/index?url=' + fileName
						})
					} else if (fileName.endsWith('.zip') || fileName.endsWith('.rar')) {
						let _that = this
						uni.showModal({
							title: '温馨提示',
							content: '微信浏览器不支持直接打开zip或rar类型的文件，请复制链接至手机浏览器打开哦~',
							confirmText: '复制',
							confirmColor: '#26B3A0',
							success: function(res) {
								if (res.confirm) {
									console.log('用户点击确定');
									uni.setClipboardData({
										data: fileName,
										success: function() {
											console.log('success');
											uni.showToast({
												title: "复制成功",
												icon: 'success',
											})
										}
									});
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						})
					}

				} else if (detectDeviceType()) {
					// pc端
					uni.downloadFile({
						url: fileName, //文件链接
						success: (res) => {
							if (res.statusCode === 200) {
								var oA = document.createElement("a");
								oA.download = _this.detail.course.name + '-学习资料'; // 设置下载的文件名，默认是'下载'	
								oA.href = res.tempFilePath; //临时路径再保存到本地
								document.body.appendChild(oA);
								oA.click();
								oA.remove(); // 下载之后把创建的元素删除
							}
						},
						fail: (err) => {
							uni.showToast({
								icon: 'none',
								mask: true,
								title: '失败请重新下载',
							});
						},
					})
				} else {
					// 其他手机浏览器
					if (fileName.endsWith('.pdf')) {
						uni.navigateTo({
							url: '/pages/user/score/prePdf/index?url=' + fileName
						})
					} else if (fileName.endsWith('.mp4')) {
						uni.navigateTo({
							url: '/pages/user/score/preMp4/index?url=' + fileName
						})
					} else if (fileName.endsWith('.jpg') || fileName.endsWith('.png') || fileName.endsWith('.jpeg')) {
						uni.navigateTo({
							url: '/pages/user/score/preImg/index?url=' + fileName
						})
					} else if (fileName.endsWith('.zip') || fileName.endsWith('.rar')) {
						downLoad2(fileName, this.detail.course.name + '-学习资料', 'application/zip')
						// window.open(
						// 	fileName
						// )
					}
				}
			},

			goPay() {
				uni.navigateTo({
					url: '/pages/index/scorePaySuc/index'
				})
			}
		}
	};
</script>

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