<template>
  <div>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <!-- 没有更多了 -->
      <van-list
        v-model="loading"
        :finished="finished"
        :finished-text="list.length ? $t('common.l5') : ''"
        @load="onLoad"
      >
        <div class="item" v-for="item in list"  :key="item.id" @click="checkDetail(item)">
          <div class="left">
            <div class="title">{{ item.title }}</div>
            <div class="origin flex">
              <div>{{ item.origin }}</div>
              <div>{{ item.created_at }}</div>
            </div>
          </div>
          <van-image
            v-if="item.img"
            width="100px"
            height="71px"
            fit="cover"
            radius="6px"
            :src="item.img"
          />
        </div>
      </van-list>
      <NoData v-if="list.length == 0 && !loading"/>
    </van-pull-refresh>
  </div>
</template>
<script>
import { getNews } from "@/api/market";
export default {
  props: ['category'],
  data () {
    return {
      loading: false,
      finished: false,
      refreshing: false,
      pageNum: 0,
      pageSize: 15,
      list: [],
      languageSign: ''
    }
  },
  activated() {
    // if (this.languageSign != this.language) {
    //   this.languageSign = this.language
    // }
  },
  created () {
    this.languageSign = this.language
    if (!this.loading) {
      this.onRefresh()
    }
  },
  methods: {
    onLoad() {
      if (this.refreshing) {
        this.list = [];
        this.refreshing = false;
      }
      this.pageNum += 1;
      var data = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        language: (this.language == 'zh-CN' || this.language == 'zh-TW') ? 'zh' : this.language
      }
      if (['港股','美股','A股', '沪深'].indexOf(this.category) !== -1) {
        data.market = this.category
      } else if (this.category == '综合'){
        data.market = 'crypto'
      } else {
        data.category = this.category
      }
      getNews(data).then((res) => {
        if (Array.isArray(res)) {
          this.loading = false;
          this.finished = true;
          return
        }
        var list = res.list || [];
        if (this.pageNum == 1) {
          this.list = list;
        } else {
          this.list = this.list.concat(list);
        }
        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.refreshing = true;
      this.pageNum = 0;
      this.onLoad();
    },
    checkDetail(item) {
      if (item.category != '快讯') {
        this.$router.push('/news-detail/' + item.id)
      }
    },
  }
}
</script>
<style lang="less" scoped>
.van-pull-refresh {
  height: calc(100vh - 176px);
  overflow-y: auto;
}
.van-list {
  padding: 12px 14px 10px;
}
.item {
  column-gap: 19px;
  cursor: pointer;
  display: flex;
  align-items: flex-start;
  padding-bottom: 22px;
  .title {
    font-size: 15px;
    line-height: 24px;
    font-weight: 500;
    color: var(--color-1-);
    margin-bottom: 12px;
  }
  .left {
    flex: 1;
    min-width: 10%;
    max-width: 100%;
  }
  .origin {
    color: #A2A2A2;
    font-size: 11px;
    font-weight: 500;
    column-gap: 18px;
  }
}
</style>