<template>
  <div class="snt-product">
    <div class="snt-product__container">
      <slot v-bind="slotProps">
        <snt-link
          :to="routerLink"
          :label="name"
          class="snt-product__link">
          <template #activator="{ label }">
            <div class="snt-product__image">
              <v-img
                :src="pictureUrl"
                class="snt-product-image__img"
                height="100%"
                contain
              />
            </div>
            <div class="snt-product__title">
              {{ label }}
            </div>
          </template>
        </snt-link>
        <div
          v-if="showSku"
          class="snt-product__sku mb-2">
          <template v-for="(sub, i) in subProducts">
            <span
              v-if="!isMain(sub.name)"
              :key="`product-sku-${i}`">
              {{ sub.name }}
            </span>
          </template>
        </div>
        <v-spacer />
        <div
          v-if="showDescription"
          class="snt-product__description">
          {{ description }}
        </div>
        <div
          v-if="showPrice"
          class="snt-product__price mb-1">
          {{ price | toLocaleString }} บาท
        </div>
        <div
          v-if="showStock"
          class="snt-product__stock mb-2">
          <div v-if="stock > 0">
            เหลือ {{ stock | toLocaleString }} ชิ้น
          </div>
          <div v-else>
            สินค้าหมด
          </div>
        </div>
        <div
          v-if="showCart"
          class="snt-product-cart">
          <v-btn
            :disabled="stock < 1"
            class="snt-product-cart__btn"
            outlined
            depressed
            @click="addCart()">
            <v-icon left>
              mdi-cart-outline
            </v-icon>
            ใส่ตะกร้า
          </v-btn>
        </div>
      </slot>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import SntLink from './Link.vue'

export default {
  name: 'Product',
  components: { SntLink },
  props: {
    id: {
      type: [Number, String],
      required: true
    },
    isCollection: {
      type: Boolean,
      required: true
    },
    pictureUrl: {
      type: String,
      default: ''
    },
    name: {
      type: String,
      default: ''
    },
    subProducts: {
      type: Array,
      default: () => ([])
    },
    description: {
      type: String,
      default: ''
    },
    price: {
      type: [String, Number],
      default: 0
    },
    stock: {
      type: [String, Number],
      default: 0
    },
    showSku: {
      type: Boolean,
      default: true
    },
    showDescription: {
      type: Boolean,
      default: false
    },
    showPrice: {
      type: Boolean,
      default: true
    },
    showStock: {
      type: Boolean,
      default: true
    },
    showCart: {
      type: Boolean,
      default: true
    }
  },
  computed: {
    slotProps () {
      return {
        item: {
          id: this.id,
          isCollection: this.isCollection,
          pictureUrl: this.pictureUrl,
          name: this.name,
          subProducts: this.subProducts,
          description: this.description,
          price: this.price,
          stock: this.stock
        },
        showSku: this.showSku,
        showDescription: this.showDescription,
        showPrice: this.showPrice,
        showStock: this.showStock,
        showCart: this.showCart,
        isMain: this.isMain,
        routerLink: this.routerLink,
        addToCart: this.addToCart
      }
    },
    routerLink () {
      return {
        name: this.isCollection ? 'SalePageCollectionDetail' : 'SalePageProductDetail',
        params: {
          productId: this.id
        }
      }
    }
  },
  methods: {
    ...mapActions({
      addToCart: 'cart/addToCart',
      setSnackbar: 'notify/setSnackbar'
    }),
    isMain (subProductName) {
      return !subProductName || subProductName === 'main'
    },
    addCart () {
      let name = this.name
      const selectedPurchase = this.$_.head(this.subProducts)
      if (!this.isCollection) {
        if (selectedPurchase.name !== 'main') {
          name += ` (${selectedPurchase.name})`
        }
      }
      this.addToCart({
        id: selectedPurchase.id,
        price: selectedPurchase.price,
        available: selectedPurchase.available,
        name,
        pictureUrl: this.pictureUrl,
        isCollection: this.isCollection,
        quantity: 1
      })
      this.setSnackbar({
        value: true,
        message: 'เพิ่มสินค้าลงตะกร้า',
        type: 'success'
      })
    }
  }
}
</script>

<style scoped>
  .snt-product {
    height: 100%;
  }
  .snt-product__container {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    height: 100%;
  }
  .snt-product__link {
    text-decoration: none;
    padding: 0;
  }
  .snt-product__image {
    height: 255px;
    border: 1px solid #E4E4E4;
    border-radius: 4px;
    margin-bottom: 12px;
    cursor: pointer;
  }
  .snt-product__title {
    display: -webkit-box;
    height: 52px;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    font-weight: 400;
    font-size: 16px;
    line-height: 24px;
    color: #6B6B6B;
    cursor: pointer;
  }
  .snt-product__sku {
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    font-size: 16px;
    font-weight: 400;
    color: #8D8D8D;
  }
  .snt-product__price {
    font-weight: 500;
    font-size: 18px;
    color: #6B6B6B;
  }
  .snt-product-cart {
    margin-top: 12px;
  }
  .snt-product-cart__btn {
    font-weight: 400;
    font-size: 16px;
    color: var(--v-primary-base);
  }
</style>
