<style scoped>
.area1 {
  background-image: linear-gradient(rgba(0, 0, 0, 0.2) 0%, rgba(0, 0, 0, 1) 90%), url("@images/news/news_bg.png");
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  min-height: 120vh;
}
.mt {
  margin-top: 100px;
}
.area1 h1 {
  color: #fff;
  font-family: Poppins;
  font-size: 2rem;
  font-weight: bold;
  text-align: center;
  padding-top: 5rem;
}
.area1 .news-main {
  height: 25rem;
  background-color: #fff;
}
.area1 .news-main img {
  width: 100%;
  height: 16rem;
  object-fit: cover;
}
.area1 .news-main .post-date {
  color: #858585;
  font-size: 1rem;
  text-align: end;
  padding: 0.5rem 1rem;
}
.area1 .news-main .post-text {
  color: #000;
  font-family: Poppins;
  padding: 0.5rem 2rem;
}
.area1 .news-main .post-text .title {
  font-size: 1.5rem;
  font-weight: bold;
}
.area1 .news-main .post-text .content {
  font-size: 0.9rem;
  white-space: nowrap;
  overflow: hidden; 
  text-overflow: ellipsis;
}
.area1 .news-list {
  height: 30rem;
  overflow: auto;
}
.area1 .news-list .news-item {
  width: 100%;
  height: 4rem;
  display: flex;
  color: #fff;
  font-family: Poppins;
  margin-bottom: 1rem;
}
.area1 .news-list .news-item img {
  width: 4rem;
  height: 4rem;
  object-fit: cover;
}
.area1 .news-list .news-item .item-main {
  width: 100%;
  overflow: hidden;
}
.area1 .news-list .news-item .head {
  display: flex;
  justify-content: space-between;
}
.area1 .news-list .news-item .title {
  font-size: 1.5rem;
  font-weight: bold;
}
.area1 .news-list .news-item .content {
  font-size: 0.9rem;
  white-space: nowrap;
  overflow: hidden; 
  text-overflow: ellipsis;
  padding-right: 1rem;
}
.area1 .news-list .news-item .date {
  color: rgba(255, 255, 255, 0.72);
  font-size: 1rem;
  text-align: end;
  padding: 0.5rem 1rem;
}
.area1 .news-area > .row > div+div {
  border-left: 1px solid rgba(255, 255, 255, 0.5);
}
.styled-hr {
  border: none;
  width: 10rem;
  height: 1px;
  background-color: #fff;
  position: relative;
  left: 50%;
  transform: translateX(-50%);
  margin: 0.5rem 0;
  opacity: 0.5;
}
.styled-hr::before,
.styled-hr::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 1px;
  height: 0.5rem;
  background-color: #fff;
  transform: translateY(-50%);
}
.styled-hr::before {
  left: 5px;
}
.styled-hr::after {
  right: 5px;
}
.area2 {
  background-color: #000;
  min-height: 10rem;
}
@media (max-width: 768px) {
  .area1 .news-area > .row > div+div {
    border: 0;
  }
}
</style>
<template>
  <div class="area1">
    <div class="container" :class="{'mt': !isAuthenticated}">
      <h1 class="title">{{ $t('backend.news.latest_news') }}</h1>
      <hr class="styled-hr">
      <div class="news-area my-5" v-if="newsData.length > 0">
        <div class="row">
          <div class="col-12 col-md-6 px-5 py-3">
            <div class="news-main">
              <img :src="hasPhoto(newsData[0].photo)" title="附圖" alt="附圖" />
              <p class="post-date">{{ formatDate(newsData[0].post_date) }}</p>
              <div class="post-text">
                <router-link :to="`/news/show/${newsData[0].id}`">
                  <p class="title">{{ newsData[0].title }}</p>
                </router-link>
                <p class="content">{{ newsData[0].content }}</p>
              </div>
            </div>
          </div>
          <div class="col-12 col-md-6 px-5 py-3">
            <div class="news-list">
              <div class="news-item" v-for="news in newsData.slice(1)" :key="news.id">
                <img :src="hasPhoto(news.photo)" title="附圖" alt="附圖" />
                <div class="item-main ps-3">
                  <div class="head">
                    <router-link :to="`/news/show/${news.id}`">
                      <p class="title">{{ news.title }}</p>
                    </router-link>
                    <p class="date">{{ formatDate(news.post_date) }}</p>
                  </div>
                  <p class="content">{{ news.content }}</p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <div class="area2"></div>
</template>
<script>
import { api } from "@/global/axios";
import { isAuthenticated } from "@/global/request";
export default {
  data() {
    return {
      isAuthenticated: isAuthenticated(),
      newsType: [this.$t('global.system'), this.$t('global.notification')],
      newsData: [],
    };
  },
  computed: {
    hasPhoto () {
      return (photo) => {
        if (photo != '' && photo != null) {
          return `/storage/${photo}`;
        }

        return "/images/loginbg2.png";
      }
    }
  },
  mounted() {
    const vm = this;
    vm.fetchData();
  },
  methods: {
    fetchData: async function () {
      const vm = this;
      await api
        .get(`/api/getNewsList`)
        .then((res) => {
          vm.newsData = res.data.data;
        });
    },
    formatDate(dateString) {
      const date = new Date(dateString);
      const day = date.getDate();
      const month = date.toLocaleString('en-GB', { month: 'short' });
      const year = date.getFullYear();
  
      return `${month}.${day}, ${year}`;
    },
  },
};
</script>