<template>
  <div class="news-com" @click="toDetail">
    <div class="news-img">
      <div v-if="flag === 'Hackathon'" :class="data.status === 'Past' ? 'mask past' : data.status === 'Upcoming' ? 'mask upcoming' : ''">
        <div class="status">
          {{ data.status !== 'Ongoing' ? data.status : '' }}
        </div>
      </div>
      <img :src="data.cover_image" alt="" style="object-fit: cover">
      <div class="featured" v-if="data.tag">
        {{ data.tag }}
      </div>
    </div>
    <div class="news-title">
      {{ data.title }}
    </div>
    <div class="news-content">
      {{ data.description }}
    </div>
    <div class="news-date" v-if="flag === 'Hackathon'">
      {{ transformDate(data.starttime) + '-' + transformDate(data.endtime) }}
    </div>
    <div class="news-date" v-else>
      {{ transformDate(data.publishedtime) }}
    </div>
  </div>
</template>

<script setup>
import { useRouter } from "vue-router";
import {transformDate} from '../utils/tools.js'
import {clickHackathon, clickNewsApi} from "../api/manage.js";
// 路由对象
const router = useRouter()

const props = defineProps({
  data: Object,
  flag: String
})

// 重定向到详情页面
const toDetail = () => {

  if (props.flag === 'news') {
    clickNewsApi({news_id: props.data.id}).then(res=>{
      if (res.data) {
        if (res.data.type === 1) {
          router.push({
            path: '/ContentPages',
            query: {
              contentId: res.data.id,
              type: 'news'
            }
          })
        } else {
          if (res.data.link) {
            window.open(res.data.link)
          }
        }
      }
    })
  }
  if (props.flag === 'Hackathon') {
    clickHackathon({hackathon_id: props.data.id})
  }
  if (props.flag === 'Hackathon' && props.data.link) {
    window.open(props.data.link)
  }


}

</script>

<style lang="scss" scoped>
.news-com {
  width: calc(100% / 2) !important;
  padding: 33px 10px 12px;
  cursor: pointer;
  .news-img {
    position: relative;
    width: 100%;
    height: 4rem;
    img {
      height: 100%;
      width: 100%;
      display: block;
    }
    .featured {
      width: 60px;
      background: rgba(255, 255, 255, 0.75);
      position: absolute;
      top: 13px;
      right: 0;
      text-align: center;
      line-height: 17px;
      font-family: 'Libre Bodoni';
      font-style: normal;
      font-weight: 400;
      font-size: 14Px;
      color: #FF6D00;
    }

    .mask {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;

      .status {
        text-align: center;
        font-family: 'Raleway';
        font-weight: 900;
        font-size: 20px;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
      }
    }

    .upcoming {
      background-color: rgba(255, 255, 255, 0.75);
      color: #FF6D00;
    }

    .past {
      background-color: rgba(0, 0, 0, 0.5);
      color: #fff;
    }

    img {
      width: 100%;
    }
  }

  .news-title {
    font-family: 'Libre Bodoni';
    font-style: normal;
    font-weight: 400;
    font-size: 12px;
    line-height: 1.3;
    margin: 16px 0 8px;
    color: #000000;
  }

  .news-content {
    font-family: 'Raleway';
    font-style: normal;
    font-weight: 400;
    font-size: 14Px;
    line-height: 14px;
    text-align: left;
    color: rgba(0, 0, 0, 0.5);
    display: -webkit-box;
    overflow: hidden;
    white-space: normal !important;
    text-overflow: ellipsis;
    word-wrap: break-word;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
  }

  .news-date {
    font-family: 'Raleway';
    font-style: normal;
    font-weight: 700;
    font-size: 14Px;
    line-height: 14px;
    color: rgba(0, 0, 0, 0.5);
    margin: 10px 0;
  }

  @media screen and (min-width:1440Px) and (max-width: 100vw) {
    width: calc(100% / 3) !important;
    padding: 16px 24px 40px;

    .news-title {
      font-size: 16px;
      line-height: 1.3;
    }

    .news-content {
      margin-top: 20px;
      font-size: 16Px;
      line-height: 16px;
    }

    .featured {
      width: 60px;
      top: 13px;
      font-size: 14Px;
      line-height: 17px;
    }
  }

  //@media screen and (min-width: 768Px) and (max-width: 1440Px) {
  //  .news-img {
  //    height: 4.2rem;
  //  }
  //}

  @media screen and (min-width:0Px) and (max-width: 415Px) {
    width: 100% !important;
    padding: 0px 0px 44px !important;

    .news-title {
      font-size: 18px;
      line-height: 1.3;
    }

    .news-content {
      margin-top: 18px;
      font-size: 14Px;
      line-height: 18px;
    }

    .news-date {
      font-size: 12px;
    }

    .featured {
      width: 80px !important;
      top: 13px;
      right: 0;
      font-size: 12Px;
      line-height: 22px !important;
    }
    .news-img {
      //width: 10.24rem;
      height: 5rem;
    }
  }

}
</style>
