<template>
  <headers
    title="西部人力培训中心"
    background="#ffffff"
    icon-name=""
    back-icon-name=""
  >
    <template #default>
      <u-image
        src="../../static/img/Frame.png"
        width="40rpx"
        height="40rpx"
        style="margin-right: 32rpx"
        @tap="toMy"
      />
    </template>
  </headers>
  <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"
        :src="baseUrl + '/' + globalStore.infoData?.FIMAGEFILESERVER"
        border-radius="24"
      />
      <text class="name">{{ globalStore.infoData?.FNAME }}</text>
      <view class="info">
        <text>{{ globalStore.infoData?.FWORKUNIT1 }}</text>
        <u-line
          color="rgba(34, 34, 34, 0.9)"
          direction="col"
          length="30rpx"
          :hair-line="false"
          margin="0 20rpx"
        />
        <text>{{ globalStore.infoData?.FDUTIES }}</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>
  <view class="bg-footer">
    <u-image src="../../static/img/mask_group.png" width="100%" height="100%" />
  </view>
</template>

<script setup lang="ts">
import { getUrlCode } from "@/utils/util";
import { login, getOpenId } from "@/api/login";
import { baseUrl } from "@/utils/request";
import { useStudentStore } from "@/stores/modules/studentStore";
import headers from "@/components/header/index.vue";

const globalStore = useStudentStore();
const code = ref();
const menuList = [
  {
    id: 0,
    src: "../../static/img/Ribbon10@2x.png",
    name: "培训报名",
  },
  {
    id: 1,
    src: "../../static/img/Ribbon2.png",
    name: "我的课程",
  },
  {
    id: 2,
    src: "../../static/img/Ribbon5.png",
    name: "我的班级",
  },
  {
    id: 3,
    src: "../../static/img/kaoq.png",
    name: "我的考勤",
  },
];

const toTrainRegistration = () => {
  if (!globalStore.infoData.FMOBILE)
    return uni.navigateTo({
      url: "/pages/login/index",
    });
  uni.navigateTo({
    url: "/pages/trainRegistration/index",
  });
};
const toMyAttendance = () => {
  if (!globalStore.infoData.FMOBILE)
    return uni.navigateTo({
      url: "/pages/login/index",
    });
  uni.navigateTo({
    url: "/pages/myAttendance/index",
  });
};

const toCourse = () => {
  if (!globalStore.infoData.FMOBILE)
    return uni.navigateTo({
      url: "/pages/login/index",
    });
  uni.navigateTo({
    url: "/pages/course/index",
  });
};

const toClass = () => {
  if (!globalStore.infoData.FMOBILE)
    return uni.navigateTo({
      url: "/pages/login/index",
    });
  uni.navigateTo({
    url: "/pages/class/index",
  });
};

const toMy = () => {
  if (!globalStore.infoData.FMOBILE)
    return uni.navigateTo({
      url: "/pages/login/index",
    });
  uni.navigateTo({
    url: "/pages/my/index",
  });
};

const toLogin = () => {
  uni.reLaunch({
    url: "/pages/login/index",
  });
};

const toRegister = () => {
  uni.navigateTo({
    url: "/pages/login/register",
  });
};

const gridChange = (type: number) => {
  switch (type) {
    case 0:
      toTrainRegistration();
      break;
    case 1:
      toCourse();
      break;
    case 3:
      toMyAttendance();
      break;
    default:
      toClass();
      break;
  }
};

onMounted(async () => {
  // 获取微信公众号跳转h5获取code
  code.value = getUrlCode().code;
  // alert("学生获取的code值：" + code.value);
  if (code.value && !globalStore.infoData.FMOBILE && !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: 1,
        };
        const { data: info }: any = await login(data);
        if (info.code === "200") {
          globalStore.setInfoData(info.data[0]);
        } else if (info.code == 501 && !globalStore.infoData?.FSTUDENTID) {
          // 新用户，跳转到注册页面
          toRegister();
        } else if (!globalStore.infoData?.FSTUDENTID) toLogin(); // 报错500跳转到登录页面
      } else toLogin();
    } catch (error) {
      console.error("登录失败", error);
    }
  } else {
    // 如果无code跳转到登录页面
    if (!globalStore.infoData.FMOBILE) toLogin();
  }
});
</script>

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

  .user-info {
    padding-top: 96rpx;
    width: 100%;
    // height: 362rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    margin-bottom: 76rpx;

    .name {
      font-size: 34rpx;
      font-weight: 500;
      color: #222222;
      margin-top: 24rpx;
    }

    .info {
      // z-index: 99;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 24rpx;
      font-weight: 400;
      color: rgba(34, 34, 34, 0.9);
      margin-top: 16rpx;
    }
  }
  .menu-title {
    margin: 0 32rpx;
    width: 100%;
    display: flex;
    margin-bottom: 68rpx;

    .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) / 4);
      // height: 200rpx;
      display: flex;
      flex-direction: column;
      align-items: center;
      margin-right: 32rpx;
      margin-bottom: 24rpx;
    }

    & .menu-item:nth-child(4n) {
      margin-right: 0;
      .u-image {
        transform: scale(0.7) translateY(-8rpx);
      }
    }
  }
}
</style>
