<template>
  <div class="RDateInput">
    <span class="RDateInput-Label">{{ label }}</span>
    <div class="RDateInput-Content">
      <x-date-input
        :options="options"
        :value="date"
        hide-bottom-space
        class="RDateInput-Input"
        @input="inputHandler($event)"
      />
    </div>
  </div>
</template>

<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
import XDateInput from './x-date-input.vue';
import { formatDate } from '@/recruits/utils';
import { QDate } from 'quasar';

@Component({
  components: { XDateInput },
})
export default class RDateInput extends Vue {
  @Prop() private date!: Date;
  @Prop() options: QDate['options'];
  @Prop() label: string;
  @Prop({ default: 'date' }) emitFormat!: 'date' | 'timestamp' | string;

  get innerMask(): string {
    return 'YYYY-MM-DD';
  }

  getEmitValue(val: string): Date | number | string {
    switch (this.emitFormat) {
      case 'date':
        return formatDate(val, this.innerMask);
      case 'timestamp':
        return +val;
      default:
        return formatDate(val, this.emitFormat);
    }
  }

  private inputHandler(val: string): void {
    this.$emit(`update:date`, this.getEmitValue(val));
  }
}
</script>

<style lang="scss" scoped>
.RDateInput {
  display: flex;
  flex-direction: column;
  &-Label {
    font-family: 'Golos Text', sans-serif;
    font-style: normal;
    font-weight: 600;
    font-size: 12px;
    line-height: 16px;
    color: rgba(2, 12, 18, 0.58);
    margin-bottom: 4px;
  }
  &-Content {
    display: flex;
    flex-wrap: nowrap;
    background: #e5edf5;
    border-radius: 4px 4px 0 0;
    background-blend-mode: multiply;
    border-bottom: 1px solid rgba(1, 72, 116, 0.18);
    box-sizing: border-box;
    transition: 0.1s;
    height: 40px;
  }
  &-Input {
    flex-basis: 100%;
    &:not(:last-child) {
      margin-right: 8px;
    }
  }
}
</style>
