<template>
  <view class="bg-header">
    <u-image src="../../static/img/background.png" width="100%" height="100%" />
  </view>
  <view class="home-container">
    <!-- 用户信息部分 -->
    <view class="user-info">
      <u-image
        width="240rpx"
        height="240rpx"
        mode="aspectFit"
        :src="'data:image/png;base64,' + globalStore.infoData?.img"
        border-radius="24"
      />
      <text class="name">{{ globalStore.infoData?.Name }}</text>
      <view v-if="globalStore.Phone" class="info">
        <text>班主任</text>
      </view>
    </view>

    <view class="menu-title">
      <view class="half-circle"></view>
      <view class="title">服务功能</view>
    </view>

    <view class="user-menu">
      <view
        class="menu-item"
        v-for="item in menuList"
        :key="item.id"
        @tap="gridChange(item.id)"
      >
        <u-image :src="item.src" width="120rpx" height="120rpx" />
        <text>{{ item.name }}</text>
      </view>
    </view>
    <view class="bg-footer">
      <u-image
        src="../../static/img/mask_group.png"
        width="100%"
        height="100%"
      />
    </view>
  </view>
</template>

<script setup lang="ts">
import { getUrlCode } from "@/utils/util";
import { useTeachStore } from "@/stores/modules/teachStore";
import { login, getOpenId } from "@/api/login";

const globalStore = useTeachStore();
const code = ref();

const menuList = [
  {
    id: 0,
    src: "../../static/img/Ribbon1.png",
    name: "班级管理",
  },
  {
    id: 1,
    src: "../../static/img/Ribbon2.png",
    name: "我的课程",
  },
  {
    id: 2,
    src: "../../static/img/Ribbon3.png",
    name: "考勤定位",
  },
  {
    id: 3,
    src: "../../static/img/Ribbon4.png",
    name: "讲师列表",
  },
  {
    id: 4,
    src: "../../static/img/Ribbon5.png",
    name: "现场教学",
  },
  {
    id: 5,
    src: "../../static/img/Ribbon6.png",
    name: "培训评价",
  },
];

const toClass = () => {
  uni.navigateTo({
    url: "/pagesTeach/class/index",
  });
};

const toCourse = () => {
  uni.navigateTo({
    url: "/pagesTeach/course/index",
  });
};
const toSetPositioning = () => {
  uni.navigateTo({
    url: "/pagesTeach/setPositioning/index",
  });
};
const toTeachPoints = () => {
  uni.navigateTo({
    url: "/pagesTeach/teachPoints/index",
  });
};

const gridChange = (type: number) => {
  switch (type) {
    case 0:
      toClass();
      break;
    case 1:
      toCourse();
      break;
    case 2:
      toSetPositioning();
      break;
    case 3:
      uni.navigateTo({
        url: "/pagesTeach/lecturer/index",
      });
      break;
    case 4:
      toTeachPoints();
      break;
    case 5:
      uni.navigateTo({
        url: "/pagesTeach/trainingEvaluation/index",
      });
      break;
  }
};
const toLogin = () => {
  uni.reLaunch({
    url: "/pagesTeach/login/index",
  });
};

onMounted(async () => {
  // 获取微信公众号跳转h5获取code
  code.value = getUrlCode().code;
  // alert("老师获取的code值：" + code.value);
  if (code.value && !globalStore.infoData.Name && !globalStore.openId) {
    try {
      const { data: res }: any = await getOpenId({ code: code.value });
      if (res.code === "200") {
        globalStore.setOpenId(res.data.OpenID);
        const data = {
          OpenID: res.data.OpenID,
          type: 0,
        };
        const { data: info }: any = await login(data);
        if (info.code === "200") {
          globalStore.setInfoData(info.data[0]);
        } else if (!globalStore.infoData.Name) toLogin();
      } else toLogin();
    } catch (error) {
      console.error("登录失败", error);
    }
  } else {
    // 如果无code跳转到登录页面
    if (!globalStore.infoData.Name) toLogin();
  }
});
</script>

<style lang="scss" scoped>
page {
  background-color: #ffffff;
}
.bg-header {
  position: relative;
}
.home-container {
  position: absolute;
  top: 3%;
  width: 100%;

  .user-info {
    margin: 40rpx 0 40rpx 0;
    width: 100%;
    height: 30%;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;

    .name {
      font-size: 36rpx;
      font-weight: bold;
      padding: 20rpx 0;
      color: #000;
    }

    .info {
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }

  .menu-title {
    margin: 0 32rpx;
    width: 100%;
    display: flex;
    margin-bottom: 32rpx;

    .title {
      font-size: 34rpx;
      font-weight: 600;
      color: #222222;
      line-height: 40rpx;
    }
  }

  .user-menu {
    // height: 238rpx;
    margin: 0 32rpx;
    padding: 48rpx;
    background: transparent;
    box-shadow: 0rpx 8rpx 60rpx 0rpx rgba(0, 0, 0, 0.05);
    border-radius: 24rpx;
    display: flex;
    flex-wrap: wrap;
    box-sizing: border-box;

    .menu-item {
      text-align: center;
      flex-basis: calc((100% - 96rpx) / 3);
      // height: 200rpx;
      display: flex;
      flex-direction: column;
      align-items: center;
      margin-right: 48rpx;
      margin-bottom: 24rpx;
    }

    & .menu-item:nth-child(3n) {
      margin-right: 0;
    }
  }
}
</style>
