<template>
	<view>
		<status-nav :ifReturn="true" navBarTitle="收藏" :marginBottom="0"></status-nav>
		<!-- 导航 -->
		<view class="nav-list-bg" :style="{top:newTop+'px'}">
			<nav-tab :list="navTabList" @chooseEv="chooseEv"></nav-tab>
		</view>
		<!-- 列表 -->
		<view class="news-list-bg" v-if="isLoading">
			<pull-list :list="collectionList" :collection="1" :isShop="isShop" @toDetail="toArticleDetail" @collectionEv="collectionEv"></pull-list>
		</view>
		<!-- 暂无更多内容 -->
		<view class="more-txt more-txt-other" v-if="total!==0 && totalAll == total">暂无更多内容</view>
		<nothing-page v-if="total==0&&totalAll == total" content="还没有相关收藏哟(*^▽^*)"></nothing-page>
	</view>
</template>

<script>
	import statusNav from '@/components/status-navs/status-nav';
	import navTab from '@/components/nav-tab/nav-tab.vue';
	import pullList from '@/components/pull-list/pull-list.vue';
	import {getCartNum,userInfoEv} from '@/jsFile/public-api.js';
	import {mapState} from 'vuex'//引入mapState
	export default {
		components:{
			statusNav,
			navTab,
			pullList
		},
		data() {
			return {
				scrollHeight:uni.getSystemInfoSync().windowHeight - uni.getSystemInfoSync().statusBarHeight - 50,
				newWidth:uni.getSystemInfoSync().windowWidth,
				newTop:uni.getSystemInfoSync().statusBarHeight + 50,
				collectionList:[], //解答&资讯列表
				navTabList:[ //导航列表
					{name:'商品'},
					{name:'文章'},
				],
				currentIndex:0, //当前位置
				page:1, //第几页
				size:10, //查询条数
				total:0, //总数
				totalAll:-1, //计算总数
				isShop:true, //是否商品
				cacheBusinessId:-1, //商户id
				isLoading:false,
			}
		},
		computed:{
			...mapState({
				footHeight: state => state.moduleA.footHeight,
			}),
		},
		onLoad(op) {
			if(op.business_id){
				this.cacheBusinessId = op.business_id;
			}
		},
		onShow() {
			if(this.cacheBusinessId !== -1){
				this.$requst.post('/api/index/change-business',{business_id:this.cacheBusinessId}).then(res=>{
					if(res.code == 0){
						this.collectionList = [];
						this.getCollectionList();
						userInfoEv();
					}
				})
			}else{
				this.collectionList = [];
				this.getCollectionList();
				userInfoEv();
			}
		},
		onReachBottom(e) {
			if(this.collectionList.length<this.total){
				this.page++;
				this.getArticleList();
			}
			this.page++;
			this.getCollectionList();
		},
		// 分享到微信
		onShareAppMessage() {
			let path = uni.getStorageSync('page-path-options')+'?business_id='+uni.getStorageSync('business_id');
			return {
				path:path
			}
		},
		// 分享到朋友圈
		onShareTimeline(res){
			let path = uni.getStorageSync('page-path-options')+'?business_id='+uni.getStorageSync('business_id');
			return {
				path:path
			}
		},
		methods: {
			// 切换事件
			chooseEv(type){
				if(type == 0){
					this.isShop = true;
				}
				if(type == 1){
					this.isShop = false;
				}
				if(this.currentIndex !== type){
					this.total=0;
					this.totalAll=-1;
					this.page = 1;
					this.currentIndex = type;
					this.collectionList=[];
					this.getCollectionList();
				}
			},
			// 获取收藏列表
			getCollectionList(){
				let params = {
					page:this.page,
					size:this.size
				}
				this.isLoading =false;
				if(this.currentIndex == 0){
					uni.showLoading({
						title: '加载中'
					});
					this.$requst.get('/api/spu/collection',params).then(res=>{
						if(res.code == 0){
							this.total = res.data.total;
							let newArr = [];
							res.data.list.forEach(item=>{
								let obj = {
									id:item.id,
									src:item.cover,
									title:item.name,
									time:'',
								}
								newArr.push(obj)
							})
							this.collectionList = this.collectionList.concat(newArr);
							if(this.collectionList.length == this.total){
								this.totalAll = this.total;
							}
						}
						uni.hideLoading();
						this.isLoading =true;
					})
				}
				if(this.currentIndex == 1){
					this.$requst.get('/api/archives/collects',params).then(res=>{
						if(res.code == 0){
							this.total = res.data.total;
							let newArr = [];
							res.data.list.forEach(item=>{
								let obj = {
									id:item.id,
									src:item.cover,
									title:item.title,
									time:this.dateFormat(item.created_at.replace(/-/g,'/')),
								}
								newArr.push(obj)
							})
							this.collectionList = this.collectionList.concat(newArr);
							if(this.collectionList.length == this.total){
								this.totalAll = this.total;
							}
						}
						uni.hideLoading();
						this.isLoading =true;
					})
				}
			},
			// 时间格式转换
			dateFormat (dateData) {
			    var date = new Date(dateData)
			    var y = date.getFullYear()
			    var m = date.getMonth() + 1
			    m = m < 10 ? '0' + m : m
			    var d = date.getDate()
			    d = d < 10 ? '0' + d : d
			    const time = y + '.' + m + '.' + d
			    return time
			},
			//收藏
			collectionEv(id){
				console.log(id,12121)
				if(this.currentIndex == 0){
					let params = {
						id:id.id,
						action:'collect'
					}
					this.$requst.post('/api/spu/un-record',params).then(res=>{
						if(res.code==0) {
							this.$toolAll.tools.showToast('取消收藏成功');
							this.getCollectionList();
						}
					})
				}
				if(this.currentIndex == 1){
					let params = {
						archive_id:id.id,
						action:'collect'
					}
					this.$requst.post('/api/archives/un-record',params).then(res=>{
						if(res.code==0) {
							this.$toolAll.tools.showToast('取消收藏成功');
							this.getCollectionList();
						}
					})
				}
			},
			//去文章详情
			toArticleDetail(id){
				if(this.currentIndex == 0){
					uni.navigateTo({
						url:`/pagesA/shop/detail?id=${id.id}&source=shop`
					})
				}
				if(this.currentIndex == 1){
					uni.navigateTo({
						url:`/pages/tabbar/news/detail?id=${id.id}`
					})
				}
			}
		}
	}
</script>

<style>

</style>