<script lang="ts" setup>
import { onMounted, ref, computed } from 'vue';
import { date } from 'quasar';
import { isEmpty } from 'src/common/utils';

interface Props {
  modelValue: string | null;
  format24h?: boolean;
  dense?: boolean;
  disable?: boolean;
  readonly?: boolean;
  config?: any;
}
const props = withDefaults(defineProps<Props>(), {
  // modelValue: date.formatDate(Date.now(), 'YYYY/MM/DD HH:mm'),
  modelValue: null,
  format24h: false,
  dense: false,
  disable: false,
  readonly: false,
  config: () => {
    return {};
  },
});

const emit = defineEmits<{
  (e: 'update:modelValue', value: string): void;
}>();

const dates = computed({
  set(value: any) {
    emit('update:modelValue', value);
  },
  get() {
    return props.modelValue;
  },
});

const timeMask = computed(() => {
  let obj = {
    time: 'HH:mm:ss',
  };
  if (!isEmpty(props.config) && props.config.timeMask) {
    obj.time = props.config.timeMask;
  }
  return obj;
});

const isWithSeconds = computed(() => {
  return timeMask.value.time.length > 6;
});

const timeProxyDate = ref<any>(null);

onMounted(() => {
  getNowDate();
});

function getNowDate() {
  timeProxyDate.value = date.formatDate(Date.now(), timeMask.value.time);
}

function updateProxy() {
  timeProxyDate.value = dates.value;
}

function onClickDate() {
  dates.value = timeProxyDate.value;
}
</script>
<template>
  <div class="hcy-datetime-pick">
    <q-input
      v-model="dates"
      v-bind="config"
      :dense="config.dense === undefined ? props.dense : config.dense"
      :disable="config.disable === undefined ? props.disable : config.disable"
      :readonly="
        config.readonly === undefined ? props.readonly : config.readonly
      "
    >
      <template v-slot:append>
        <q-icon name="access_time" class="cursor-pointer">
          <q-popup-proxy
            v-if="!props.readonly"
            @before-show="updateProxy"
            transition-show="scale"
            transition-hide="scale"
            class="row"
          >
            <q-time
              flat
              square
              v-model="timeProxyDate"
              :mask="timeMask.time"
              :format24h="format24h"
              :with-seconds="isWithSeconds"
              text-color="primary"
              class="my-time"
            >
              <div class="row items-center justify-end q-gutter-sm">
                <q-btn
                  label="此刻"
                  size="sm"
                  color="primary"
                  flat
                  @click="getNowDate"
                />
                <q-btn
                  unelevated
                  label="确定"
                  size="sm"
                  color="primary"
                  @click="onClickDate"
                  v-close-popup
                />
              </div>
            </q-time>
          </q-popup-proxy>
        </q-icon>
      </template>
    </q-input>
  </div>
</template>

<style lang="scss" scoped>
.my-time {
  :deep(.q-time__header) {
    background-color: white;
  }
  :deep(.q-time__main
      .q-time__content
      .q-time__container-child
      .q-time__clock
      .q-time__clock-circle
      .q-time__clock-position--active) {
    color: white !important;
  }
}
</style>
