<template>
	<!-- 我的 -->
	<view class="my">
		<!-- 状态栏 -->
		<view class="status-bar bg-white pt-30"></view>
		<!-- 头像栏 -->
		<view class="flex items-center wrap-card" hover-class="bg-gray-100" @click="$u.route('/pages/mine/edit-info')">
			<image class="header-img" src="/static/img/demo/avatar.jpeg" mode=""
				@click.stop="$u.route('/pages/mine/edit-info')">
			</image>
			<view class="flex flex-col flex-1 px-20">
				<text class="text-32 font-bold">{{userInfo.name}}</text>
				<text class="text-28 text-gray-500 mt-10">{{userInfo.contact_mobile}}</text>
			</view>
		</view>
		<!-- 统计栏 -->
		<!-- <view class="wrap-card flex items-center">
			<view v-for="(item,index) in countList" :key="index"
				class="flex-1 flex flex-col items-center justify-center">
				<view class="text-32 font-bold">
					{{item.num}}
				</view>
				<view class="text-28 text-gray-500 mt-10">
					{{item.name}}
				</view>
			</view>
		</view> -->
		<!-- 广告位 -->
		<view v-if="swiperList.length>0" class="wrap-cards">
			<u-swiper :list="swiperList" keyName="src" interval='10000' circular height="180rpx"
				bgColor="#ffffff"></u-swiper>
		</view>
		<!-- 设置 -->
		<u-cell-group :border='false'>
			<u-cell v-for="(item,index) in setList" :key="index" :title="item.name" isLink size="large" :border='false'
				@click="goPage(item.url)">
				<text slot="icon" class="iconfont" :class="item.icon"></text>
			</u-cell>
		</u-cell-group>
	</view>
</template>

<script>
	import {
		userInfo,
		fileGet,
	} from '@/api/login.js'
	import {
		bannerList
	} from '@/api/train.js'
	export default {
		data() {
			return {
				// 统计列表
				// countList: [{
				// 		num: 12,
				// 		name: '帖子'
				// 	},
				// 	{
				// 		num: 278,
				// 		name: '动态'
				// 	},
				// 	{
				// 		num: 369,
				// 		name: '评论'
				// 	},
				// 	{
				// 		num: 750,
				// 		name: '粉丝'
				// 	}
				// ],
				// 设置列表
				setList: [
					// {
					// 	icon: 'iconhuiyuan',
					// 	name: '个人空间',
					// 	url: 'user-space'
					// },
					{
						icon: 'iconliulan',
						name: '浏览历史',
						url: 'history',
					},


					{
						icon: 'iconshezhi1',
						name: '修改资料',
						url: 'set'
					}
				],
				userInfo: {},
				flieList: {},
				swiperList: [],
			}
		},
		onLoad() {
			this.getBannerList()
		},
		onShow() {
			this.getUserInfo()
		},
		// 监听导航栏按钮点击
		onNavigationBarButtonTap() {
			this.goSet()
		},
		methods: {

			getBannerList() {
				bannerList({
					position: 'mine',
					org_id: getApp().globalData.org_id
				}).then(res => {
					this.swiperList = res.data
				})
			},

			getUserInfo() {
				userInfo().then(res => {
					this.userInfo = res.data
					uni.setStorageSync('userInfo', res.data)
				})
			},
			goSet() {
				this.$u.route('/pages/mine/edit-info')
			},
			goPage(type) {
				switch (type) {
					case 'history':
						this.$u.route('/pages/mine/' + type)
						break;
					case 'set':
						this.$u.route('/pages/mine/edit-info')
						break;
					case 'file2':
						this.downloadFile(this.flieList.file2)
						break;
					case 'file1':
						this.downloadFile(this.flieList.file1)
						break;
					default:
						break;
				}
			},
			downloadFile(url) {
				uni.showLoading({
					title: '正在下载...',
				});
				uni.downloadFile({
					url: url, // 文件的 URL
					success: (res) => {
						if (res.statusCode === 200) {
							// 下载成功
							const tempFilePath = res.tempFilePath;

							// 保存文件到本地
							uni.saveFile({
								tempFilePath: tempFilePath,
								success: (saveRes) => {
									// 文件保存成功，获取保存后的文件路径
									const savedFilePath = saveRes.savedFilePath;

									// 提示用户文件已保存，并显示保存路径
									uni.showToast({
										title: '文件已保存',
										icon: 'success'
									});

									// 打开文件
									uni.openDocument({
										showMenu: true,
										filePath: savedFilePath,
										fileType: 'xlsx', // 根据文件类型设置
										success: () => {
											console.log('文件打开成功');
										},
										fail: (err) => {
											uni.showToast({
												title: '打开文件失败',
												icon: 'none'
											});
											console.error('打开文件失败:', err);
										}
									});
								},
								fail: (err) => {
									uni.showToast({
										title: '保存失败',
										icon: 'none'
									});
									console.error('保存文件失败:', err);
								}
							});
						} else {
							uni.showToast({
								title: '下载失败',
								icon: 'none'
							});
						}
					},
					fail: (err) => {
						uni.showToast({
							title: '下载失败',
							icon: 'none'
						});
						console.error('下载文件失败:', err);
					},
					complete: () => {
						uni.hideLoading();
					}
				});
			}



		}
	}
</script>

<style lang="scss" scoped>
	.my {

		.header-img {
			width: 100rpx;
			height: 100rpx;
			border-radius: 100%;
			box-shadow: inset 0rpx -1rpx 0rpx 0rpx #EEEEEE;
		}

		.wrap-cards {
			width: 92%;
			margin: 0 auto;
		}

		.adv-img {
			height: 200rpx;
			width: 100%;
		}
	}
</style>