<template>
  <view class="width-100 height-100 view-page">
    <u-tabs ref="tabs" :list="tabList" :scrollable="false" :current="currentTab" @change="tabChange" lineColor="#65371b"
      :activeStyle="{color:'#65371b'}" inactiveStyle="{color:'#65371b'}"></u-tabs>
    <u-list @scrolltolower="scrolltolower" height="calc(100% - 91px)" lowerThreshold="10">
      <view class="info-list">
        <view class="info-item" v-for="(item, index) in list" :key="index" @click="toDetail(item.collectId)">
          <view class="info-pic">
            <u-image width="96px" height="96px" mode="aspectFill" :src="item.cover"></u-image>
          </view>
          <view class="info-title">
            {{item.name}}
          </view>
        </view>
      </view>
      <u-loadmore :status="page.status" :loading-text="page.loadingText" :loadmore-text="page.loadmoreText"
        :nomore-text="page.nomoreText" />
    </u-list>
  </view>
</template>

<script>
  import themeMixins from '@/pages/yuecai/mixins/themeMixins.js';
  export default {
    mixins: [themeMixins],
    data() {
      return {
        page: {
          loading: false,
          current: 1,
          size: 10,
          total: 0,
          // loadmore loading nomore
          status: 'loadmore',
          loadingText: '正在加载中',
          loadmoreText: '上拉加载更多',
          nomoreText: '没有更多了'
        },
        keyword: '',
        list: [],
        tabList: [{
          name: '名店'
        }, {
          name: '名菜',
        }, {
          name: '名点',
        }],
        currentTab: 0
      }
    },
    onLoad(options) {
      this.collectionType = options.type || 'restaurant'
      switch (options.type) {
        case 'restaurant':
          this.currentTab = 0;
          break;
        case 'dish':
          this.currentTab = 1;
          break;
        case 'snacks':
          this.currentTab = 2;
          break;
        default:
          this.currentTab = 0;
          break;
      }
    },
    onShow() {
      this.keyword = '';
      this.doRefresh();
    },
    methods: {
      tabChange(e) {
        this.currentTab = e.index;
        switch (e.index) {
          case 0:
            this.collectionType = 'restaurant';
            break;
          case 1:
            this.collectionType = 'dish';
            break;
          case 2:
            this.collectionType = 'snacks';
            break;
          default:
            this.collectionType = 'restaurant';
            break;
        }
        this.doRefresh();
      },
      scrolltolower() {
        this.loadmore()
      },
      loadmore() {
        this.page.current++;
        this.getList();
      },
      doRefresh() {
        this.page.current = 1;
        this.page.status = 'loadmore'
        this.list = [];
        this.getList();
      },
      getList() {
        if (this.page.status == 'loading' || this.page.status == 'nomore') {
          // 防止重复下拉
          return;
        }
        this.page.status = 'loading';
        //
        this.$u.api.getCollectionList(this.page.current, this.page.size, {
          collectType: this.collectionType || ''
        }).then(res => {
          if (res.data.records && res.data.records.length > 0) {
            this.list.push(...res.data.records)
          }
          this.page.total = res.data.total || 0;
          if (this.page.size * this.page.current >= res.data.total) {
            this.page.status = 'nomore'
          } else {
            this.page.status = 'loadmore'
          }
        }).catch(e => {
          this.page.status = 'loadmore'
        })
      },
      toDetail(id) {
        if (this.collectionType == 'dish') {
          uni.navigateTo({
            url: "/pages/yuecai/dish/detail/index?id=" + id,
          })
        } else if (this.collectionType == 'snacks') {
          uni.navigateTo({
            url: "/pages/yuecai/snacks/detail/index?id=" + id,
          })
        } else if (this.collectionType == 'restaurant') {
          uni.navigateTo({
            url: "/pages/yuecai/restaurant/detail/index?id=" + id,
          })
        }
      },
      deleteCollection(id, index) {
        if (this.loading) {
          return;
        }
        this.loading = true;
        this.$u.api.deleteCollection(id).then(res => {
          this.list.splice(index, 1);
          this.loading = false;
        }).catch(e => {
          this.loading = false;
        })
      }
    },
  }
</script>

<style scoped lang="scss">
  .view-page {
    overflow: hidden;
  }

  .info-list {
    display: flex;
    flex-wrap: wrap;

    .info-item {
      padding: 20rpx;
      width: 33%;
      box-sizing: border-box;

      .info-pic {
        margin: auto;
        width: 175rpx;
        height: 175rpx;
        background: bisque;
      }

      .info-title {
        word-break: break-all;
        text-align: center;
        color: #65371b;
        font-size: 24rpx;
        font-weight: 300;
        line-height: 47rpx;
        word-spacing: 2rpx;
        overflow: hidden;
        text-overflow: -o-ellipsis-lastline;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 1;
        line-clamp: 3;
        -webkit-box-orient: vertical;
      }
    }
  }
</style>