<template>
  <div>
    <div class="list" v-if="list.length">
      <img class="img" v-if="language == 'zh-CN' || language == 'zh-TW'" src="/img/home/new.png" alt="极度证券">
      <img class="img" v-else src="/img/home/new_en.png" alt="极度证券">
      <!-- <van-list
        v-model="loading"
        :finished="finished"
        :finished-text="list.length == 0 ? '' : '没有更多了'"
        @load="onLoad"
      > -->
        <div class="item" v-for="(item, index) in list" :key="index" @click="$router.push('/news-detail/' + item.id)">
          <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
            width="100px"
            height="71px"
            fit="cover"
            radius="6px"
            :src="item.img"
          />
        </div>
      <!-- </van-list> -->
    </div>
  </div>
</template>
<script>
import { getNews } from "@/api/market";
import bus from '@/utils/bus'
export default {
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      pageNum: 1,
      pageSize: 6,
    }
  },
  created () {
    this.getNews()
    bus.$on('selectLang', this.getNews)
  },
  methods: {
    onLoad() {
      this.pageNum += 1
      this.getNews()
    },
    getNews() {
      getNews({
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        img: 1,
        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
        }
        var list = res.list || []
        if (this.pageNum == 1) {
          this.list = list
        } else {
          this.list = this.list.concat(list)
        }
        // 加载状态结束
        this.loading = false;
        // 数据全部加载完成
        this.finished = true;
        // if (this.list.length >= res.total || this.pageNum == 4) {
        //   this.finished = true;
        // }
      }).catch(() => {
        this.loading = false;
        this.finished = true;
      }).finally(() => {
        this.$emit('success')
      })
    }
  }
}
</script>
<style lang="less" scoped>
.list {
  padding: 14px 8px 0 11px;
  border-radius: 4px;
  border: 1px solid #F1F1F1;
  background-color: var(--bg-);
}
.img {
  display: block;
  height: 20px;
  margin-bottom: 15px;
}
.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;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .left {
    flex: 1;
    min-width: 10%;
    max-width: 100%;
  }
  .origin {
    color: #A2A2A2;
    font-size: 11px;
    font-weight: 500;
    column-gap: 18px;
    white-space: nowrap;
  }
}
</style>