<template>
	<view>
		<view class="topt">
			<view class="search_con">
				<view class="search_inp">
					<view class="flex">
						<image src="../../static/img/search.png" mode="widthFix"
							style="width: 48rpx;margin-right: 10rpx;"></image>
						<input type="text" v-model="params.search_name" placeholder="输入关键字" placeholder-style="color:#999;" style="width: 370rpx;" />
					</view>
					<view class="search" @click="params.page_index = 1, list = [], getList()">查询</view>
				</view>
			</view>
		</view>
		<view class="list">
			<view v-for="(item, index) in list" :key="index" class="box" @click="getItem(item)">
				<view class="box_top flexS">
					<view class="title">
						{{ item.travel_name }}
					</view>
					<view class="right flex">
						<text>查看全部</text>
						<image src="../../static/img/right.png" mode="widthFix" style="width: 28rpx;"></image>
					</view>
				</view>
				<view class="box_time">
					{{ getTime(item.time_start) }}-{{ getTime(item.time_end) }}
				</view>
				<view class="box_image flex">
					<image v-if="item.get_img[0]" :src="item.get_img[0].thumbnail" mode="aspectFill"></image>
					<image v-if="item.get_img[1]" :src="item.get_img[1].thumbnail" mode="aspectFill"></image>
					<image v-if="item.get_img[2]" :src="item.get_img[2].thumbnail" mode="aspectFill"></image>
					<image style="margin-right: 0;" v-if="item.get_img[3]" :src="item.get_img[3].thumbnail" mode="aspectFill"></image>
				</view>
			</view>
		</view>
		
	</view>
</template>

<script>
	import { GetTravelList, wx_Login } from '../../api/index.js';
	export default {
		data() {
			return {
				params: {
				    page_index: 1,
				    page_size: 8,
					search_name: ''
				},
				list: []
			}
		},
		onLoad() {
			// const token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9hcGkuYXBwLmNsaXUuY2NcL2FwaVwvbG9naW5cL2dldF91c2VyX29wZW5pZCIsImlhdCI6MTY5NzMyNjU1MiwiZXhwIjoxNjk4MTkwNTUyLCJuYmYiOjE2OTczMjY1NTIsImp0aSI6InlkYnV5ZmVMenFSTHZlVjQiLCJzdWIiOjc1NjYwLCJwcnYiOiIyM2JkNWM4OTQ5ZjYwMGFkYjM5ZTcwMWM0MDA4NzJkYjdhNTk3NmY3In0.U2P1XRAFVk47k86SW12AHHQZWXew-PKkIZRmOuMq_K0'
			// uni.setStorageSync('token', token)
			this.getAuth()
			// 监听 token 失效事件，重新获取
			uni.$on('RELAUNCH', () => {
				uni.removeStorageSync('token')
				this.getAuth()
			})
		},
		onReachBottom() {
		   this.more()
		},
		methods: {
			getItem(item) {
				item.time = item.time_start.replace(/-/g, '.').slice(0, 10) + '-' + item.time_end.replace(/-/g, '.').slice(0, 10)
				let obj = encodeURIComponent(JSON.stringify(item))
				uni.navigateTo({
					url:'./item?item='+ obj
				})
			},
			getTime(v) {
				if (v) {
					return v.replace(/-/g, '.').slice(0, 10)
				}
			},
			getUrlParam(name) {
			    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
			    var r = window.location.search.substr(1).match(reg)
			    if (r != null) return unescape(r[2])
			    return null
			},
			getAuth() {
				let that = this
				if (!uni.getStorageSync('token')) {
					const code = this.getUrlParam('code') // 截取路径中的code，如果没有就去微信授权
					const appid = 'wx5224793b7dc7f7b7'
					const redirect_uri = encodeURIComponent('https://api.app.jiuweiyun.cn/api/travel#/pages/index/index');
					if (code == null || code === '') {
					    window.location.href =
					        `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
					} else {
					    wx_Login({ code: code }).then((res) => {
					        if (res.code == 200) {
								uni.setStorageSync('token', res.data.token)
								that.getList()
							} else {
								uni.showToast({ title: res.message || '获取失败', icon: 'none' })
							}
					    }).catch(e => {
							uni.showToast({ title: e.message || '获取失败', icon: 'none' })
						})
					}
				} else  {
					const token = uni.getStorageSync('token')
					uni.setStorageSync('token', token)
					this.getList()
				}
			},
			//获取列表
			getList(isMore) {
				uni.showLoading()
			    GetTravelList(this.params).then(res => {
					uni.hideLoading()
			        if (res.code === 200) {
			            const { list, total } = res.data
			            if (list.length === 0) {
							uni.showToast({ title: '暂无内容', icon: 'none' })
							return false
						}
			            this.totalPage = Math.ceil(Number(total) / 15)
			            this.list = isMore ? this.list.concat(list) : list
			        } else {
			            uni.showToast({ title: res.message || '获取列表失败', icon: 'none' })
			        }
			    }).catch(e => {
					uni.hideLoading()
				})
			},
			//加载获取更多
			more() {
			    if (this.params.page_index >= this.totalPage) {
					this.nomore = true
			        return false
			    }
			    this.params.page_index++
			    this.getList(true)
			},
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #F5F5F5;
	}
	.topt {
		width: 100%;
		padding: 30rpx 24rpx 24rpx 24rpx;
		box-sizing: border-box;
		background-color: #fff;
		position: fixed;
		left: 0;
		top: 0;
		z-index: 10000;
		.search_con {
			width: 700rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
		}
		.search_inp {
			height: 80rpx;
			width: 100%;
			padding: 0 10rpx 0 30rpx;
			border-radius: 44rpx;
			background: #F9F9FB;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.flex {
				display: flex;
			}
		}
		.search {
			width: 132rpx;
			height: 64rpx;
			line-height: 64rpx;
			font-size: 28rpx;
			color: #FFFFFF;
			text-align: center;
			background: #3D6EF6;
			border-radius: 32rpx;
		}
	}
	.list {
		padding: 158rpx 24rpx 24rpx 24rpx;
		.box{
			margin-bottom: 24rpx;
			padding: 24rpx;
			background: #FFFFFF;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			&_top {
				.title {
					font-size: 32rpx;
					font-weight: bold;
					color: #333333;
					font-family: PingFang SC-Bold, PingFang SC;
				}
				.right {
					font-size: 24rpx;
					color: #BCBCBC;
					image {
						margin-left: 8rpx;
					}
				}
			}
			&_time {
				font-size: 24rpx;
				color: #BCBCBC;
				margin: 8rpx 0 22rpx 0;
			}
			&_image {
				image {
					width: 156rpx;
					height: 156rpx;
					border-radius: 8rpx;
					margin-right: 10rpx;
				}
			}
		}
	}
</style>
