<template>
  <view>
    <mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption"
      :up="upOption">
      <block v-for="(ite,ind) in dataList" :key="ind">
        <view class="p-base text-hex-A6A6A6 text-md">{{ite.view_date}}</view>
        <view class="px-10rpx">
          <u-waterfall v-model="ite.list" ref="uWaterfall">
            <template v-slot:left="{ leftList }">
              <view v-for="(item, index) in leftList" :key="index" class="px-rowSm mb-base">
                <goods-item :goods="item.sku"></goods-item>
              </view>
            </template>
            <template v-slot:right="{ rightList }">
              <view v-for="(item, index) in rightList" :key="index" class="px-rowSm mb-base">
                <goods-item :goods="item.sku"></goods-item>
              </view>
            </template>
          </u-waterfall>
        </view>
      </block>
    </mescroll-body>
  </view>
</template>
<script>
  import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
  export default {
    mixins: [MescrollMixin], // 使用mixin
    data() {
      return {
        downOption: {
          auto: false,
        },
        upOption: {
          page: {
            size: 20
          },
          noMoreSize: 1
        },
        dataList: [], //获取到的数据
      };
    },
    methods: {
      downCallback() {
        this.mescroll.resetUpScroll();
        this.dataList = []
        this.$refs.uWaterfall[0].clear();
      },
      async upCallback(page) {
        this.loadData(page);
      },
      loadData(page) {
        this.$api.get(`/v1/product/view-logs`, {
          params: {
            page: page.num,
            per_page: page.size
          }
        }).then(res => {
          this.mescroll.endSuccess(res.data.length)
          if (page.num == 1) this.dataList = [];
          this.gethandle(res.data)
          // this.dataList = this.dataList.concat(res.data);
        }).catch(err => {
          this.mescroll.endErr()
        })
      },
      //处理数据
      gethandle(arr) {
        let tempArr = [],
          newArr = []
        for (let i = 0; i < arr.length; i++) {
          if (tempArr.indexOf(arr[i].view_date) === -1) {
            newArr.push({
              view_date: arr[i].view_date,
              list: [arr[i]]
            })
            tempArr.push(arr[i].view_date);
          } else {
            for (let j = 0; j < newArr.length; j++) {
              if (newArr[j].view_date == arr[i].view_date) {
                newArr[j].list.push(arr[i])
              }
            }
          }
        }
        return this.dataList = this.dataList.concat(newArr);
      },
    }
  };
</script>
