<template>
  <view class="container">
    <!-- 空白页 -->
    <view style="padding: 150rpx 0rpx;" v-if="loadingType === 'nomore' && footprintList.length === 0">
      <missing v-if="loadingType === 'nomore' && footprintList.length === 0"
               :imgUrl="'http://qiniuoss.nauzone.cn/%E7%BB%84%203%20%E6%8B%B7%E8%B4%9D@3x.png'" :desc="'暂无浏览足迹哦！'"></missing>
    </view>
    <view class="footprint-list">
      <block v-for="(item, index) in footprintList" :key="index">
        <view class="footprint-item" :class="{'b-b': index!==footprintList.length-1}">
          <view class="image-wrapper" style="width: 180rpx;height: 180rpx;">
            <image :src="JSON.parse(item.img)[0].url" :class="[item.loaded]" mode="aspectFill" lazy-load
                   @load="onImageLoad('footprintList', index)" @error="onImageError('footprintList', index)"
                   style="width: 180rpx;height: 180rpx;"></image>
          </view>
          <view class="item-right">
            <text class="clamp title">{{item.productName}}</text>
            <text class="attr text-cut">{{item.description}}</text>
            <text class="attr">累计销售{{item.sales}}件</text>
            <text class="attr">浏览时间：{{item.updateTime}}</text>

            <text class="price"><text v-if="item.otPrice > (isVip?item.vipPrice:item.price)"
                                      style="text-decoration:line-through;color: #909399;margin-right: 20rpx;">¥{{item.otPrice}}</text>
              ¥{{(isVip?(item.vipPrice + ' [VIP]'):item.price)}}</text>
          </view>
          <text class="del-btn yticon icon-fork" @click="deleteFootprint(item)"></text>
        </view>
      </block>
    </view>
  </view>
</template>

<script>
import missing from '@/components/missing.vue'
export default {
  components: {
    missing
  },
  data() {
    return {
      footprintList: [],
      pageNo: 1,
      loadingType: 'more',
      isVip: false
    };
  },
  onShow() {
    this.isVip = this.$api.isVip()
  },
  onLoad(options) {
    this.loadData()
  },
  //下拉刷新
  onPullDownRefresh() {
    this.loadData('refresh');
  },
  //加载更多
  onReachBottom() {
    this.loadData();
  },
  methods: {
    //获取足迹列表
    loadData(type) {
      const that = this
      if (type === 'refresh') {
        that.pageNo = 1
        that.footprintList = []
        that.loadingType = 'more'
      }
      this.pageSize = 10
      if (that.loadingType === 'more') {
        that.loadingType = 'loading'
        that.$api.request('get', 'user/userFootprint/getFootprintAll', {
          pageNum: that.pageNo,
          pageSize: this.pageSize
        }).then(res => {
          that.pageNo = that.pageNo + 1
          that.loadingType = that.pageNo < res.total / this.pageSize + (res.total % this.pageSize == 0 ? 0 : 1) ?
              'more' : 'nomore'
          res.rows.forEach(item => {
            that.footprintList.push(item);
          })
          if (type === 'refresh') {
            uni.stopPullDownRefresh();
          }
        })
      }
    },
    //删除足迹
    deleteFootprint(item) {
      const that = this
      uni.showModal({
        title: '提示',
        content: '确定要删除这条足迹记录吗？',
        success: function(res) {
          if (res.confirm) {
            that.$api.request('post', 'user/userFootprint/deleteFootprint', {
              id: item.id
            }).then(res => {
              uni.showToast({
                title: '删除成功',
                icon: 'success'
              });
              that.loadData('refresh');
            })
          }
        }
      });
    },
    //监听image加载完成
    onImageLoad(key, index) {
      this.$set(this[key][index], 'loaded', 'loaded');
    },
    //监听image加载失败
    onImageError(key, index) {
      this[key][index].image = '/static/errorImage.jpg';
    },
  },
}
</script>

<style lang="scss">
.container {
  padding-bottom: 134upx;
}

/* 足迹列表项 */
.footprint-item {
  display: flex;
  position: relative;
  padding: 30upx 40upx;

  .image-wrapper {
    width: 230upx;
    height: 230upx;
    flex-shrink: 0;
    position: relative;

    image {
      border-radius: 8upx;
    }
  }

  .item-right {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: hidden;
    position: relative;
    padding-left: 30upx;

    .title,
    .price {
      font-size: $font-base + 2upx;
      color: $font-color-dark;
      height: 40upx;
      line-height: 40upx;
    }

    .attr {
      font-size: $font-sm + 2upx;
      color: $font-color-light;
      height: 50upx;
      line-height: 50upx;
    }

    .price {
      height: 50upx;
      line-height: 50upx;
    }
  }

  .del-btn {
    position: absolute;
    right: 30upx;
    top: 30upx;
    color: $font-color-disabled;
    font-size: 40upx;
  }
}
</style>