<template>
<view>
<!--mine/collect/collect.wxml-->
<view class="list" v-if="pageList.length > 0">
	<view v-for="(item, index) in pageList" :key="index" class="li">
		<image :src="item.pic" @tap="redirectDetail" :data-id="item.id" mode="aspectFit" class="cimg"></image>
		<view class="zhinfo">
			<view class="zhtit" @tap="redirectDetail" :data-id="item.id">{{item.title}}</view>
			<!-- <view class="guige">规格：15g</view> -->
			<view class="jiage">
				<view class="zhjia" @tap="redirectDetail" :data-id="item.id">￥<text>{{item.salePrice}}</text></view>
				<view class="btn" :data-index="index" @tap="clickCollect">取消收藏</view>
			</view>
		</view>
	</view>
</view>
<view class="nodata" v-else>
  <image :src="picUrlss+'empty_sc.png'"></image>
  <text>暂无收藏</text>
</view>
</view>
</template>

<script>
const req = require("../../utils/request.js");
const app = getApp();

export default {
  data() {
    return {
		picUrlss: req.public.picUrls,
      form: {
        page: 1,
        limit: 10
      },
      pageList: [],
      isLoad: true,
      ishow: false
    };
  },

  components: {},
  props: {},
  onLoad: function (options) {
    this.loadFootprint();
  },

  onReachBottom() {
    this.form.page++;
    this.loadCoupon();
  },

  methods: {
    loadFootprint() {
      let isShowLoading = false;

      if (this.form.page == 1 && !isShowLoading) {
        req.loadIng('加载中');
        isShowLoading = true;
      }

      if (!this.isLoad) return false;
      this.isLoad = false;
      let that = this;
      req.getRequest('/api/collect/product', this.form, data => {
        if (data && data.length == 10) this.isLoad = true;
        if (that.form.page > 1) data = that.pageList.concat(data);
        that.setData({
          pageList: data
        });

        if (!this.pageList||this.pageList.length<1) {
          that.setData({
            ishow: true
          });
        } else {
          that.setData({
            ishow: false
          });
        }

        if (isShowLoading) {
          uni.hideLoading();
          isShowLoading = false;
        }
      });
    },

    clickCollect(event) {
      let _ts = this;

      const index = event.currentTarget.dataset.index;
      const page = this.pageList[index];
      req.postRequest('/api/collect', {
        bindId: page.id,
        type: 1
      }, data => {
        _ts.pageList.splice(index, 1);

        _ts.setData({
          pageList: _ts.pageList
        });
      });
    },

    redirectDetail(event) {
      uni.navigateTo({
        url: '/product/detail/detail?id=' + event.currentTarget.dataset.id
      });
    }

  }
};
</script>
<style>
@import "./collect.css";
</style>