<template>
  <div>
    <div class="list">
      <van-list
        v-model="loading"
        :finished="finished"
        :finished-text="list.length ? $t('common.l5') : ''"
        @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
            v-if="item.img"
            width="107px"
            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 {
  props: ['category'],
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      pageNum: 0,
      pageSize: 6,
    }
  },
  beforeDestroy() {
    bus.$off('selectLang', this.init)
  },
  created () {
    bus.$on('selectLang', this.init)
  },
  methods: {
    init() {
      this.finished = false;
      this.loading = true;
      this.pageNum = 0;
      this.onLoad();
    },
    onLoad() {
      this.pageNum += 1
      this.getNews()
    },
    getNews() {
      getNews({
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        img: this.category == '要闻' ? 1 : '',
        category: this.category,
        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;
        // 数据全部加载完成
        if (this.list.length >= res.total) {
          this.finished = true;
        }
      }).catch(() => {
        this.loading = false;
        this.finished = true;
      })
    }
  }
}
</script>
<style lang="less" scoped>
.list {
  padding: 17px 18px 0;
  background-color: var(--bg-);
  height: 50vh;
  overflow-y: auto;
}
.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;
  }
  .left {
    flex: 1;
    min-width: 10%;
    max-width: 100%;
  }
  .origin {
    color: #A2A2A2;
    font-size: 11px;
    font-weight: 500;
    column-gap: 18px;
  }
}
</style>