<template>
  <div id="home">
    <!--已入驻-->
    <go-examine v-if="isExaime" />
    <!-- 第一步  未入驻 -->
    <go-enter
      :current-step="currentStep"
      :store-application-status="storeApplicationStatus"
      @update:current-step="handleUpdateCurrentStep"
      v-if="isEntry && currentStep === 0"
    />
    <!-- 第二步 -->
    <go-flow
      v-if="isEntry && currentStep > 0"
      :current-step="currentStep"
      @update:current-step="handleUpdateCurrentStep"
      @update:get-user-info="getUserInfo"
      :store-application-status="storeApplicationStatus"
    />
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watch } from "vue";
import { localGet, localSet } from "@/utils/index";
import goEnter from "./components/go-enter.vue";
import goFlow from "./components/go-flow.vue";
import goExamine from "./components/go-examine.vue";
import { getMerchantByPhone, getDetail } from "@/api/modules/homeEntry";
import { is } from "@/utils/is";
import { useAuthStore } from "@/stores/modules/auth";

const authStore = useAuthStore();
const isEntry = ref<boolean>(false);
const isExaime = ref<boolean>(false);

onMounted(() => {
  getUserInfo();
});

// 当前步骤：0-首页，1-第一步，2-第二步
const currentStep = ref(0);
const storeId = ref<Number | undefined>(undefined);
// 处理更新currentStep事件
const handleUpdateCurrentStep = (step: number) => {
  currentStep.value = step;
};
let storeApplicationStatus = ref<number | undefined>(undefined);

// 隐藏财务管理菜单的函数
const hideFinancialManagementMenu = () => {
  const hideMenus = (menuList: any[]) => {
    menuList.forEach(menu => {
      if (menu.name && menu.name === "financialManagement") {
        menu.meta.isHide = true;
      }
      if (menu.children && menu.children.length > 0) {
        hideMenus(menu.children);
      }
    });
  };
  if (authStore.authMenuList && authStore.authMenuList.length > 0) {
    hideMenus(authStore.authMenuList);
  }
};

// 显示财务管理菜单的函数
const showFinancialManagementMenu = () => {
  const showMenus = (menuList: any[]) => {
    menuList.forEach(menu => {
      if (menu.name && menu.name === "financialManagement") {
        menu.meta.isHide = false;
      }
      if (menu.children && menu.children.length > 0) {
        showMenus(menu.children);
      }
    });
  };
  if (authStore.authMenuList && authStore.authMenuList.length > 0) {
    showMenus(authStore.authMenuList);
  }
};

// 根据审核状态控制菜单显示/隐藏
const updateMenuVisibility = (status: number | undefined) => {
  if (status === 1) {
    // 审核通过，显示财务管理菜单
    showFinancialManagementMenu();
  } else if (status === 2) {
    // 审核拒绝，隐藏财务管理菜单
    hideFinancialManagementMenu();
  }
};

const getUserInfo = async () => {
  try {
    const geekerUser = localGet("geeker-user");
    if (!geekerUser || !geekerUser.userInfo || !geekerUser.userInfo.phone) {
      console.error("用户信息不存在");
      return;
    }
    let param = {
      phone: geekerUser.userInfo.phone
    };
    const res: any = await getMerchantByPhone(param);
    console.log("getMerchantByPhone返回数据:", res.data);
    storeId.value = res.data.storeId;
    if (res && res.code == 200 && res.data) {
      // 更新缓存中的 storeId，确保保留businessSection字段
      geekerUser.userInfo = {
        ...geekerUser.userInfo,
        ...res.data,
        businessSection: geekerUser.userInfo.businessSection
      };
      localSet("geeker-user", geekerUser);
      // 同时更新 createdId 缓存
      if (res.data.storeId) {
        localSet("createdId", res.data.storeId);
      }
      if (res.data.storeId == null) {
        isEntry.value = true;
      }
      if (res.data.storeId != null) {
        let param1 = {
          id: res.data.storeId
        };
        const resStore: any = await getDetail(param1);
        if (resStore && resStore.code == 200 && resStore.data) {
          storeApplicationStatus.value = resStore.data.storeApplicationStatus;
          // 有店铺时重新拉取菜单，使价目表、演出等“有店铺才显示”的菜单正确展示
          authStore.getAuthMenuList();
          // 如果是等待审核(0)或审核拒绝(2)，且当前步骤为0，显示 go-enter 组件
          // 如果用户已经主动跳转到其他步骤，则不重置步骤
          if ((storeApplicationStatus.value == 0 || storeApplicationStatus.value == 2) && currentStep.value === 0) {
            currentStep.value = 0;
          }

          if (resStore.data.storeApplicationStatus !== 1 && res.data.storeId != null) {
            //storeId && storeDetail.storeApplicationStatus === 1
            isEntry.value = true;
          } else {
            isExaime.value = true;
          }

          // 获取到审核状态后，立即更新菜单显示/隐藏
          updateMenuVisibility(storeApplicationStatus.value);
        }
      }
    }
  } catch (error) {
    console.error(error);
  }
};

// 监听审核状态变化，更新菜单显示/隐藏
watch(
  () => storeApplicationStatus.value,
  status => {
    updateMenuVisibility(status);
  }
);

// 监听菜单列表变化，根据审核状态显示或隐藏财务管理菜单
watch(
  () => authStore.authMenuList.length,
  newLength => {
    if (newLength > 0 && storeApplicationStatus.value !== undefined) {
      updateMenuVisibility(storeApplicationStatus.value);
    }
  }
);
</script>

<style scoped lang="scss"></style>
