@extends('wap-browser.template')

@section('head')
<link rel="stylesheet" href="/wapbrowser/web/component.css">
<style>
  .stock-limit{
    margin-top: .24rem;
    padding: 0 .24rem;
    font-size: .24rem;
    overflow: hidden;
    transition: height .3s;
  }
  .stock-limit__type {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: start;
    -ms-flex-align: start;
    align-items: flex-start;
    -webkit-box-orient: horizontal;
    -webkit-box-direction: normal;
    -ms-flex-direction: row;
    flex-direction: row;
    -webkit-box-pack: justify;
    -ms-flex-pack: justify;
    justify-content: space-between;
  }
  .stock-limit__label {
    width: 1.2rem;
    line-height: .5rem;
  }
  .stock-limit__items {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    -webkit-box-orient: horizontal;
    -webkit-box-direction: normal;
    -ms-flex-direction: row;
    flex-direction: row;
    -webkit-box-pack: start;
    -ms-flex-pack: start;
    justify-content: flex-start;
    width: 5.8rem;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
  }
  .stock-limit__item {
    width: 22%;
    box-sizing: border-box;
    border: 1px solid #999;
    color: #999;
    border-radius: .25rem;
    height: .5rem;
    line-height: .5rem;
    text-align: center;
    margin-bottom: .3rem;
    margin-right: 4%;
  }
  .stock-limit__item:nth-child(4n) {
    margin-right: 0;
  }
  .stock-limit__item.is-active {
    border-color: #ff6060;
    color: #ff6060;
  }
  .book-stock-switch {
    margin-top: -.2rem;
    background-color: #fff;
  }
  .book-stock-switch__content {
    text-align: center;
    color: #999;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    -webkit-box-orient: horizontal;
    -webkit-box-direction: normal;
    -ms-flex-direction: row;
    flex-direction: row;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    justify-content: center;
    height: 2.5em;
  }
  .book-stock-switch__icon {
    width: 1em;
    margin-left: .5em;
  }
  .book-stock-switch__shadow {
    width: 7.5rem;
    height: .2rem;
    background: radial-gradient(3.75rem .1rem at 50% 0,#f0f0f0,#fff);
  }
  .x-book--stock{
    margin: .36rem .24rem;
  }

</style>
@endsection

@section('body')
  @component('wap-browser.component.header',['name'=> $title])
  @endcomponent
  <!-- 输入框   -->
  <!-- 输入框   -->
  <div class="stock-limit">
    <dl class="stock-limit__type">
      <dt class="stock-limit__label">频道</dt>
      <dd class="stock-limit__items stock-limit__items--channel">
      </dd>
    </dl>
    <dl class="stock-limit__type stock-limit__type--category">
      <dt class="stock-limit__label">分类</dt>
      <dd class="stock-limit__items stock-limit__items--category">
      </dd>
    </dl>
    <dl class="stock-limit__type">
        <dt class="stock-limit__label">是否完结</dt>
        <dd class="stock-limit__items stock-limit__items--status">  
        </dd>
      </dl>
  </div>
  <div class="book-stock-switch">
    <div class="book-stock-switch__content">
      <span>收回筛选项</span> 
      <img src="data:image/png;base64, " class="book-stock-switch__icon"></div> 
      <div class="book-stock-switch__shadow">
    </div>
  </div>
  <div class="stock-list">

  </div>
  <div class="spinner" style="heigth: 30px; overflow: hidden;">
    <div class="mint-spinner-snake" style="margin: 0  auto;border-top-color: rgb(38, 162, 255); border-left-color: rgb(38, 162, 255); border-bottom-color: rgb(38, 162, 255); height: 28px; width: 28px;"></div>
  </div>
@endsection

@section('foot')
<script>
  var categories = @json($categories);
  categories.unshift({
    id: 0,
    name: '不限'
  })
  categories.map(function(item){
    if(item.children){
      item.children.unshift({
        id: item.id,
        name: '不限'
      })
    }
  })

  var stats = [
    { id: -1, name: '不限' },
    { id: 1, name: '完结' },
    { id: 0, name: '连载' }
  ]

  var channel = '1' , category = '1', stat = '1', page = '1', last = false

  function createItem(list,event){
    return list.map(function(v){
      var item = document.createElement('div')
      item.setAttribute('class','stock-limit__item')
      item.setAttribute('data-id',v.id)
      item.innerHTML = v.name
      addEvent(item,'click',event)
      return item
    })
  }

  function isActivbe(nodes,id){
    nodes.map(function(node){
      if(node.getAttribute('data-id') === id){
        addClass(node,'is-active')
      }else{
        removeClass(node,'is-active')
      }
    })
  }

  function setHeight(hide){
    var dom = document.querySelector('.stock-limit')
    var limit = document.querySelector('.book-stock-switch__content span')
    if(hide &&  dom.offsetHeight){
      dom.style.height = 0
      limit.innerHTML = '收回筛选项'
    }else{
      var h = Math.ceil(categories.length / 4) 
        + (categories[channel].children ?  Math.ceil(categories[channel].children.length / 4) : 0)
        + Math.ceil(stats.length / 4) 
      limit.innerHTML = '展开筛选项'        
      dom.style.height =  h * 0.8 + "rem"
    }
  }
  
  function initChannel(channel_id){
    var dom = document.querySelector('.stock-limit__items--channel')
    var items = createItem(categories,function(e){
      var id =  e.target.getAttribute('data-id')
      if(id !== channel){
        channel = id
        category = id
        page = 1
        initCategory(id)
        isActivbe(items,id)
        setHeight()
        getList()
      }
    })
    items.map(function(item,i){
        dom.appendChild(item)
      })
    isActivbe(items,channel_id) 
  }

  function initCategory(channel_id){
    var dom = document.querySelector('.stock-limit__items--category')
    var parent = document.querySelector('.stock-limit__type--category')
    if(categories[channel_id].children){  
      dom.innerHTML = ''
      parent.style.display = ''
      var items = createItem(categories[channel_id].children,function(e){
        var id = e.target.getAttribute('data-id')
        category = id
        page = 1
        getList()        
        isActivbe(items,id)
      })
      items.map(function(item){
        dom.appendChild(item)
      })
      isActivbe(items,channel_id) 
    }else{
      parent.style.display = 'none'
    }
  }

  function initStatus(){
    var dom = document.querySelector('.stock-limit__items--status')
    var items = createItem(stats,function(e){
      var id = e.target.getAttribute('data-id')
      stat = id
      page = 1      
      getList()      
      isActivbe(items,id)
    })
    items.map(function(item){
        dom.appendChild(item)
      })
    isActivbe(items,stat)
  }


  function createBook(book){
    var a = document.createElement('a')
    a.setAttribute('href','/detail?bid='+ book.book_id)
    a.innerHTML = '\
      <dl class="x-book x-book--multi x-book--stock">\
        <dt class="x-book__coverbox" style="width: 1.8rem;height: 2.4rem">\
          <img src="'+book.cover_url+'" class="x-book__cover" style="width: 1.8rem;height: 2.4rem">\
        </dt>\
        <dd class="x-book__text--multi" style="width: calc(100% - 1.8rem);height: 2.4rem">\
          <div class="x-book-info">\
            <p class="x-book-info__name">'+ book.book_name +'</p>\
            <p class="x-book-info__flex">\
              <span class="x-book-info__author">'+ book.book_author +'</span>\
              <span class="x-book-info__finish"> '+ (book.book_end_status ? '完结' : '连载') +' </span>\
            </p>\
            <p class="x-book-info__count">字数：'+ book.book_word_count +'</p>\
            <p class="x-book-info__flex">\
              <span class="x-book-info__lastest">\
                <span class="red">最新：'+ book.last_chapter +'</span></span>\
                '+(book.last_chapter_is_vip ? '<img src="/wapbrowser/web/vip.png" class="x-book-info__vip">' : '<img src="/wapbrowser/web/vip.png" class="x-book-info__vip">')+'\
            </p>\
          </div>\
        </dd>\
      </dl>'
    return a 
  }

  function queryString(obj) {
    let str = ''
    for (k in obj){
      str = k+'='+obj.k+'&'
    }
    if(str){
      str = str.substr(0,str.length-1)
    }
    return str;
  }
  function getList(append){
    var wrap = document.querySelector('.stock-list')    
    if(getList.prev) getList.prev.xhr.abort()
    if(!append) wrap.innerHTML = ''    
    var params = {
      category_id: category,
      page: page
    }
    params.stat === -1 ? (params.stat = stat) : null
    var url = '/api/books/library?' +  queryString(params)
    function callback(json){
      getList.loading = false      
      json.data.list.map(function(book){
        wrap.appendChild(createBook(book))
      })
      if( json.data.meta.last_page === json.data.meta.current_page ) {
        last = true
        document.querySelector('.spinner').style.display = 'none'
      }else{
        last = false
        document.querySelector('.spinner').style.display = ''        
      }
    }
    getList.prev = {
      params : params,
      xhr : getJSON(url,callback)
    }
    getList.loading = true
  }

  initChannel(channel)
  initCategory(channel,category)
  initStatus(stat)
  setTimeout(setHeight,0)
  getList()
  
  addEvent( document.querySelector('.book-stock-switch'),'click',function () {
    setHeight(true)
  })

  addEvent(window, 'scroll', function(e){
    if(getList.loading || last) return
    var scrollbottom = Math.max(window.pageYOffset || 0, document.documentElement.scrollTop) + document.documentElement.clientHeight
    if(scrollbottom + 50 > document.body.offsetHeight){
      page++
      getList(true)
    }
  })

</script>
@endsection