<template>
  <component
    @click.prevent="openModal(feed)"
    @like-feed="likeFeed"
    :is="currentTypeNode"
    :feed_id="feed.id"
    :user="feed.user"
    :entity="feed.entity"
  ></component>

<p class="mt-2 text-gray-light text-sm" v-if="feed.entity.price">Цена: {{feed.entity.price}}</p>
<p class="mt-2 text-gray-light text-sm" v-if="feed.entity.purchase_date">Дата покупки: {{feed.entity.purchase_date}}</p>
</template>

<script>
import { Inertia } from "@inertiajs/inertia";
import FeedImages from "@/Shared/Feed/Images";
import FeedVideos from "@/Shared/Feed/Videos";
import FeedMusics from "@/Shared/Feed/Musics";
import { usePage } from "@inertiajs/inertia-vue3";

export default {
  components: {
    FeedImages,
    FeedVideos,
    FeedMusics,
  },
  props: {
    feed: Object,
  },
  emits: ["openModal"],
  computed: {
    authUser() {
      return usePage().props.value.auth.user;
    },
    currentTypeNode() {
    //   if (this.feed.entity.is_paid && this.authUser.id !== this.feed.user.id) {
    //     return "feed-paids";
    //   }
      return "feed-" + this.feed.type.toLowerCase();
    },
  },
  methods: {
    openModal(feed) {
        this.$emit("openModal", feed);
    },

    likeFeed() {
      Inertia.post(
        route("feed.like", this.feed.id),
        {},
        {
          preserveScroll: true,
          preserveState: true,
        }
      );
      if (this.feed.entity.liked) {
        this.feed.entity.liked = false;
        this.feed.entity.likes--;
      } else {
        this.feed.entity.liked = true;
        this.feed.entity.likes++;
      }
    },
  },
};
</script>
