<template>
  <view class="file-info-container">
    <view class="search-content">
      <u-search placeholder="请输入关键字" v-model="keyword" @custom="onSearch" @search="onSearch"></u-search>
    </view>
    <view class="u-page">
      <u-list
          v-if="indexList.length>0"
          @scrolltolower="scrolltolower"
      >
        <u-list-item
            v-for="(item, index) in indexList"
            :key="index"
        >
          <view class="file-info-item" @click="onItemClick(item)">
            <view class="file-info-item-img">
              <image :src="item.imgUrl" mode="aspectFill" />
            </view>
            <view class="file-info-item-content">
              <view class="file-info-item-title">{{ item.title }}</view>
              <view class="file-info-item-date">{{ item.date }}</view>
            </view>
          </view>
        </u-list-item>
      </u-list>
      <u-loadmore :status="loadStatus" />
    </view>
  </view>
</template>

<script>

import { fetchContentInfo } from '@/common/api';
import { CONTENT_TYPE, ICON_CFG } from '@/common/EnumConst';
import { fileIcon, getImageUrl } from '@/util';

let page = 1;
export default {
  name: 'content',
  props: {},
  data() {
    return {
      ICON_CFG,
      keyword: '',
      indexList: [],
      loadStatus: 'loadmore', // loadmore,loading,nomore
    };
  },
  created() {
  },
  onLoad() {
    page = 1;
    this.loadmore();
  },

  methods: {
    fileIcon,
    getImageUrl,
    onItemClick(item) {
      // viewFileFn(getImageUrl(item.uri), item.title, `.${item.fileExt}`);
      uni.navigateTo({
        url: `/pasb/pages/detail/index?id=${item.id}`,
      });
    },
    scrolltolower() {
      if (page > 1 && this.loadStatus === 'nomore') {
        return;
      }
      this.loadmore();
    },
    loadmore() {
      this.loadStatus = 'loading';
      fetchContentInfo({
        pageNum: page,
        pageSize: 10,
        contentType: CONTENT_TYPE.conferenceMaterials,
        title: this.keyword,
      }).then(res => {
        page += 1;
        if (res.rows.length > 0) {
          this.indexList = this.indexList.concat(res.rows.map(item => {
            return {
              id: item.id,
              title: item.fileName,
              date: item.createTime,
              imgUrl: getImageUrl(item.thumbnail),
              ...item,
            };
          }));
        }
        if (res.rows.length < 10) {
          this.loadStatus = 'nomore';
        }
      });
    },
    onSearch() {
      page = 1;
      this.indexList = [];
      this.loadmore();
    },
  },
};
</script>
<style lang="scss" src="./index.scss" />;
