<template>
  <section class="home">
    <div class="page">
      <div class="tab-content">
        <span v-for="(item, index) in tabList" :key="index" class="tab-item" :class="tabIndex == index ? 'active' : ''"
              @click="timeSelect(index)">{{ item }}</span>
        <img src="https://cdn.cunwuyun.cn/img/party_detail.png" alt=""
             @click="handleGoto(`./select?index=${tabIndex}`)">
      </div>
      <div class="item" v-for="(item, index) in todayList" :key="index"
           @click="handleGoto(`./todayDetail?id=${item.id}`)">
        <div class="header">
          <img :src="item.thumbUrl[0].url" alt="" v-if="item.thumbUrl && item.thumbUrl.length"/>
          <p class="text">{{ item.title }}</p>
          <p class="time">{{ item.organizationName }} {{ item.publishDate || '' }}</p>
        </div>
        <div class="line-bg"></div>
      </div>
      <AiEmpty v-if="todayList.length == 0"/>
    </div>
  </section>
</template>
<script>
import {mapState} from "vuex";

export default {
  name: "home",
  computed: {
    ...mapState(["user", "token"]),
  },
  data() {
    return {
      tabList: ['全部', '一月', '二月', '三月', '四月', '五月'],
      tabIndex: 0,
      todayList: [],
      current: 1,
      pages: 2
    };
  },
  onLoad() {
    this.getTodayList();
  },
  onShow() {
    if (uni.getStorageSync('partyTabIndex')) {
      this.tabIndex = uni.getStorageSync('partyTabIndex')
      uni.setStorageSync('partyTabIndex', '')
      this.timeSelect(this.tabIndex);
    }
  },
  methods: {
    getTodayList() {
      if (this.current > this.pages) return;
      var publishDate = ''
      if (this.tabIndex > 0) {
        var myDate = new Date();
        var tYear = myDate.getFullYear();
        var month = this.tabIndex < 10 ? '0' + this.tabIndex : this.tabIndex
        publishDate = tYear + '-' + month + '-01'
      }
      this.$instance.post(`/app/apppartyeducation/list?style=2&current=${this.current}&size=10&publishDate=${publishDate}&status=1`).then((res) => {
        if (res.code == 0) {
          if (res.data.records && res.data.records.length) {
            res.data.records.map((item) => {
              console.log(item.thumbUrl)
              if (item.thumbUrl) {
                item.thumbUrl = JSON.parse(item.thumbUrl)
              }
              if (item.publishDate) {
                item.publishDate = item.publishDate.substring(0, 10)
              }
            })
            const todayList = this.current > 1 ? [...this.todayList, ...res.data.records] : res.data.records
            this.pages = Math.ceil(res.data.total / 10)
            this.todayList = todayList
          }
        }
      });
    },
    handleGoto(url) {
      uni.navigateTo({url})
    },
    timeSelect(index) {
      this.tabIndex = index
      this.current = 1
      this.pages = 2
      this.todayList = []
      this.getTodayList()
    }
  },
  onReachBottom() {
    this.current = this.current + 1
    this.getTodayList()
  },
};
</script>
<style scoped lang="scss">
@import "~dvcp-wui/common";

.home {
  width: 100%;
  height: 100%;
}

.page {
  width: 100%;
  overflow-x: hidden;
  background-color: #fff;

  .tab-content {
    width: 100%;
    height: 84px;
    padding: 0 30px;
    box-sizing: border-box;
    background: #FCF0ED;

    .tab-item {
      display: inline-block;
      width: 60px;
      line-height: 80px;
      height: 80px;
      margin-right: 48px;
      font-size: 30px;
      font-family: PingFangSC-Regular, PingFang SC;
      color: #70737B;
    }

    .active {
      font-family: PingFangSC-Semibold, PingFang SC;
      font-weight: 600;
      color: #DA2D1A;
      border-bottom: 4px solid #DE2C19;
    }

    img {
      width: 32px;
      vertical-align: middle;
    }
  }

  .header {
    padding: 0 34px;

    img {
      width: 684px;
      height: 252px;
      margin: 32px 0;
    }

    .text {
      font-size: 32px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #333;
      line-height: 48px;
      word-break: break-all;
      margin-bottom: 16px;
    }

    .time {
      font-size: 24px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #999;
      line-height: 30px;
      padding-bottom: 40px;
    }
  }

  .line-bg {
    width: 100%;
    height: 12px;
    background: #f2f2f2;
  }
}
</style>
