<template>
  <div class="main-screen">
    <div class="content">
      <swiper
        :slides-per-view="1"
        :pagination="{ clickable: true }"
        :modules="modules"
        class="movie-swiper"
      >
        <swiper-slide v-for="movie in movies" :key="movie.id" class="swiper-slide">
          <h1 class="movie-title">{{ movie.name }}</h1>
          <div class="poster-container">
            <img :src="getMovieImage(movie.poster)" :alt="movie.name" class="poster-image">
          </div>
        </swiper-slide>
      </swiper>
    </div>

    <BottomNav />
  </div>
</template>

<script>
import { Swiper, SwiperSlide } from 'swiper/vue'
import { Pagination } from 'swiper/modules'
import 'swiper/css'
import 'swiper/css/pagination'
import api from '@/services/api'
import BottomNav from '@/components/BottomNav.vue'

export default {
  name: 'MainScreen',
  components: {
    BottomNav,
    Swiper,
    SwiperSlide
  },
  data() {
    return {
      movies: [],
      modules: [Pagination]
    }
  },
  methods: {
    getMovieImage(posterPath) {
      if (!posterPath) return ''
      return `https://cinema.kolei.ru/up/images/${posterPath}`
    },
    async fetchMovies() {
      try {
        const token = localStorage.getItem('token')
        if (!token) {
          this.$router.push('/signin')
          return
        }

        const response = await api.get('/movies', {
          params: {
            filter: 'new'
          }
        })
        
        if (response && response.data.length > 0) {
          this.movies = response.data
        }
      } catch (error) {
        console.error('Error fetching movies:', error)
      }
    }
  },
  created() {
    this.fetchMovies()
  }
}
</script>

<style>
.main-screen {
  min-height: 100vh;
  background-color: #000000;
  display: flex;
  flex-direction: column;
}

.content {
  flex: 1;
  padding-top: 20px;
}

.movie-swiper {
  height: calc(100vh - 80px);
}

.swiper-slide {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0 20px;
}

.movie-title {
  color: white;
  font-size: 34px;
  font-weight: bold;
  margin: 20px 0;
  text-align: center;
}

.poster-container {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.poster-image {
  width: 100%;
  max-width: 340px;
  height: auto;
  border-radius: 16px;
}

.bottom-nav {
  height: 80px;
  background: #000000;
  display: flex;
  justify-content: space-around;
  align-items: center;
  padding-bottom: 20px;
}

.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-decoration: none;
  color: #686868;
}

.nav-item.active {
  color: #2196F3;
}

.material-icons {
  font-size: 24px;
  margin-bottom: 4px;
}

.nav-text {
  font-size: 10px;
}

.swiper-pagination-bullet {
  background: #686868 !important;
  opacity: 1 !important;
}

.swiper-pagination-bullet-active {
  background: #2196F3 !important;
}
</style>
  