<template>
	<view class="pad-x180">
		<!-- 状态栏 -->
		<status-nav :titleVal="titleStr" :statusTitle="true"></status-nav>
		<!-- 输入框 -->
		<view class="search-input-box bacf" :style="{paddingTop: statusHeight+'px'}">
			<view class="disac pad-zy30 mar-s20">
				<!-- 输入框 -->
				<input @confirm="searchEv" @input="inputEv" class="width100 fon34 radius10 problem-input" type="text" v-model="searchVal" placeholder="请输入内容..."/>
				<!-- 搜索 -->
				<view @tap="searchEv" class="flexs mar-z30 radius10 pad-zy40 fon34 colf" style="height: 80rpx;line-height: 80rpx;" :style="{background:publicColor}">搜索</view>
			</view>
		</view>
		<view class="pad-zy20 mar-s20">
			<!-- 列表 -->
			<view v-if="dataList.length!=0">
				<list-pu @chooseLike="chooseLike" @praise="praiseEv" @comfirmev="comfirmevl" :list="dataList"></list-pu>
			</view>
			<view v-else class="disjcac fc" style="margin-top: 40%;">
				<image class="zanw-img" src="/static/public/nothing.png" mode="aspectFill"></image>
				<view class="fon24 col3">您搜索的内容暂无结果,换个关键词试试吧</view>
			</view>
		</view>
		<!-- 弹框 -->
		<pu-po :isShowT="isShowT" @comfirmev="comfirmev" @cancleev="cancleev"></pu-po>
		<!-- 返回顶部 -->
		<!-- <back-top :showTop="showTop" @backTop="backTop"></back-top> -->
		<!-- 用户信息授权,手机号授权 -->
		<auth-userInfo-mobileInfo></auth-userInfo-mobileInfo>
		<!-- 底部tab -->
		<foot-tab :titleList="titleList" :imgList="imgList" :newcurrent='-1'></foot-tab>
	</view>
</template>

<script>
	import cateTu from '@/components/cate-items/cate-tu.vue';
	import {collectionEV,cancleCollectionEV} from '@/jsFile/publicAPI.js';
	export default {
		components:{
			cateTu
		},
		data() {
			return {
				titleStr:'',//标题
				searchVal:'',//输入框的值
				isShowT:false,//是否显示弹框
				cateList:[//所以二级分类
					
				],
				dataList:[//小课堂数据列表
					
				],
				showTop:false,//是否显示返回顶部
				newCurrent:0,
				page:1,
				size:10,
				total:'',//总数
				isZanw:true,
				category_id:'',//栏目ID
			}
		},
		computed: {
			// 底部标题
			titleList() {
				return this.$store.state.titleList 
			},
			// 底部图标
			imgList() {
				return this.$store.state.imgList
			},
			// 主题颜色
			publicColor() {
				return this.$store.state.publicColor
			},
			statusHeight() {
				return this.$store.state.statusHeight
			}
		},
		onReachBottom() {//触底事件
			if(this.total!=this.dataList.length){
				this.page++
				this.searchEv()//调用自主预约列表事件
			} else {
				if(this.isZanw) this.$toolAll.tools.showToast('暂无更多列表','none',1000)
				this.isZanw = false
			}
		},
		onShareAppMessage() {
			return {
				title:this.titleStr,
				path:`/pagesB/problemList/problemList?category_id=${this.category_id}&title=${this.titleStr}`
			}
		},
		onLoad(options) {
			this.category_id = options.category_id;//病种id
			this.titleStr = options.title;//标题
			if(uni.getStorageSync('phone_active')!=0 && uni.getStorageSync('is_active')!=0){
				this.searchEv()
			}
		},
		methods: {
			// 搜索框改变事件
			inputEv(e) {
				this.page = 1;
			},
			chooseLike(e){//收藏事件
				if(this.dataList[e].is_collected==0){
					this.dataList[e].is_collected = 1
					// 调用收藏事件
					collectionEV({action:'collect',archive_id:this.dataList[e].id})
				}
			},
			praiseEv(e){ // 点赞事件
				if(this.dataList[e].is_liked==0){
					this.dataList[e].is_liked = 1;
					this.dataList[e].likes++;
					if(this.dataList[e].likes>1000) this.dataList[e].likes = '999+'
					// 调用收藏事件
					collectionEV({action:'like',archive_id:this.dataList[e].id})
				}
			},
			comfirmevl(e){//确认取消收藏事件
				this.dataList[e].is_collected = 0
				this.$toolAll.tools.showToast('正在取消...','loading')
				// 调用取消收藏事件
				cancleCollectionEV({action:'collect',archive_id:this.dataList[e].id})	
			},
			searchEv(){//搜索事件
				this.$toolAll.tools.showToast('搜索中...')
				this.$requst.post('archives/category',{page:this.page,size:this.size,category_id:this.category_id,keyword:this.searchVal}).then(res=>{
					// console.log('搜索提交:',res);
					if(res.code==0){
						uni.hideToast()
						if(uni.getStorageSync('cateList')==''){//首次渲染栏目
							if(res.data.category.length!=0){
								res.data.category.forEach((item,index)=>{
									let cateObj = {
										id:item.id,//栏目ID 
										title:item.title,//栏目名称
										active:item.active,//是否选中
										model_id:item.model_id,//模型ID
										model_name:item.model_name,//模型标识
										sort:item.sort
									}
									this.cateList.push(cateObj)
								})
								uni.setStorageSync('cateList',this.cateList)
							}
						} else {
							//获取缓存中的栏目
							this.cateList = uni.getStorageSync('cateList')
						}
						// 获取列表
						if(this.page==1) this.dataList = []
						this.total = res.data.list.total
						if(res.data.list.list.length!=0){
							res.data.list.list.forEach(item=>{
								let fabImg = '';
								if(item.published_headimgurl!='' && item.published_headimgurl!=null) fabImg = this.$http + item.published_headimgurl;
								let tObj = {
									category_id:item.category_id,//栏目ID
									id:item.id,
									is_collected:item.is_collected,//是否已收藏
									collects:item.collects,//收藏量
									views:item.views,//查看量
									main_img:this.$http + item.cover,//封面图
									video:this.$http + item.video,//视频地址
									title:item.title,//标题
									content:item.subtitle,//副标题
									head_img: fabImg || '/static/public/logo.png',//发布者头像
									name:item.published_by || '恒美植发',//发布者昵称
									isVideo:item.video.includes(".mp4"),//是否是视频
									likes:item.likes,//点赞数量
									is_liked:item.is_liked//是否已点赞
								}
								this.dataList.push(tObj);
							})
						}
						let obj = {
							content:this.searchVal
						}
					}
				})
			},
			cancleev(){//取消事件
				this.isShowT = false
			},
		}
	}
</script>

<style>

</style>