<!--
Sélecteur de dates

@see https://vuetifyjs.com/en/components/date-pickers/
-->

<template>
  <main>
    <v-menu
      v-model="dateOpen"
      :close-on-content-click="false"
      :nudge-right="40"
      transition="scale-transition"
      offset-y
      min-width="auto"
    >
      <template #activator="{ on, attrs }">
        <v-text-field
          v-model="datesFormatted"
          autocomplete="off"
          :label="$t(label_field)"
          prepend-icon="mdi-calendar"
          :disabled="readonly"
          v-bind="attrs"
          :dense="dense"
          :single-line="singleLine"
          v-on="on"
          :error="error"
        />
      </template>
      <v-date-picker
        v-model="datesParsed"
        :range="range"
        color="ot_green lighten-1"
        @input="dateOpen = range && datesParsed.length < 2"
      />
    </v-menu>
  </main>
</template>

<script lang="ts">
import { defineComponent, watch, ref, useContext, onUnmounted, computed, Ref, ComputedRef } from '@nuxtjs/composition-api'
import { WatchStopHandle } from '@vue/composition-api'
import DatesUtils from '~/services/utils/datesUtils'
import {useError} from "~/composables/form/useError";

export default defineComponent({
  props: {
    field: {
      type: String,
      required: false,
      default: null
    },
    label: {
      type: String,
      required: false,
      default: null
    },
    data: {
      type: [String, Array],
      required: false,
      default: null
    },
    readonly: {
      type: Boolean,
      required: false
    },
    range: {
      type: Boolean,
      required: false
    },
    dense: {
      type: Boolean,
      required: false
    },
    singleLine: {
      type: Boolean,
      required: false
    }
  },
  setup (props, { emit }) {
    const { data, range } = props
    const { $moment, store } = useContext()
    const {error, onChange} = useError(props.field, emit, store)
    const dateUtils = new DatesUtils($moment)

    const datesParsed: Ref<Array<string>|string|null> = range ? ref(Array<string>()) : ref(null)

    if (Array.isArray(datesParsed.value)) {
      for (const date of data as Array<string>) {
        if (date) { datesParsed.value.push($moment(date).format('YYYY-MM-DD')) }
      }
    } else {
      datesParsed.value = data ? $moment(data as string).format('YYYY-MM-DD') : null
    }

    const datesFormatted: ComputedRef<string|null> = computed(() => {
      if (props.range && datesParsed.value.length < 2) { return }
      return datesParsed.value ? dateUtils.formattedDate(datesParsed.value, 'DD/MM/YYYY') :  null
    })

    const unwatch: WatchStopHandle = watch(datesParsed, (newValue, oldValue) => {
      if (newValue === oldValue) { return }
      if (props.range && newValue.length < 2) { return }
      onChange(Array.isArray(newValue) ? dateUtils.sortDate(newValue) : newValue)
    })

    onUnmounted(() => {
      unwatch()
    })

    return {
      label_field: props.label ?? props.field,
      datesParsed,
      datesFormatted,
      dateOpen: ref(false),
      error
    }
  }
})
</script>

<style scoped>
</style>
