"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const Vue = require("vue");
require("../../../node_modules/grid-layout-plus/es/index.js");
const all = require("./components/all.js");
const pageHistory = require("./utils/pageHistory.js");
const usePageEvents = require("./composables/usePageEvents.js");
const moment = require("../node_modules/moment/dist/moment.js");
;/* empty css           */
const _pluginVue_exportHelper = require("../../../_virtual/_plugin-vue_export-helper.js");
const gridLayout_vue = require("../../../node_modules/grid-layout-plus/es/components/grid-layout.vue.js");
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 = Vue.ref(true);
    const instance = Vue.getCurrentInstance();
    const ctx = instance.proxy;
    const formData = Vue.ref(props.params);
    function getComp(type) {
      return all[type];
    }
    const gridLayout = Vue.ref();
    const layoutData = Vue.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 = Vue.ref(0);
    const childPage = Vue.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.pageHistory.replace(page);
      }
      console.log("🚀 ~ onTabbarChange ~ page:", page);
    }
    const currentPage = Vue.computed(() => {
      return childPage.value || props.parentPage || {};
    });
    Vue.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 = Vue.ref([]);
    const hiddenFields = Vue.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);
    };
    Vue.onMounted(async () => {
      loadFormData();
      const pageEvents = usePageEvents.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] = moment.default(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 };
      Vue.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 = Vue.resolveComponent("FecMobileNavBar");
      const _component_page = Vue.resolveComponent("page", true);
      const _component_FecMobileMenuGrid = Vue.resolveComponent("FecMobileMenuGrid");
      const _component_FecMobileNoticeBar = Vue.resolveComponent("FecMobileNoticeBar");
      const _component_FecMobileDataStat = Vue.resolveComponent("FecMobileDataStat");
      const _component_FecMobileAppDetail = Vue.resolveComponent("FecMobileAppDetail");
      const _component_FecMobileTabbar = Vue.resolveComponent("FecMobileTabbar");
      const _component_FecMobileActionBar = Vue.resolveComponent("FecMobileActionBar");
      return Vue.openBlock(), Vue.createElementBlock(Vue.Fragment, null, [
        __props.parentPage.isNavBar ? (Vue.openBlock(), Vue.createBlock(_component_FecMobileNavBar, Vue.normalizeProps(Vue.mergeProps({ key: 0 }, __props.parentPage.navBarConfig)), null, 16)) : Vue.createCommentVNode("", true),
        Vue.createElementVNode("div", _hoisted_1, [
          __props.parentPage.footerType == "tabbar" ? (Vue.openBlock(), Vue.createElementBlock(Vue.Fragment, { key: 0 }, [
            Vue.unref(childPage) ? (Vue.openBlock(), Vue.createBlock(_component_page, {
              key: 0,
              parentPage: Vue.unref(childPage)
            }, null, 8, ["parentPage"])) : Vue.createCommentVNode("", true)
          ], 64)) : (Vue.openBlock(), Vue.createElementBlock(Vue.Fragment, { key: 1 }, [
            __props.parentPage.type == "home" ? (Vue.openBlock(), Vue.createElementBlock(Vue.Fragment, { key: 0 }, [
              Vue.createVNode(_component_FecMobileMenuGrid, Vue.normalizeProps(Vue.guardReactiveProps(__props.parentPage.layoutData.menuGridProps)), null, 16),
              Vue.createVNode(_component_FecMobileNoticeBar),
              Vue.createVNode(_component_FecMobileDataStat)
            ], 64)) : __props.parentPage.type == "approval" ? (Vue.openBlock(), Vue.createBlock(_component_FecMobileAppDetail, Vue.mergeProps({ key: 1 }, __props.parentPage.layoutData.appDetailProps, {
              actionBar: __props.parentPage.layoutData.actionBar,
              params: __props.params
            }), null, 16, ["actionBar", "params"])) : (Vue.openBlock(), Vue.createBlock(Vue.unref(gridLayout_vue.default), {
              key: 2,
              ref_key: "gridLayout",
              ref: gridLayout,
              layout: Vue.unref(layoutData),
              "onUpdate:layout": _cache[0] || (_cache[0] = ($event) => Vue.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: Vue.withCtx(({ item: { component } }) => [
                Vue.createElementVNode("div", _hoisted_2, [
                  (Vue.openBlock(), Vue.createBlock(Vue.resolveDynamicComponent(getComp(component.fieldType)), Vue.mergeProps(component.props, {
                    modelValue: Vue.unref(formData)[component.props.fieldName],
                    "onUpdate:modelValue": ($event) => Vue.unref(formData)[component.props.fieldName] = $event,
                    dataSources: Vue.unref(currentPage).dataSources,
                    formData: Vue.unref(formData),
                    compId: component.id,
                    fields: [...Vue.unref(fieldsData), ...Vue.unref(hiddenFields)]
                  }), null, 16, ["modelValue", "onUpdate:modelValue", "dataSources", "formData", "compId", "fields"]))
                ])
              ]),
              _: 1
            }, 8, ["layout"]))
          ], 64))
        ]),
        __props.parentPage.footerType == "tabbar" ? (Vue.openBlock(), Vue.createBlock(_component_FecMobileTabbar, Vue.mergeProps({
          key: 1,
          modelValue: Vue.unref(tabbarActive),
          "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => Vue.isRef(tabbarActive) ? tabbarActive.value = $event : null)
        }, __props.parentPage.tabbarConfig, { onChange: onTabbarChange }), null, 16, ["modelValue"])) : Vue.createCommentVNode("", true),
        __props.parentPage.footerType == "actionBar" ? (Vue.openBlock(), Vue.createBlock(_component_FecMobileActionBar, Vue.normalizeProps(Vue.mergeProps({ key: 2 }, __props.parentPage.actionBarConfig)), null, 16)) : Vue.createCommentVNode("", true)
      ], 64);
    };
  }
};
const MobilePage = /* @__PURE__ */ _pluginVue_exportHelper.default(_sfc_main, [["__scopeId", "data-v-726a6dad"]]);
exports.default = MobilePage;
