/* empty css                                               */
/* empty css                                                */
/* empty css                                               */
/* empty css                                                  */
/* empty css                                                */
/* empty css                                                  */
/* empty css                                                 */
/* empty css                                                */
/* empty css                                                     */
/* empty css                                               */
/* empty css                                              */
/* empty css                                                 */
/* empty css                                                       */
import { ref, computed, watch, createBlock, openBlock, unref, mergeProps, isRef, createSlots, withCtx, createVNode, withModifiers } from "vue";
import { MobileField } from "../field/index.mjs";
import hooks from "../../../../node_modules/moment/dist/moment.mjs";
import { parseDateFormatter } from "../../../utils/dateUtil.mjs";
import { DatePicker } from "../../../../node_modules/vant/es/date-picker/index.mjs";
import { Popup } from "../../../../node_modules/vant/es/popup/index.mjs";
import { Icon } from "../../../../node_modules/vant/es/icon/index.mjs";
const _sfc_main = {
  __name: "FieldDatePicker",
  props: {
    modelValue: {
      type: String,
      default: ""
    },
    // textFormat: {
    //   type: String,
    //   default: "YYYY-MM-DD",
    // },
    // valueFormat: {
    //   type: String,
    //   default: "YYYYMMDD",
    // },
    dateColumnsType: {
      type: String,
      default: "year,month,day"
    },
    dateTextFormat: {
      //0:YYYYMMDD;1: YYYY-MM-DD;2：YYYY/MM/DD；3：YYYY年MM月DD日
      type: String,
      default: "1"
    },
    disabled: {
      type: Boolean,
      default: false
    },
    readonly: {
      type: Boolean,
      default: false
    },
    "is-link": false
  },
  emits: ["update:modelValue"],
  setup(__props, { emit: __emit }) {
    const props = __props;
    const fieldTextValue = ref("");
    const showPicker = ref(false);
    const emit = __emit;
    let textFormat = parseDateFormatter(
      props.dateTextFormat,
      props.dateColumnsType
    );
    let valueFormat = parseDateFormatter("0", props.dateColumnsType);
    const pickerValue = computed(() => {
      let date;
      if (!props.modelValue) {
        date = hooks();
      } else {
        date = hooks(props.modelValue, valueFormat);
      }
      const year = date.year();
      const month = date.month() + 1;
      const day = date.date();
      let value = [];
      const columnsTypeArr = props.dateColumnsType.split(",");
      if (columnsTypeArr.length == 1) {
        value = [year];
      } else if (columnsTypeArr.length == 2) {
        value = [year, month];
      } else if (columnsTypeArr.length == 3) {
        value = [year, month, day];
      }
      return value;
    });
    const columnsType = computed(() => {
      return props.dateColumnsType.split(",");
    });
    const formatter = (type, option) => {
      if (type === "year") {
        option.text += "年";
      } else if (type === "month") {
        option.text += "月";
      } else if (type === "day") {
        option.text += "日";
      }
      return option;
    };
    watch(
      () => props.modelValue,
      (value) => {
        if (!value) {
          fieldTextValue.value = "";
          return;
        }
        fieldTextValue.value = hooks(value, valueFormat).format(textFormat);
      },
      { immediate: true }
    );
    const onConfirm = ({ selectedValues, selectedOptions, selectedIndexes }) => {
      const val = hooks(
        `${selectedValues[0]}${selectedValues[1]}${selectedValues[2]}`,
        "YYYYMMDD"
      ).format(valueFormat);
      emit("update:modelValue", val);
      showPicker.value = false;
    };
    return (_ctx, _cache) => {
      const _component_van_icon = Icon;
      const _component_van_date_picker = DatePicker;
      const _component_van_popup = Popup;
      return openBlock(), createBlock(unref(MobileField), mergeProps(_ctx.$attrs, {
        modelValue: unref(fieldTextValue),
        "onUpdate:modelValue": _cache[4] || (_cache[4] = ($event) => isRef(fieldTextValue) ? fieldTextValue.value = $event : null),
        isLink: __props.readonly || __props.disabled ? false : true,
        readonly: "",
        disabled: __props.disabled,
        onOpenPicker: _cache[5] || (_cache[5] = () => {
          if (!__props.readonly) {
            showPicker.value = true;
          }
        })
      }), createSlots({
        default: withCtx(() => [
          createVNode(_component_van_popup, {
            show: unref(showPicker),
            "onUpdate:show": _cache[3] || (_cache[3] = ($event) => isRef(showPicker) ? showPicker.value = $event : null),
            "destroy-on-close": "",
            position: "bottom",
            teleport: "body"
          }, {
            default: withCtx(() => [
              createVNode(_component_van_date_picker, mergeProps(_ctx.$attrs, {
                modelValue: unref(pickerValue),
                "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => isRef(pickerValue) ? pickerValue.value = $event : null),
                readonly: false,
                onConfirm,
                columnsType: unref(columnsType),
                formatter,
                onCancel: _cache[2] || (_cache[2] = ($event) => showPicker.value = false)
              }), null, 16, ["modelValue", "columnsType"])
            ]),
            _: 1
          }, 8, ["show"])
        ]),
        _: 2
      }, [
        !__props.readonly && !__props.disabled && unref(fieldTextValue) ? {
          name: "right-icon",
          fn: withCtx(() => [
            createVNode(_component_van_icon, {
              name: "clear",
              class: "custom-close-icon",
              onClick: _cache[0] || (_cache[0] = withModifiers(($event) => emit("update:modelValue", null), ["stop"]))
            })
          ]),
          key: "0"
        } : void 0
      ]), 1040, ["modelValue", "isLink", "disabled"]);
    };
  }
};
export {
  _sfc_main as default
};
