<template>
    <v-menu
          v-model="menu2"
          :close-on-content-click="false"
          :nudge-right="40"
          lazy
          transition="scale-transition"
          offset-y
          full-width
          max-width="290px"
          min-width="290px"
        >
          <v-text-field
            slot="activator"
            v-model="computedDateFormatted"
            :label=init_label
            hint="MM/DD/YYYY format"
            persistent-hint
            readonly
            browser-autocomplete="off"
          ></v-text-field>
          <v-date-picker v-model="init_date" no-title @input="menu2 = false" :max="init_max_date"></v-date-picker>
    </v-menu>
 </template>

 <script>
 module.exports = {
     props : ['label', 'date', 'data', 'max_date'],

     data () {
        return {
            init_date: this.date && this.date != "0000-00-00" ? this.date : null, //new Date().toISOString().substr(0, 10),
            init_max_date: this.max_date ? this.max_date : '2999-09-09',
            dateFormatted: this.formatDate(new Date().toISOString().substr(0, 10)),
            menu1: false,
            menu2: false,

            init_label: this.label ? this.label : 'Date',
            init_data: this.data ? this.data : ''
        }
     },

    computed: {
      computedDateFormatted () {
        return this.formatDate(this.init_date)
      }
    },

    watch: {
      init_date (n, o) {
        this.dateFormatted = this.formatDate(this.init_date)
        
        this.$emit('change', {"old_date":o, "new_date":n, "data":this.init_data});
      }
    },

    methods: {
      formatDate (date) {
        if (!date) { return null }

        const [year, month, day] = date.split('-')
        return `${day}-${month}-${year}`
      },
      parseDate (date) {
        if (!date) return null

        const [month, day, year] = date.split('/')
        return `${year}-${month.padStart(2, '0')}-${day.padStart(2, '0')}`
      },

      emitChange (n, o) {
          console.log("old:"+o)
          console.log("new:"+n)
      }
    }
 }
</script>
