<!--
 * @Description: 新闻通知部分
 * @Author: 空白格
 * @Date: 2022-08-30 14:15:32
 * @LastEditors: 空白格
 * @LastEditTime: 2023-04-24 17:18:58
 * @FilePath: \veterans_client_web\src\views\Home\components\NewsNotice.vue
 * @Copyright: Copyright (c) 2016~2022 by 空白格, All Rights Reserved.
-->
<template>
  <div class="home-main-news-box">
    <!-- 新闻部分 -->
    <el-row :gutter="0" class="home-main-news">
      <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
        <div class="home-main-news-left">
          <el-carousel :interval="5000" arrow="never" height="395px">
            <el-carousel-item v-for="(item, index) in newsList" :key="index">
              <el-image
                @click="newsItemClick(item)"
                class="image"
                :src="
                  item.artImage || require('@/assets/images/default-news.jpg')
                "
                fit="cover"
              >
                <div slot="placeholder" class="image-slot">
                  加载图片中<span class="dot">...</span>
                </div>
                <div slot="error" class="image-slot">
                  <el-image
                    class="image"
                    :src="require('@/assets/images/default-news.jpg')"
                    fit="fill"
                  ></el-image>
                </div>
              </el-image>
              <div class="title" @click="newsItemClick(item)">
                {{ item.artTitle || "" }}
              </div>
            </el-carousel-item>
          </el-carousel>
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
        <div class="home-main-news-right">
          <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane label="新闻动态" name="first">
              <ul class="news-infomation">
                <li
                  @click="newsItemClick(item)"
                  class="news-infomation-item"
                  v-for="(item, index) in newsList"
                  :key="index"
                >
                  <div class="title">{{ item.artTitle }}</div>
                  <div class="date">
                    {{ parseTime(item.createTime, "{y}-{m}-{d}") }}
                  </div>
                </li>
              </ul>
            </el-tab-pane>
            <el-tab-pane label="通知公告" name="second">
              <ul class="news-infomation">
                <li
                  @click="jumpPage('noticedetails', { id: item.id })"
                  class="news-infomation-item"
                  v-for="(item, index) in noticeList"
                  :key="index"
                >
                  <div class="title">{{ item.content }}</div>
                  <div class="date">
                    {{ parseTime(item.releasTime, "{y}-{m}-{d}") }}
                  </div>
                </li>
              </ul>
            </el-tab-pane>
          </el-tabs>
          <div class="more-btn" @click="jumpPage('/newscenter')">更多</div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { parseTime } from "@/utils/utils";
import { getNewsList, getNoticeList, getNewsData } from "@/api/Home";
export default {
  name: "",
  data() {
    return {
      newsList: [],
      noticeList: [],
      activeName: "first",
      parseTime: parseTime,
    };
  },
  created() {
    this.getNews();
  },
  methods: {
    /**
     * tab点击事件
     * @date 2022-08-10
     * @param {any} tab
     * @returns {any}
     */
    handleClick(tab) {
      switch (tab.name) {
        case "first":
          this.getNews();
          break;
        case "second":
          this.getNotice();
          break;
      }
    },
    /**
     * 获取新闻
     * @date 2022-08-10
     * @returns {any}
     */
    getNews() {
      getNewsList({
        artCategoryId: 2,
        pageNum: 1,
        pageSize: 10,
      }).then((res) => {
        this.newsList = res?.rows ?? [];
        if (this.newsList.length) {
          this.getNewsInfo(this.newsList[0].artId);
        }
      });
    },
    /**
     * 获取新闻详情
     * @date 2022-08-17
     * @param {any} id
     * @returns {any}
     */
    getNewsInfo(id) {
      getNewsData({ id }).then((res) => {
        this.$emit("getLastNews", res?.data ?? {});
      });
    },
    /**
     * 获取公告
     * @date 2022-08-10
     * @returns {any}
     */
    getNotice() {
      getNoticeList().then((res) => {
        this.noticeList = res?.data ?? [];
      });
    },
    /**
     * 跳转
     * @date 2022-08-30
     * @param {any} path
     * @param {any} query
     * @returns {any}
     */
    jumpPage(path, query) {
      this.$router.push({ path, query });
    },
    newsItemClick(item) {
      if (item.artLinkUrl) location.href = item.artLinkUrl;
      else
        this.jumpPage("newsdetails", {
          id: item.artId,
          type: "news",
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.home-main {
  &-news-box {
    background-color: #f4f4f4;
  }
  &-news {
    width: 70%;
    margin: 0 auto;
    min-width: 600px;
    padding: 30px 0;
    border-radius: 5px;
    margin-top: 14px;
    &-left {
      margin-right: 30px;
      .image {
        width: 100%;
        height: 100%;
        border-radius: 2px;
        cursor: pointer;
      }
      .title {
        width: calc(100% - 26px);
        height: 43px;
        line-height: 43px;
        padding: 0 13px;
        background-color: rgba($color: #000000, $alpha: 0.6);
        color: #fff;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        word-break: break-all;
        position: absolute;
        bottom: 0;
        font-size: 14px;
        font-weight: blod;
        cursor: pointer;
      }
      :deep(.el-carousel__indicators) {
        // 指示器
        left: auto;
        right: -20px;
      }
      :deep(.el-carousel__button) {
        // 指示器按钮
        width: 7px;
        height: 7px;
        border: none;
        border-radius: 50%;
        background-color: #fff;
      }
      :deep(.is-active .el-carousel__button) {
        // 指示器激活按钮
        width: 16px;
        border-radius: 10px;
        background-color: #f00;
      }
    }
    &-right {
      position: relative;
      :deep(.el-tabs__item) {
        font-size: 20px;
        font-weight: 500;
        color: #000000;
      }
      :deep(.el-tabs__item:hover) {
        color: #163da5;
      }
      :deep(.el-tabs__item.is-active) {
        color: #163da5;
      }
      :deep(.el-tabs__active-bar) {
        background-color: #163da5;
      }
      .news-infomation {
        &-item {
          display: flex;
          justify-content: space-between;
          margin-bottom: 10px;
          font-size: 14px;
          color: #373737;
          cursor: pointer;
          &:hover {
            text-decoration: underline;
          }
          .title {
            width: 80%;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            word-break: break-all;
          }
          .date {
            width: 20%;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            word-break: break-all;
            text-align: right;
            color: #797979;
          }
        }
      }
      .more-btn {
        position: absolute;
        right: 0;
        top: 12px;
        color: #727272;
        font-size: 14px;
        cursor: pointer;
      }
    }
  }
}
</style>
