<template>
  <div class="order-dish-container mb-m" :class="{ selected: quantity > 0 }">
    <div class="dish mb-s pr-l">
      <BaseText class="name mr-xs">{{ name }}</BaseText>

      <BaseText class="price">({{ price }}€)</BaseText>
    </div>

    <div class="inputs">
      <input-number
        :model-name="`dish_${uuid}_quantity`"
        :form-id="formId"
        :parent="parent"
        :label="$_tr('form.quantity')"
        :default-value="0"
        :rules="['required', 'min:0']"
        :min="0"
        :use-asterisk="false"
        :callback="value => +value"
        error-message=" "
        class="input-item quantity"
        @onDataChange="handleQuantityChange"
      />

      <div class="total-container">
        <input-number
          :model-name="`dish_${uuid}_price`"
          :form-id="formId"
          :parent="parent"
          :label="$_tr('form.price')"
          :default-value="0"
          :current-value="total.toFixed(2)"
          :rules="['required', 'min:0']"
          :min="0"
          :readonly="true"
          :use-asterisk="false"
          class="input-item"
        />

        <div
          v-if="quantity > 0"
          class="currency"
          :class="`length-${
            total.toFixed(2).toString().replace('.', '').length
          }`"
        >
          €
        </div>
      </div>

      <input-hidden
        :model-name="`dish_${uuid}_name`"
        :form-id="formId"
        :parent="parent"
        :default-value="name"
        :rules="['required']"
      />

      <input-hidden
        :model-name="`dish_${uuid}_category`"
        :form-id="formId"
        :parent="parent"
        :default-value="category"
        :rules="['required']"
      />
    </div>
  </div>
</template>

<script>
import { Title, BaseText } from 'inside-collection/atoms'

export default {
  name: 'OrderDish',
  $_i18nRootPath: 'customs.RESTAURANTS.ORDER',
  components: { Title, BaseText },
  props: {
    name: {
      type: String,
      required: true
    },
    category: {
      type: String,
      required: true
    },
    price: {
      type: Number,
      required: true
    },
    formId: {
      type: String,
      required: true
    },
    parent: {
      type: String,
      required: true
    },
    uuid: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      total: 0,
      quantity: 0
    }
  },
  watch: {
    total() {
      this.$emit('onTotalPriceChange', { name: this.name, total: this.total })
    }
  },
  methods: {
    handleQuantityChange({ value: quantity }) {
      this.quantity = quantity
      this.total = quantity * this.price
    }
  }
}
</script>

<style lang="scss">
@use '../../../styles/variables/mixins' as *;

.order-dish-container {
  display: inline-flex;
  width: 100%;
  justify-content: space-between;
  align-items: flex-end;

  .dish {
    position: relative;
    display: flex;
    align-items: center;
    width: 100%;

    &::before {
      content: '';
      height: 6px;
      min-width: 6px;
      background: var(--color-primary);
      margin-right: calc(var(--spacing-s) * (-1));
      border-radius: 50%;
      opacity: 0;
      transition: all 0.2s ease;
    }
  }

  &.selected {
    .dish {
      &::before {
        opacity: 1;
        margin-right: var(--spacing-xs);

        transition: all 0.2s ease;
      }
    }
  }

  .inputs {
    display: flex;

    @include restaurants-currency;

    .input-item {
      width: 56px;

      &.quantity {
        margin-right: calc(var(--spacing-s) + var(--spacing-xs));
        width: 64px;
      }
    }
  }
}
</style>
