<import name="x-book" src="../../components/book/book.ux"></import>

<template>
  <div class="home-wrap">
    <!-- <div class="home_title"><text>精选</text></div> -->
    <div class="home_title">
      <div class="type-wrap_content">
        <div class="type-wrap">
          <block for="type in typeList">
            <div class="type-item" @click="typeChange(type.index)">
              <text class="{{type.index === current ? 'cur' : ''}}">{{
                type.name
              }}</text>
              <text class="choose-bar" show="{{type.index === current}}"></text>
            </div>
          </block>
          <!-- <text class="feebar-text"  @click="toPage('Free')">限时免费</text> -->
        </div>
        <!-- <div class="search-bar">
          <div class="search-bar__wrap">
            <text @click="toCategory">请输入书名或者作者名</text
            ><image
              src="https://yueduyun.oss-cn-hangzhou.aliyuncs.com/xiaochengxu/img/search.png"
            ></image>
          </div>
        </div> -->
      </div>
    </div>
    <!-- <div class="type-bar">
      <div class="search-bar__wrap">
        <text @click="toCategory">请输入书名或者作者名</text
        ><image
          src="https://yueduyun.oss-cn-hangzhou.aliyuncs.com/xiaochengxu/img/search.png"
        ></image>
      </div>
    </div> -->
    <list class="list-content">
      <list-item type="swipe" class="swipe-bar">
        <swiper class="swipe-bar" autoplay="true" indicator="false">
          <block for="b in banner">
            <div class="swipe-item">
              <image
                class="cover"
                src="{{b.banner_url}}"
                @click="pageRouter(b)"
              ></image>
            </div>
          </block>
        </swiper>
      </list-item>
      <list-item class="type-bar">
        <div class="type-bar-item">
          <image src="../../assets/imgs/classify_img.png" @click="jump('Category')"></image>
          <image src="../../assets/imgs/list_rank.png" @click="jump('Leaderboard')"></image>
        </div>
      </list-item>
      <list-item class="type-bar">
        <div class="function-bar-item">
          <image src="../../assets/imgs/search_img.png" @click="toCategory"></image>
          <image src="../../assets/imgs/task_welfare.png" @click="toTaskWelfare('Task')"></image>
          <image src="../../assets/imgs/limit_free.png"  @click="jump('Free')"></image>
          <image src="../../assets/imgs/recent_read.png" @click="toMyRead"></image>
        </div>
      </list-item>
      <!-- <list-item type="entrance">
        <div class="entrance_block">
          <div>
            <image
              src="../../assets/imgs/to_search.png"
              @click="toPage('Search')"
            ></image>
          </div>
          <div>
            <image
              src="../../assets/imgs/to_task.png"
              @click="toPage('Task')"
            ></image>
           
          </div>
          <div>
            <image
              src="../../assets/imgs/to_rank.png"
              @click="toPage('Leaderboard')"
            ></image>
          </div>
        </div>
      </list-item> -->
      <block for="bookObj in list">
        <list-item type="simple" class="book-list__wrap" if="$idx % 3 === 0">
          <div class="book-list_content">
            <div class="book-list__title">
              <text class="border"></text>
              <text>{{ bookObj.lable }}</text>
            </div>
            <div class="book-list__multi">
              <block for="book in bookObj.books.slice(4)">
                <div class="book-item">
                  <x-book multi="{{true}}" width="{{150}}" book="{{book}}">
                    <div class="book-info__multi">
                      <text class="name">{{ book.book_name }}</text>
                      <text class="intro">{{ book.book_summary }}</text>
                    </div>
                  </x-book>
                </div>
              </block>
            </div>
            <div class="book-list__line">
              <block for="book in bookObj.books.slice(0, 4)">
                <x-book
                  multi="{{false}}"
                  width="{{150}}"
                  lines="{{true}}"
                  book="{{book}}"
                ></x-book>
              </block>
            </div>
          </div>
        </list-item>
        <list-item
          type="multi"
          class="book-list__wrap book-list__wrap--multi"
          else
        >
          <div class="book-list_content">
            <div class="book-list__title">
              <text>{{ bookObj.lable }}</text>
            </div>
            <div class="book-list">
              <block for="book in bookObj.books">
                <x-book multi="{{false}}" book="{{book}}"></x-book>
              </block>
            </div>
          </div>
        </list-item>
      </block>
      <list-item style="height:120px;"></list-item>
    </list>
  </div>
</template>

<script>
import { getHomeList , getBannerData,checkAdvertisement} from '../../api'

import storage from '@system.storage'
import router from '@system.router'

export default {
  props: {
    tabindex: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      typeList: [
        {
          name: '男生',
          index: 0
        },
        {
          name: '女生',
          index: 1
        }
      ],
      banner: [],
      list: [],
      current: 1
    }
  },
  async onInit() {
    await this.getCityListByTabChange()
    this.$on('refreshData', this.broadevt)
    this.$watch('current', 'getCityListByTabChange')
    this.$watch('tabindex', 'watchPropsChange')
  },
  typeChange(index) {
    this.current = index
  },
  watchPropsChange(v) {
    if (v === 0) this.getCityListByTabChange(this.current)
  },
  async broadevt(evt) {
    if (evt.detail.current == 0) {
      await this.getCityListByTabChange(this.current)
    }
  },
  toCategory() {
    //this.$emit('change');
    router.push({
      uri: '/views/Search'
    })
  },
  pageRouter(page) {
    if(page.ids){
      try{
        let type = page.type ? "default" : "";
        checkAdvertisement(type,page.ids);
        // console.log('type',type);
      }catch(e){

      }
    }
    if (page.redirect_url && page.redirect_url != "#") {
      if (page.redirect_type == "book") {
        router.push({
          uri: page.redirect_url,
          params: {
            bid: page.bid,
            chapter_id: page.cid
          }
        })
      } else if (page.redirect_type == "activity") {
        router.push({
          uri: page.redirect_url,
          params:page.param
        })
      }
    }
  },
  toTaskWelfare(page) {
    router.push({
      uri: `/views/${page}`,
    })
  },
  toMyRead() {
    this.$emit('change')
  },
  jump(page) {
    router.push({
      uri: '/views/' + page
    })
  },toPage(page) {
    router.push({
      uri: '/views/' + page,
      params: {
        sex: this.current
      }
    })
  },toCategory() {
    router.push({
      uri: '/views/Search',
    })
  },
  async getCityListByTabChange(index = 1) {
   await storage.set({ key: 'sex', value: index || 0 })
    let sex = index ? 'female' : 'male'
    let ret = await getHomeList(sex)
    // this.banner = ret.slice(0, 1)[0]
    this.list = ret.slice(1)
    let banner = await getBannerData(sex == "female" ? 2 : 1);
    this.banner = banner;
  }
}
</script>

<style lang="less">
@import "../../assets/less/home.less";

</style>
