<template>
  <div class="container">
    <div v-if="pending" class="loader"></div>
    <div v-else-if="error" class="error" role="alert">
      Erro ao tentar carregar a lista de animes
    </div>
    <div class="card grow" v-else v-for="anime in animes" :key="anime.name">
      <div class="card-header">
        <img :src="anime.image" :alt="anime.name" />
      </div>
      <div class="card-body">
        <span class="tag tag-teal">Tag</span>
        <h4 class="card-title">{{ anime.name }}</h4>
        <p>
          {{ anime.description }}
        </p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "AnimeList",
  data() {
    return {
      animes: null,
      pending: true,
      error: false,
      data: null,
    };
  },
  mounted() {
    this.getAnimesList();
  },
  methods: {
    async getAnimesList() {
      this.pending = true;
      await fetch("https://www.mockachino.com/a5c0d330-8fb8-4a/animes")
        .then((res) => res.json())
        .then((data) => (this.animes = data.animes))
        .catch(() => (this.error = true));

      this.pending = false;
    },
  },
};
</script>

<style>
@import url("https://fonts.googleapis.com/css2?family=Roboto&display=swap");
.grow {
  transition: all 0.2s ease-in-out;
}

.grow:hover {
  transform: scale(1.05);
}

.container {
  margin-top: 40px;
  margin-bottom: 40px;
  display: flex;
  max-width: 1040px;
  justify-content: space-evenly;
  flex-wrap: wrap;
}

.card {
  margin: 20px;
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0 2px 20px rgba(0, 0, 0, 0.2);
  overflow: hidden;
  width: 250px;
}

.card span {
  margin-left: 0px;
}

.card-header img {
  width: 100%;
  height: 200px;
  object-fit: cover;
}

.card-body {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: flex-start;
  padding: 20px;
  min-height: 250px;
}

.card-body p {
  font-size: 13px;
  margin: 0 0 40px;
}

.card-title {
  margin: 10px 0;
}

.tag {
  background: #cccccc;
  border-radius: 50px;
  font-size: 12px;
  color: #fff;
  padding: 2px 10px;
  text-transform: uppercase;
  cursor: pointer;
}

.tag-teal {
  background-color: #47bcd4;
}

.loader {
  border: 16px solid #f3f3f3; /* Light grey */
  border-top: 16px solid #3498db; /* Blue */
  border-radius: 50%;
  width: 120px;
  height: 120px;
  animation: spin 2s linear infinite;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.error {
  border: 1px solid;
  margin: 10px 0px;
  padding: 15px 10px 15px 50px;
  background-repeat: no-repeat;
  background-position: 10px center;
  color: #d8000c;
  background-color: #ffbaba;
  background-image: url("https://i.imgur.com/GnyDvKN.png");
}
</style>
