<script setup lang="ts">
import { onLoad, onShow } from "@dcloudio/uni-app";
import { useLoginStore } from "@/stores/modules/login";
import { computed, ref, reactive } from "vue";
import CustomNavbar from "./components/CustomNavbar.vue";
import CategoryPanel from "./components/CategoryPanel.vue";
import footRight from "./components/footRight.vue";
import PageSkeleton from "./components/PageSkeleton.vue";
import {
  countReview,
  getHomeCategoryAPI,
  getUnreadMessage,
  countUnqualifiedNotification,
  countUnqualifiedNotificationConfirm
} from "@/services/home";
// 获取前台分类数据
const categoryList = ref([
  {
    path: "approveOrder",
    name: "订单批准",
    auth: false,
    imgUrl: "/static/images/approveOrder.png",
    defaultImgUrl: "/static/images/approveOrder-default.png",
  },
  {
    path: "unqualifiedNotification",
    name: "品质异常通知",
    auth: false,
    imgUrl: "/static/images/unqualifiedNotification.png",
    defaultImgUrl: "/static/images/unqualifiedNotification-default.png",
  },
  {
    path: "productionReport",
    name: "生产报工",
    auth: false,
    imgUrl: "/static/images/productionReport.png",
    defaultImgUrl: "/static/images/productionReport-default.png",
  },
  {
    path: "assembleReport",
    name: "装配报工",
    auth: false,
    imgUrl: "/static/images/assembleReport.png",
    defaultImgUrl: "/static/images/assembleReport-default.png",
  },
  {
    path: "pgMaster",
    name: "过程检报工",
    auth: false,
    imgUrl: "/static/images/guochengjian.png",
    defaultImgUrl: "/static/images/assembleReport-default.png",
  },
  {
    path: "zjPgMaster",
    name: "终检报工",
    auth: false,
    imgUrl: "/static/images/zhongjian.png",
    defaultImgUrl: "/static/images/assembleReport-default.png",
  },
  {
    path: "moJuSheJiReport",
    name: "进度上报",
    auth: false,
    imgUrl: "/static/images/mojusheji.png",
    defaultImgUrl: "/static/images/assembleReport-default.png",
  },
  {
    path: "cgdsp",
    name: "采购单审批",
    auth: false,
    imgUrl: "/static/images/pic_cgdsp.png",
    defaultImgUrl: "/static/images/assembleReport-default.png",
   },
  {
    path: "messageNotification",
    name: "消息通知",
    auth: false,
    imgUrl: "/static/images/pic_message.png",
    defaultImgUrl: "/static/images/assembleReport-default.png",
    unReadCount: 0, // 未读消息数
  },
  {
    path: "unqualifiedNotificationConfirm",
    name: "品质异常确认",
    auth: false,
    imgUrl: "/static/images/unqualifiedNotificationConfirm.png",
    defaultImgUrl: "/static/images/unqualifiedNotification-default.png",
  },
]);
const categoryList1 = ref([

]);
const loginStore = useLoginStore();
const isLogin = computed(() => {
  const accessToken = loginStore.userInfo?.accessToken;
  const storage_token = uni.getStorageSync("storage_userInfo")?.accessToken;
  return !!accessToken || !!storage_token;
});
// 是否加载中标记
const isLoading = ref(false);
// 页面加载
onShow(async () => {
  if (isLogin.value) {
    await getHomeCategory();
  } else {
    categoryList.value.forEach((e) => {
      e.auth = true;
    });
  }
});
const getHomeCategory = async () => {
  isLoading.value = true;

  const params = {};
  const data = await getHomeCategoryAPI(params);
  const menus = data?.menus.find((e) => e.path == "/applet")?.children || [];
  if (menus.length) {
    const arr = [];
    categoryList.value.forEach((e) => {
		// if (e.path === 'cgdsp') return;
      const target = menus.find((q) => q.path == e.path);
      e.auth = !!target;
    });
    console.log()
  }

  categoryList1.value = categoryList.value.filter(item => item.auth == true);


  const unReadCount = await getUnreadMessage();
  const count = await countReview();
  var query ={
	  queryType:2,
  }
  var query1 ={
    queryType:1,
  }
  const unqualifiedNotificationCount = await countUnqualifiedNotification(query);
  const unqualifiedNotificationCountConfirm = await countUnqualifiedNotificationConfirm(query1);
  // 把未读数放到消息通知项
  const cgdsp = categoryList1.value.find(
    (e) => e.path === "cgdsp"
  );
  const msgItem = categoryList1.value.find(
      (e) => e.path === "messageNotification"
  );
  const unqualifiedNotificationItem =  categoryList1.value.find(
      (e) => e.path === "unqualifiedNotification"
  );
  const unqualifiedNotificationConfirm = categoryList1.value.find(
      (e) => e.path === "unqualifiedNotificationConfirm"
  );
  if (cgdsp){
    cgdsp.unReadCount = count;
    // cgdsp.auth=true
  }
  if (msgItem) {
    msgItem.unReadCount = unReadCount;
    // msgItem.auth=true
  }
  if(unqualifiedNotificationItem){
	  unqualifiedNotificationItem.unReadCount = unqualifiedNotificationCount
  }
  if(unqualifiedNotificationConfirm){
    unqualifiedNotificationConfirm.unReadCount = unqualifiedNotificationCountConfirm
  }

  if (loginStore.userInfo.userId) {
    const obj = {
      ...loginStore.userInfo,
      nickname: data.user.nickname,
    };
    loginStore.setInfo(obj);
  }
  isLoading.value = false;
};
</script>
<template>
  <view class="viewport">
    <template v-if="isLoading">
      <PageSkeleton />
    </template>
    <template v-else>
      <CustomNavbar />
      <view class="cont">
        <CategoryPanel :list="categoryList1" />
        <footRight />
      </view>
    </template>
  </view>
</template>

<style lang="scss">
page {
  height: 100%;
  overflow: hidden;
  background-color: #f8fafd;
}

.viewport {
  width: 100vw;
  height: 100vh;
  margin-bottom: 300rpx;
  .cont {
    //margin: 100rpx 0;
  }
}
</style>

