<template>
  <a-date-picker :show-time="showTime" :readonly="readonly" :disabled="see || disabled" :value="formattedValue"
                 :format="format" :picker="type" @change="handleChange"  :placeholder="placeholder" :style="style" v-bind="$attrs"/>
</template>
<script>
import dayjs from "dayjs";
import weekday from "dayjs/plugin/weekday";
import localeData from "dayjs/plugin/localeData";

dayjs.extend(weekday);
dayjs.extend(localeData);
dayjs.locale("zh-cn");

export default {
  name: "PaasDate",
  inheritAttrs: false, // 禁用自动继承，手动控制透传
  computed: {
    see() {
      return this.$store?.forms?.see || false;
    },
    formattedValue() {
      if (!this.modelValue) {
        return null;
      }
      // 如果是数组（比如范围选择器），需要逐个转换
      if (Array.isArray(this.modelValue)) {
        return this.modelValue.map(v => v ? dayjs(v) : null);
      }
      // 如果已经是 dayjs 对象，直接返回
      if (dayjs.isDayjs(this.modelValue)) {
        return this.modelValue;
      }
      // 字符串或其他类型转换为 dayjs 对象
      return dayjs(this.modelValue);
    }
  },
  props: {
    placeholder: {
      type: String,
      default: "",
    },
    type: {
      type: String,
      default: "date"
    },
    icon: String,
    readonly: {
      type: Boolean,
      default: false
    },
    disabled: {
      type: Boolean,
      default: false
    },
    modelValue: {
      type: [String, Array, Date],
      default: null
    },
    showTime: {
      type: Boolean,
      default: false
    },
    format: {
      type: String,
      default: "YYYY-MM-DD"
    },
    onChange: {
      type: Function,
      default: null
    },
    style: { type: [String, Object], default: "width: 100%;" }
  },
  data() {
    return {};
  },
  methods: {
    handleChange(date) {
      if (this.onChange) {
        this.onChange(date);
      }
      if (Array.isArray(date)) {
        this.$emit("update:modelValue", date.map(d => d?.format(this.format || 'YYYY-MM-DD')));
      } else {
        this.$emit("update:modelValue", date?.format(this.format || 'YYYY-MM-DD'));
      }
    }
  },
};
</script>