import { ref, getCurrentInstance, computed, watch, onMounted, resolveComponent, createElementBlock, openBlock, Fragment, createBlock, createCommentVNode, createElementVNode, normalizeProps, mergeProps, unref, createVNode, guardReactiveProps, isRef, withCtx, resolveDynamicComponent, nextTick } from "vue";
import "../../../node_modules/grid-layout-plus/es/index.mjs";
import * as all from "./components/all.mjs";
import { pageHistory } from "./utils/pageHistory.mjs";
import { usePageEvents } from "./composables/usePageEvents.mjs";
import hooks from "../node_modules/moment/dist/moment.mjs";
/* empty css           */
import _export_sfc from "../../../_virtual/_plugin-vue_export-helper.mjs";
import De from "../../../node_modules/grid-layout-plus/es/components/grid-layout.vue.mjs";
const _hoisted_1 = { class: "grid-wrapper no-scrollbar" };
const _hoisted_2 = { class: "component-content" };
const colNum = 24;
const rowHeight = 10;
const _sfc_main = {
  __name: "page",
  props: {
    parentPage: {
      type: Object,
      required: true
    },
    routePage: {
      type: Object
    },
    params: {
      type: Object,
      default: {}
    }
  },
  setup(__props, { expose: __expose }) {
    var _a;
    const props = __props;
    const configLoading = ref(true);
    const instance = getCurrentInstance();
    const ctx = instance.proxy;
    const formData = ref(props.params);
    function getComp(type) {
      return all[type];
    }
    const gridLayout = ref();
    const layoutData = computed(() => {
      var _a2;
      if (Array.isArray(props.parentPage.layoutData)) {
        return ((_a2 = props.parentPage.layoutData) == null ? void 0 : _a2.map((item) => {
          var _a3, _b;
          item.isResizable = false;
          if ((_b = (_a3 = item.component) == null ? void 0 : _a3.props) == null ? void 0 : _b.autoHeight) {
            item.class = "isAutoHeight";
          }
          return item;
        })) || [];
      }
      return props.parentPage.layoutData;
    });
    const tabbarActive = ref(0);
    const childPage = ref(null);
    function onTabbarChange(pageId) {
      if (!pageId) {
        return;
      }
      const page = props.parentPage.children.find((item) => item.id == pageId);
      if (!page) ;
      else {
        childPage.value = page;
        pageHistory.replace(page);
      }
      console.log("🚀 ~ onTabbarChange ~ page:", page);
    }
    const currentPage = computed(() => {
      return childPage.value || props.parentPage || {};
    });
    watch(
      [() => props.parentPage, () => props.routePage],
      () => {
        var _a2;
        childPage.value = props.routePage;
        if (props.parentPage.footerType == "tabbar") {
          tabbarActive.value = ((_a2 = props.routePage) == null ? void 0 : _a2.id) || props.parentPage.tabbarConfig.tabbarOptions[0].pageId;
        }
      },
      {
        immediate: true
      }
    );
    const fieldsData = ref([]);
    const hiddenFields = ref(props.parentPage.hiddenFields || []);
    if (Array.isArray(props.parentPage.layoutData)) {
      fieldsData.value = ((_a = props.parentPage.layoutData) == null ? void 0 : _a.map((item) => {
        var _a2;
        return { id: item.component.id, ...(_a2 = item.component) == null ? void 0 : _a2.props };
      })) || [];
    }
    const loadFormData = (state) => {
      formData.value = props.params;
      console.log("初始化赋值", formData.value);
    };
    onMounted(async () => {
      loadFormData();
      const pageEvents = usePageEvents(() => ({
        pageEventConfig: props.parentPage.pageEventConfig,
        fields: [...fieldsData.value, ...hiddenFields.value],
        instance,
        formData: formData.value,
        localConfig: props.parentPage,
        components: {
          form: ctx
        }
      }));
      if (pageEvents) {
        pageEvents.handleBeforeUnmount();
        pageEvents.handleUnmounted();
        if (props.parentPage.pageEventConfig) {
          const onCreatedEvent = props.parentPage.pageEventConfig.find(
            (e) => e.name === "onCreated"
          );
          if (onCreatedEvent) {
            console.log("🚀 页面 onCreated 事件触发");
            try {
              await pageEvents.executeEvent("onCreated");
            } catch (error) {
            }
          }
        }
      }
      configLoading.value = false;
    });
    const valueFormatMap = {
      date: "YYYYMMDD",
      week: "YYYYMMDD",
      month: "YYYYMM",
      year: "YYYY",
      datetime: "YYYYMMDDHHmmss"
    };
    const formValueFormatMap = {
      date: "YYYY/MM/DD",
      week: "YYYY/MM/DD",
      month: "YYYY/MM",
      year: "YYYY",
      datetime: "YYYY/MM/DD HH:mm:ss"
    };
    const setFormData = (data) => {
      const processedData = { ...data };
      const processField = (field) => {
        const fieldName = field.fieldName;
        const fieldType = field.fieldType;
        if (!processedData.hasOwnProperty(fieldName)) {
          return;
        }
        const value = processedData[fieldName];
        if (fieldName && (fieldType === "checkbox" || fieldType === "multipleSelection" || fieldType === "cascader" || fieldType === "area" || fieldType === "industry")) {
          let splitStr = "|";
          if (fieldType === "cascader" || fieldType === "area" || fieldType === "industry") {
            splitStr = ",";
          }
          if (typeof value === "string" && value.includes(splitStr)) {
            processedData[fieldName] = value.split(splitStr);
          } else if (typeof value === "string" && value !== "") {
            processedData[fieldName] = [value];
          } else if (value === "" || value === null || value === void 0) {
            processedData[fieldName] = [];
          }
          processedData[fieldName] = processedData[fieldName].map((item) => {
            if (item.indexOf(",") > -1) {
              return item.split(",");
            }
            return item;
          });
        } else if (fieldName && fieldType === "date" && value) {
          const valueFormat = valueFormatMap[field.dateType] || "YYYYMMDD";
          const formValueFormat = formValueFormatMap[field.dateType] || "YYYY/MM/DD";
          processedData[fieldName] = hooks(value, valueFormat).format(
            formValueFormat
          );
        }
      };
      if (fieldsData.value) {
        fieldsData.value.forEach((component) => {
          processField(component);
        });
      }
      if (hiddenFields.value && hiddenFields.value.length > 0) {
        hiddenFields.value.forEach((field) => {
          processField(field);
        });
      }
      formData.value = { ...formData.value, ...processedData };
      nextTick(() => {
        var _a2;
        layoutData.value.forEach((item) => {
          const id = item.component.id;
          const dom = document.getElementById(id);
          const labelDom = dom == null ? void 0 : dom.querySelector(".van-field__label");
          if (labelDom) {
            let height = labelDom.offsetHeight + 20;
            item.h = height / 10;
          }
        });
        (_a2 = gridLayout.value) == null ? void 0 : _a2.resizeEvent();
      });
    };
    __expose({
      setFormData
    });
    return (_ctx, _cache) => {
      const _component_FecMobileNavBar = resolveComponent("FecMobileNavBar");
      const _component_page = resolveComponent("page", true);
      const _component_FecMobileMenuGrid = resolveComponent("FecMobileMenuGrid");
      const _component_FecMobileNoticeBar = resolveComponent("FecMobileNoticeBar");
      const _component_FecMobileDataStat = resolveComponent("FecMobileDataStat");
      const _component_FecMobileAppDetail = resolveComponent("FecMobileAppDetail");
      const _component_FecMobileTabbar = resolveComponent("FecMobileTabbar");
      const _component_FecMobileActionBar = resolveComponent("FecMobileActionBar");
      return openBlock(), createElementBlock(Fragment, null, [
        __props.parentPage.isNavBar ? (openBlock(), createBlock(_component_FecMobileNavBar, normalizeProps(mergeProps({ key: 0 }, __props.parentPage.navBarConfig)), null, 16)) : createCommentVNode("", true),
        createElementVNode("div", _hoisted_1, [
          __props.parentPage.footerType == "tabbar" ? (openBlock(), createElementBlock(Fragment, { key: 0 }, [
            unref(childPage) ? (openBlock(), createBlock(_component_page, {
              key: 0,
              parentPage: unref(childPage)
            }, null, 8, ["parentPage"])) : createCommentVNode("", true)
          ], 64)) : (openBlock(), createElementBlock(Fragment, { key: 1 }, [
            __props.parentPage.type == "home" ? (openBlock(), createElementBlock(Fragment, { key: 0 }, [
              createVNode(_component_FecMobileMenuGrid, normalizeProps(guardReactiveProps(__props.parentPage.layoutData.menuGridProps)), null, 16),
              createVNode(_component_FecMobileNoticeBar),
              createVNode(_component_FecMobileDataStat)
            ], 64)) : __props.parentPage.type == "approval" ? (openBlock(), createBlock(_component_FecMobileAppDetail, mergeProps({ key: 1 }, __props.parentPage.layoutData.appDetailProps, {
              actionBar: __props.parentPage.layoutData.actionBar,
              params: __props.params
            }), null, 16, ["actionBar", "params"])) : (openBlock(), createBlock(unref(De), {
              key: 2,
              ref_key: "gridLayout",
              ref: gridLayout,
              layout: unref(layoutData),
              "onUpdate:layout": _cache[0] || (_cache[0] = ($event) => isRef(layoutData) ? layoutData.value = $event : null),
              "row-height": rowHeight,
              margin: [0, 0],
              "col-num": colNum,
              "use-css-transforms": false,
              "is-draggable": false,
              "is-resizable": false,
              style: { "height": "100%" }
            }, {
              item: withCtx(({ item: { component } }) => [
                createElementVNode("div", _hoisted_2, [
                  (openBlock(), createBlock(resolveDynamicComponent(getComp(component.fieldType)), mergeProps(component.props, {
                    modelValue: unref(formData)[component.props.fieldName],
                    "onUpdate:modelValue": ($event) => unref(formData)[component.props.fieldName] = $event,
                    dataSources: unref(currentPage).dataSources,
                    formData: unref(formData),
                    compId: component.id,
                    fields: [...unref(fieldsData), ...unref(hiddenFields)]
                  }), null, 16, ["modelValue", "onUpdate:modelValue", "dataSources", "formData", "compId", "fields"]))
                ])
              ]),
              _: 1
            }, 8, ["layout"]))
          ], 64))
        ]),
        __props.parentPage.footerType == "tabbar" ? (openBlock(), createBlock(_component_FecMobileTabbar, mergeProps({
          key: 1,
          modelValue: unref(tabbarActive),
          "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => isRef(tabbarActive) ? tabbarActive.value = $event : null)
        }, __props.parentPage.tabbarConfig, { onChange: onTabbarChange }), null, 16, ["modelValue"])) : createCommentVNode("", true),
        __props.parentPage.footerType == "actionBar" ? (openBlock(), createBlock(_component_FecMobileActionBar, normalizeProps(mergeProps({ key: 2 }, __props.parentPage.actionBarConfig)), null, 16)) : createCommentVNode("", true)
      ], 64);
    };
  }
};
const MobilePage = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-726a6dad"]]);
export {
  MobilePage as default
};
