<template>
  <figure class="card" @click="chooseAsset" :class="{ selected: value == asset }">
    <img class="card-img-top" :src="previewUrl" />
    <figcaption>
      {{asset.title}}
    </figcaption>
  </figure>
</template>

<style lang="scss" scoped>
  .card {
    cursor: pointer;
    width: 100%;

    &.selected {
      background-color: #01335f;
      color: #fff;
      outline: 2px #01335f solid;

      .card-block {
        color: #fff;
      }
    }
  }

  figcaption {
    padding: 5px 10px;
  }

  img {
    object-fit: cover;
    width: 100%;
    height: 110px;


    &:hover {
      object-fit: contain;
    }
  }
</style>

<script>
  import { assetStore } from '../tools/asset-store.js';
  var pdfImage = require('../images/pdf.png'); // source & more icons: http://www.iconsfind.com/tag/document/

  export default {
    props: {
      asset: {
        type: Object,
        required: true
      },
      value: {
        required: true
      }
    },
    computed: {
      previewUrl() {
        var contentType = this.asset.contentType;

        if (contentType.indexOf('pdf') > -1) {
          return pdfImage;
        }

        return assetStore.previewUrl(this.asset);
      }
    },
    methods: {
      chooseAsset() {
        this.$emit('input', this.asset); // vue syntax: update prop 'value' in the parent
      }
    }
  };
</script>
