<template>
  <!-- #ifdef H5 -->
  <view>
    <view class="bg-container"></view>
    <zj-page-layout :hasFooter="false" :isScroll="true" :refresherTriggered="refresherTriggered"
      @refresherrefresh="refresherrefresh" @scrolltolower="scrolltolower">
      <!-- <template slot="header">
        <view class="header-container" :style="cuStyle">二手空调</view>
      </template> -->

      <view class="all-container">
        <view class="search-container">
          <u-search placeholder="输入商品名称搜索" :showAction="false" clearabled v-model="keyword" @search="handleSearch">
          </u-search>
        </view>

        <view class="swiper-container card mt30">
          <u-swiper :list="bannerList" indicator indicatorMode="line" circular></u-swiper>
        </view>

        <view class="category-container card mt30">
          <view class="item" v-for="(item, index) in categoryList" :key="index" @tap="toGoodsList(item.categoryId)">
            <image :src="imageUrl + item.imgUrl"></image>
            <view class="name">{{item.categoryName}}</view>
          </view>
        </view>

        <view class="tab-container mt30">
          <u-tabs :list="tabList" @click="clickTab" :current="tabCurrent" :scrollable="false" lineColor="#01C30B">
          </u-tabs>
        </view>

        <view class="common-goods-list">
          <view class="item card" v-for="(item, index) in dataList" :key="index" @tap="toGoodsDetail(item.id)">
            <view class="top">
              <image src="@/static/common/logo.png"></image>
              <view class="user">
                <view class="name">{{item.userName}}</view>
                <view class="time">{{item.createTime}}</view>
              </view>
              <view class="price">{{item.amount | priceFilter2}}</view>
            </view>
            <view class="title">{{item.title}}</view>
            <view class="des">{{item.content}}</view>
            <view class="imgs">
              <image :src="imageUrl + it.imgUrl" v-for="(it, idx) in item.goodsFiles" :key="idx"></image>
            </view>
            <view class="bottom">
              <view class="left-location"><text class="iconfont icon-dingwei"></text>{{item.address}}</view>
              <view class="right-stats">
                <view class="it">
                  <text class="iconfont icon-dianzan"></text>
                  <text class="text">{{item.up || 0}}</text>
                </view>
                <view class="it">
                  <text class="iconfont icon-shoucang"></text>
                  <text class="text">{{item.collectNum || 0}}</text>
                </view>
                <view class="it">
                  <text class="iconfont icon-liulan"></text>
                  <text class="text">{{item.visit || 0}}</text>
                </view>
              </view>
            </view>
          </view>
        </view>

        <Loading :loadStatus="loadStatus" :dataList="dataList" />

      </view>

    </zj-page-layout>
  </view>
  <!-- #endif -->
  <!-- #ifndef H5 -->
  <web-view :src="webViewHref('/pages/index/index')"></web-view>
  <!-- #endif -->
</template>

<script>
  export default {
    data() {
      return {
        imageUrl: this.$imageUrl,
        dataList: [],
        pageNum: 1,
        loadStatus: 0,
        refresherTriggered: false,
        keyword: '',
        bannerList: [],
        categoryList: [],
        tabList: [{
            name: '默认顺序',
            value: 0
          },
          {
            name: '在售商品',
            value: 1
          },
          {
            name: '离我最近',
            value: 2
          },
          {
            name: '人气最高',
            value: 3
          },
        ],
        tabCurrent: 0,

        lat: '',
        lng: '',
      }
    },

    computed: {
      cuStyle() {
        return `height:${this.CustomBar-this.StatusBar}px; padding-top:${this.StatusBar}px;`
      },
    },

    async onLoad() {
      await this.getBanner();
      await this.getCategory();
      await this.getList();
      let lo = await this.$getLocation();
      this.lat = lo.latitude;
      this.lng = lo.longitude;
    },

    methods: {
      async getBanner() {
        // let res = await this.$api.get('/carouselMap/list/page', {
        //   pageNum: 1,
        //   pageSize: -1,
        //   state: true
        // })
        // this.bannerList = res.data.records;
        // return res.data.records

        return new Promise((resolve, reject) => {
          this.$api.get('/carouselMap/list/page', {
            pageNum: 1,
            pageSize: -1,
            state: true
          }).then(res => {
            if(res.data && res.data.records) {
              this.bannerList = res.data.records.map(item => {
                return this.imageUrl + item.imgSrc;
              });
            }
          }).finally(res => {
            resolve(1);
          })
        })
      },

      async getCategory() {
        return new Promise((resolve, reject) => {
          this.$api.get('/goods/category/list')
          .then(res => {
            this.categoryList = res.data || [];
          }).finally(res => {
            resolve(1);
          })
        })
      },

      //获取列表数据
      async getList() {
        let params = {};
        if(this.tabCurrent === 1) {
          params.status = '1';
        }else if(this.tabCurrent === 2) {
          params.lng = this.lng;
          params.lat = this.lat;
        }else if(this.tabCurrent === 3) {
          params.visitOrderBy = 'YES';
        }

        this.$api.get('/goods/list', {
          pageNum: this.pageNum,
          pageSize: 10,
          ...params
        }).then(res => {
          this.loadStatus = 0;
          let list = res.data.records;
          if (list.length < 10) {
            this.loadStatus = 2;
          }
          this.dataList = this.dataList.concat(list);
        }).catch(() => {
          this.loadStatus = 2;
        })
      },

      clickTab(item) {
        this.tabCurrent = item.value;
        this.refreshList();
      },

      // 滚动到底部
      scrolltolower(e) {
        if (this.loadStatus === 0) {
          this.pageNum++;
          this.getList();
        }
      },

      // 触发下拉刷新
      async refresherrefresh(e) {
        this.refresherTriggered = true;
        await this.getBanner();
        await this.getCategory();
        this.refreshList();
        this.refresherTriggered = false;
      },

      refreshList() {
        this.dataList = [];
        this.pageNum = 1;
        this.getList();
      },

      handleSearch() {
        this.$navToPage({
          url: `/pages/goods/list?keyword=${this.keyword}`
        })
        this.keyword = '';
      },

      toGoodsList(categoryId) {
        this.$navToPage({
          url: `/pages/goods/list?categoryId=${categoryId}`
        })
      },

      toGoodsDetail(id) {
        this.$navToPage({
          url: `/pages/goods/detail?id=${id}`
        })
      },

    }
  }
</script>

<style lang="scss" scoped>
  .all-container {
    padding: 0 30rpx 30rpx;
  }

  .header-container {
    font-size: 32rpx;
    color: #ffffff;
    height: 88rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    font-weight: 600;
    position: relative;
  }

  .bg-container {
    position: fixed;
    top: 0;
    left: 0;
    width: 750rpx;
    height: 750rpx;
    background: linear-gradient(to bottom, $theme-color, #f4f2f2);
  }

  .card {
    @include zj-card;
  }

  .swiper-container {
    border-radius: 20rpx;
    overflow: hidden;
  }

  .category-container {
    display: flex;
    padding: 30rpx 0;

    .item {
      width: 25%;
      display: flex;
      align-items: center;
      flex-direction: column;

      image {
        width: 44rpx;
        height: 44rpx;
      }

      .name {
        font-size: 28rpx;
        margin-top: 16rpx;
      }
    }
  }
</style>
