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

interface Props {
  modelValue: string[] | null;
  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,
  dense: false,
  disable: false,
  readonly: false,
  config: () => {
    return {};
  },
});

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

const myDateMultiple = ref<any>(null);

const dates = computed({
  set(value: any) {
    let arr = [];
    if (value) {
      arr = value.split(',');
    }
    emit('update:modelValue', arr);
  },
  get() {
    return props.modelValue;
  },
});

const dateMask = computed(() => {
  let obj = {
    date: 'YYYY/MM/DD',
  };
  if (!isEmpty(props.config) && props.config.dateMask) {
    const maskList = props.config.dateMask.split(' ');
    obj.date = maskList[0];
  }
  return obj;
});

const dateProxyDate = ref<any>([]);
const dateLocale = reactive({
  daysShort: '六_日_一_二_三_四_五'.split('_'),
  months:
    '一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月'.split(
      '_'
    ),
  monthsShort: '1月_2月_3月_4月_5月_6月_7月_8月_9月_10月_11月_12月'.split('_'),
  firstDayOfWeek: 1,
  format24h: true,
  pluralDay: 'dias',
});

const toolTipData = computed(() => {
  let str = '';
  dates.value.map((item: string) => {
    str += item + '<br />';
  });
  return str;
});

onMounted(() => {
  const inputElement =
    myDateMultiple.value.$el.firstElementChild.firstElementChild
      .firstElementChild.firstElementChild;
  inputElement.setAttribute('unselectable', 'on');
  inputElement.setAttribute('readonly', true);
});

function updateProxy() {
  const _dates = dates.value;
  if (!isEmpty(_dates)) {
    dateProxyDate.value = _dates;
  } else {
    dateProxyDate.value = [];
  }
}

function onClickDate() {
  dates.value = dateProxyDate.value.join(',');
}
</script>
<template>
  <div class="hcy-datetime-pick">
    <q-input
      v-model="dates"
      v-bind="config"
      ref="myDateMultiple"
      :dense="config.dense === undefined ? props.dense : config.dense"
      :disable="config.disable === undefined ? props.disable : config.disable"
      :readonly="
        config.readonly === undefined ? props.readonly : config.readonly
      "
    >
      <q-tooltip v-if="!isEmpty(dates)">
        <span v-html="toolTipData"></span
      ></q-tooltip>
      <template v-slot:append>
        <q-icon name="event" class="cursor-pointer">
          <q-popup-proxy
            v-if="!props.readonly"
            @before-show="updateProxy"
            transition-show="scale"
            transition-hide="scale"
            class="row"
          >
            <q-date
              flat
              square
              minimal
              multiple
              v-model="dateProxyDate"
              :locale="dateLocale"
              :mask="dateMask.date"
              class="my-date"
            >
              <div class="row items-center justify-end q-gutter-sm">
                <q-btn
                  unelevated
                  label="确定"
                  size="sm"
                  color="primary"
                  @click="onClickDate"
                  v-close-popup
                />
              </div>
            </q-date>
          </q-popup-proxy>
        </q-icon>
      </template>
    </q-input>
  </div>
</template>

<style lang="scss" scoped>
.my-date {
  :deep(.q-date__main .q-date__content .q-date__view .q-date__navigation) {
    > .relative-position {
      color: $primary;
    }
  }
}
.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>
