<template>
  <div class="list">
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <!-- 没有更多了 -->
      <van-list
        v-model="loading"
        :finished="finished"
        :finished-text="list.length ? $t('common.l46') : ''"
        @load="getNews"
      >
        <div class="item" v-for="item in list" @click="$router.push('/news-detail/' + item.id)" :key="item.id">
          <div class="title">{{ item.title }}</div>
          <div class="time">{{ item.time || item.created_at }}</div>
        </div>
      </van-list>
      <NoData v-if="!loading && list.length == 0"/>
    </van-pull-refresh>
  </div>
</template>
<script>
import { getNews } from "@/api/market";
export default {
  props: ['symbolInfo'],
  data () {
    return {
      loading: false,
      finished: false,
      refreshing: false,
      list: [],
      total: 0,
      pageNum: 0,
      pageSize: 10,
    }
  },
  computed: {
    marketName() {
      return {
        'US': '美股',
        'CN': 'A股',
        'HK': '港股',
      }
    }
  },
  methods: {
    operationShow(item) {
      this.targetInfo = item;
      this.operationPopupShow = true;
    },
    getNews() {
      if (this.refreshing) {
        this.list = [];
        this.refreshing = false;
      }
      this.pageNum += 1;
      getNews({
        title: this.symbolInfo.title,
        market: this.marketName[this.symbolInfo.market] || this.symbolInfo.market,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        language: (this.language == 'zh-CN' || this.language == 'zh-TW') ? 'zh' : this.language
      }).then(res => {
        if (Array.isArray(res)) {
          this.loading = false;
          this.finished = true;
          return
        }
        if (this.pageNum == 1) {
          this.list = res.list || []
        } else {
          this.list = this.list.concat(res.list || [])
        }
        this.total = res.total
        this.loading = false;
        if (this.list.length >= res.total) {
          this.finished = true;
        }
      }).catch(() => {
        this.loading = false;
        this.finished = true;
      })
    },
    onRefresh() {
      this.finished = false;
      this.loading = true;
      this.pageNum = 0;
      this.getNews();
    }
  },
}
</script>
<style lang="less" scoped>
.van-pull-refresh {
  height: calc(100vh - 130px);
  overflow-y: auto;
  padding: 2px 18px 0;
}
.item {
  cursor: pointer;
  padding: 17px 0;
  border-bottom: 1px solid rgba(210,217,227,0.26);
  .title {
    color: var(--color-1-);
    line-height: 20px;
    margin-bottom: 8px;
  }
  .time {
    height: 1;
    font-size: 12px;
    color: #A6A6A6;
  }
}
</style>