<script setup lang="ts">
import dayjs from 'dayjs'
import { ReadonlyInj } from '~/context'

interface Props {
  modelValue: string | null
}

const { modelValue } = defineProps<Props>()

const emit = defineEmits(['update:modelValue'])

const { isMysql } = useProject()

const readOnlyMode = inject(ReadonlyInj, false)

let isDateInvalid = $ref(false)
const dateFormat = isMysql ? 'YYYY-MM-DD HH:mm:ss' : 'YYYY-MM-DD HH:mm:ssZ'

const localState = $computed({
  get() {
    if (!modelValue) {
      return undefined
    }

    if (!dayjs(modelValue).isValid()) {
      isDateInvalid = true
      return undefined
    }

    return /^\d+$/.test(modelValue) ? dayjs(+modelValue) : dayjs(modelValue)
  },
  set(val?: dayjs.Dayjs) {
    if (!val) {
      emit('update:modelValue', null)
      return
    }

    if (val.isValid()) {
      emit('update:modelValue', val?.format(dateFormat))
    }
  },
})

const open = ref(false)

const randonClass = `picker_${Math.floor(Math.random() * 99999)}`
watch(
  open,
  (next) => {
    if (next) {
      onClickOutside(document.querySelector(`.${randonClass}`)! as HTMLDivElement, () => (open.value = false))
    }
  },
  { flush: 'post' },
)
</script>

<template>
  <a-date-picker
    @click="open = !open"
    v-model:value="localState"
    :show-time="true"
    :bordered="false"
    class="!w-full px-1"
    format="YYYY-MM-DD HH:mm"
    :placeholder="isDateInvalid ? 'Invalid date' : !readOnlyMode ? 'Select date and time' : ''"
    :allow-clear="!readOnlyMode"
    :input-read-only="true"
    :dropdown-class-name="randonClass"
    :open="readOnlyMode ? false : open"
  >
    <template #suffixIcon></template>
  </a-date-picker>
</template>

<style scoped></style>
