<template>
  <div
    id="scroll-area"
    class="tour-container">
    <Head/>
    <second-banner
      :list = "bannerList"
      :looptime="looptime"
      :width="width"
      :height="height"
      :background="background"
      :color="color"
      class="mb-50"
      @prev="prev"
      @next="next"
      @change="changeBanner"
      @click="bannerClick"/>
    <div
      id="nav"
      :class="searchBarFixed == true ? 'isFixed' :''"
      class="nav-sec-menu">
      <nav>
        <p
          v-for="(item, index) in dataType"
          :key="index"
          :class="listQuery.tagId == item.id?'active':''"
          @click="chooseSelect(item)">{{ item.name }}</p>
      </nav>
    </div>
    <ul
      v-if="dataDetail.length"
      style="padding-top: 0;margin-top: 20px;"
      class="main-padding">
      <router-link
        v-for="item in dataDetail"
        :to="{ path: 'tourDetail', query:{id: item.id }}"
        :key="item.id"
        class="flex-jcb tour-item">
        <img
          :src="item.cover"
          class="tour-item-img">
        <div class="tour-list">
          <div class="ellipsis2 tour-item-name">{{ item.name }}</div>
        </div>
      </router-link>
    </ul>
    <p
      v-if="preventRepeatReuqest"
      class="loading">加载中……</p>
    <p id="empty"/>
    <p
      v-if="touchend"
      class="empty_data">——共{{ totalCount?totalCount:0 }}个旅行路线——</p>
    <go-top/>
    <Footer v-if="touchend"/>
  </div>
</template>

<script>
  import config from "~/config";
  import Footer from "~/components/footer";
  import Head from "~/components/head";
  import axios from '../plugins/axios'
  import SecondBanner from '~/components/secondBanner'
  import {
    mapGetters
  } from "vuex";
  import GoTop from "~/components/goTop";
  export default {
    components: {
      Footer,
      Head,
      SecondBanner,
      GoTop
    },
    data(){
      return{
        showBanner: !0,
        looptime: 4000, // 循环时间
        touchend: false,////没有更多数据
        nodata: `${config.IMG_URL}nodata.png`,
        dataType: [],
        totalCount: "",
        dataDetail: [],
        width: 400,
        height:200,
        background: 'red',
        color: '#fff',
        fontSize: '70px',
        listQuery: {
          tagId: "",
          page: 1,
          limit: 10
        },
        store: "",
        dataNull:false,
        tourList: [],
        currentPage: 1,//当前加载页
        searchBarFixed: false,
        preventRepeatReuqest: false, //到达底部加载数据，防止重复加载
      };
    },
    computed: {
      ...mapGetters(["userInfo"])
    },
    created() {
      // handleScroll为页面滚动的监听回调
      window.addEventListener('scroll', this.handleScroll);
      this.getType();
      this.preventRepeatReuqest = true;
      this.getAll();
    },
    async asyncData() {
      const { data } = await axios.get('/api/app/banner/app/unauth/findBannerlist?type=1&platform=1');
      let cc = data.data;
      return { bannerList: cc}
    },
    mounted(){
    },
    methods: {
      handleScroll: function () {
        let path = $nuxt.$route.path;
        if(path == '/tour') {
          let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
          // 设备/屏幕高度
          let scrollObj = document.getElementById("scroll-area"); // 滚动区域
          let menuTop = document.querySelector('#nav').offsetTop;
          let empty = document.querySelector('#empty').offsetTop;
          if (scrollTop > menuTop-44 && empty > document.documentElement.clientHeight) {
            this.searchBarFixed = true;
          }
          if (scrollTop < 80) {
            this.active = 'activity';
            this.searchBarFixed = false;
          }
          if (scrollObj) {
            let scrollHeight = scrollObj.scrollHeight; // 滚动条的总高度
            //滚动条到底部的条件
            if (this.totalCount>(this.listQuery.page-1)*this.listQuery.limit) {
              if (this.touchend) {
                return;
              }
              //防止重复请求
              if (this.preventRepeatReuqest) {
                return;
              }
              this.preventRepeatReuqest = true;
              //数据的定位加20位
              this.listQuery.page = this.currentPage + 1;
              this.getAll();
            }
          }
        }
      },

      chooseSelect(e) {
        this.listQuery.tagId = e.id;
        this.preventRepeatReuqest = true;
        this.dataDetail = [];
        this.listQuery.page = 1;
        document.documentElement.scrollTop = 0;
        this.touchend = false;
        this.getAll();
      },
      getType() {
        let t = this;
        axios.get('/api/uccn/app/unauth/tour/tagList')
          .then(function(response) {
            t.dataType = response.data.data;
            t.dataType.unshift({id:"",name:'全部'})
        });
      },
      getAll() {
        let t = this;
        axios.get('/api/uccn/app/unauth/tour/getGoodList?tagId='+this.listQuery.tagId+'&page='+this.listQuery.page+'&limit='+this.listQuery.limit)
          .then(function(response) {
            t.totalCount = response.data.data.totalCount;
            let list = response.data.data.data;
            t.dataDetail = [...t.dataDetail, ...list];
//            t.dataDetail = response.data.data.data;
            t.currentPage = response.data.data.pageNum;
            if (response.data.data.totalCount < t.listQuery.limit*t.listQuery.page) {
              t.touchend = true;
            }
            t.preventRepeatReuqest = false;
          });
      },
      // 点击下一页回调
      prev () {},
      // 点击下一页回调
      next () {},
      // 鼠标移入状态点回调
      changeBanner () {},
      bannerClick () {},
    },
    head () {
      return {
        title: "欣新房车旅游|房车自驾游|房车旅行|房车一日游|房车出行|房车旅游景点|房车旅行攻略|房车旅游APP"
      }
    }
  };

</script>

<style lang="scss">
  @import "../assets/styles/mixin";
  .tour-container{
    nav{
      display: -webkit-box;
      display: -ms-flexbox;
      display: flex;
      -webkit-box-align: center;
      -ms-flex-align: center;
      align-items: center;
      overflow: auto;
    }
    nav::-webkit-scrollbar { width: 0 !important }
    .nav-sec-menu{
      border-bottom: 1px solid #dfdfdf;
    }
    .nav-sec-menu.isFixed{
      position: fixed;
      top: 44px;
      background: #fff;
      height: 64px;
      width: 100%;
      border-bottom: 1px solid #dfdfdf;
    }
    p{
      text-align: center;
      font-size: 14px;
      color: #999;
      flex-shrink: 0;
      margin: 27px 13px 0px 13px;
      padding-bottom: 13px;
    }
    p.active{
      border-bottom: 2px solid #1bbb9f;
      font-size: 16px;
      color: #333;
    }
    .tyle-list-li{
      padding-right:10px;
      display: inline-block;
    }
    .tour-item{
      margin: 17px 0;
    }
    .tour-item-name{
      font-size: 15px;
      font-weight: bold;
    }
    .tour-item-img{
      width: 92px;
      height: 75px;
      object-fit: cover;
      border-radius: 3px;
    }
    .tour-list{
      border-bottom: 1px solid #dfdfdf;
      margin-left: px2rem(20px);
      width: 100%;
    }
  }

</style>
