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

<template>
  <div class="home-wrap">
    <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>
      </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="entrance">
        <div class="entrance_block">
          <div>
            <image
              src="../../assets/imgs/home_read.png"
              @click="toMyRead"
            ></image>
            <text>最近阅读</text>
          </div>
          <div>
            <image
              src="../../assets/imgs/leader_board.png"
              @click="toPage('Leaderboard')"
            ></image>
            <text>排行榜</text>
          </div>
          <div>
            <image
              src="../../assets/imgs/home_free.png"
              @click="toPage('Free')"
            ></image>
            <text>限时免费</text>
          </div>
        </div>
      </list-item>
      <list-item type="swipe">
        <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>

      <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
        })
      }
    }
  },
  toMyRead() {
    this.$emit('change')
  },
  jump(page) {
    router.push({
      uri: '/views/' + page
    })
  },toPage(page) {
    router.push({
      uri: '/views/' + page,
      params: {
        sex: this.current
      }
    })
  },
  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"; */
.home-wrap {
  background-color: #f5f5f5;
  flex-direction: column;
  .home_title {
    height: 112px;
    background-color: #fff;
    text {
      padding: 0 30px;
      font-size: 36px;
      color: #E5401C;
    }
  }
  .type-wrap_content {
    background-color: #fff;
    .type-wrap {
      background-color: #fff;
      margin: 20px 0;
      flex-direction: row;
      .type-item {
        padding: 0 15px;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        position: relative;
        text {
          font-size: 30px;
          color: #d7d7ff;
        }

        .cur {
          color: #E5401C;
          font-size: 36px;
          font-weight: bold;
        }

        .choose-bar {
          width: 38px;
          height: 4px;
          background-color: #E5401C;
          border-radius: 2px;
          margin-top: 10px;
        }
      }
    }
  }
  .free {
    background-color: #ffffff;
    width: 100%;
    padding-top: 10px;
    .free_warp {
      margin: 0 auto;
    }
  }
  .entrance_block {
    background-color: #ffffff;
    width: 100%;
    padding: 30px;
    display: flex;
    justify-content: space-around;
    text-align: center;
    div {
      flex-direction: column;
      text {
        margin-top: 10px;
        text-align: center;
      }
      image {
        margin: 0 auto;
        height: 80px;
        width: 80px;
      }
    }
  }

  .type-bar {
    background-color: #ffffff;
    padding: 20px;
    .search-bar__wrap {
      flex: 1;
      background-color: #f7f7f7;
      height: 62px;
      padding: 0 14px;
      border-radius: 30px;
      align-items: center;

      image {
        margin-right: 20px;
        width: 36px;
        height: 36px;
      }

      text {
        flex: 1;
        font-size: 24px;
        color: #999;
      }
    }
  }
  .search-bar {
    padding: 24px;

    .search-bar__wrap {
      flex: 1;
      background-color: #f7f7f7;
      height: 62px;
      padding: 0 14px;
      border-radius: 30px;
      align-items: center;

      image {
        margin-right: 20px;
        width: 36px;
        height: 36px;
      }

      text {
        flex: 1;
        font-size: 24px;
        color: #999;
      }
    }
  }

  .swipe-bar {
    height: 260px;
    background-color: #fff;
    .swipe-item {
      padding: 0px 24px;
      .swipe-item__info {
        flex: 1;
        flex-direction: column;
        padding-right: 32px;

        .name {
          font-size: 32px;
          color: #333;
        }

        .intro {
          font-size: 26px;
          color: #666;
          margin-top: 36px;
          lines: 4;
          text-overflow: ellipsis;
        }

        .category {
          font-size: 24px;
          color: #32a1ff;
          margin-top: 10px;
        }
      }

      .cover {
        width: 100%;
        height: 260px;
        // flex-basis: 180px;
        // flex-shrink: 0;
        // height: 240px;
        border-radius: 6px;
      }
    }
  }

  .book-list__wrap {
    padding: 0 24px;
    flex-direction: column;
    .book-list_content {
      border-radius: 20px;
      padding: 0 24px;
      flex-direction: column;
      background-color: white;
      margin-top: 30px;
      .book-list__title {
        background-color: #fff;
        padding: 40px 0 30px 0;
        align-items: center;
        font-size: 30px;

        text {
          color: #333;
          font-size: 32px;
          font-weight: 900;
        }
      }
    }
  }

  .book-list {
    flex: 1;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: space-between;
  }

  .book-list__multi {
    flex-direction: column;

    .book-item {
      margin-bottom: 30px;
    }

    .book-info__multi {
      flex: 1;
      flex-direction: column;
      justify-content: flex-start;
      align-items: flex-start;
      margin-left: 38px;
      // margin-bottom: 40px;

      .name {
        font-size: 32px;
        color: #333;
        lines: 1;
        text-overflow: ellipsis;
      }

      .intro {
        font-size: 24px;
        color: #666;
        lines: 3;
        text-overflow: ellipsis;
        margin-top: 36px;
        line-height: 36px;
      }
    }
  }

  .book-list__line {
    justify-content: space-between;
    align-items: flex-start;
    border-bottom: 2px solid #f7f7f7;
  }
}
</style>
