<template>
  <a-date-picker
    v-model:value="dataValue"
    :value-format="valueFormat"
    :disabled="readonly || disabled"
    allowClear
    @change="dataValueChange"
    @click="dataValueClick"
    @blur="dataValueBlur"
    @focus="dataValueFocus"
    class="w-full"
    :disabled-date="disabledDate"
    :placeholder="props.record?.placeholderContent || placeholder"
  ></a-date-picker>
</template>

<script setup name="FormDate">
import moment from 'moment';
import { ref, onMounted, watch } from 'vue';

const props = defineProps({
  value: {
    type: String,
    default: ''
  },
  //保存的值
  valueFormat: {
    type: String,
    default: 'YYYY-MM-DD'
  },
  //是否禁用
  disabled: {
    type: Boolean,
    default: false
  },
  record: {
    type: Object,
    default: {}
  },
  placeholder: {
    type: String,
    default: '请选择日期'
  },
  // 是否只读
  readonly: {
    type: Boolean,
    default: false
  }
});

const emits = defineEmits(['update:value', 'onBlur', 'onClick', 'onChange', 'onFocus']);

// 选中的值
const dataValue = ref('');

onMounted(() => {
  dataValue.value = props.value;
});

watch(
  () => props.value,
  val => {
    dataValue.value = val;
  },
  { deep: true }
);

// 更改值
const dataValueChange = () => {
  emits('update:value', dataValue.value);
  emits('onChange', props.record);
};
// 点击
const dataValueClick = () => {
  emits('onClick', props.record);
};
// 失去焦点
const dataValueBlur = () => {
  emits('onBlur', props.record);
};
// 获取焦点
const dataValueFocus = () => {
  emits('onFocus', props.record);
};

// 禁用
const disabledDate = current => {
  let valueFlag = false;
  if (props.record?.formItemDate?.disableCurrentBefore) {
    valueFlag = current && current < moment().subtract(1, 'days').endOf('day');
  }
  return valueFlag;
};
</script>

<style></style>
