<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"
        :src="baseUrl + '/' + globalStore.infoData?.FIMAGEFILESERVER"
        border-radius="24"
      />
      <text class="name">{{ globalStore.infoData?.FNAME }}</text>
      <!-- <view v-if="!globalStore.infoData.FMOBILE" class="info">
        <text @tap="toLogin" style="color: #2979ff; font-weight: bolder"
          >登录</text
        >
      </view> -->
      <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)"
      >
        <!-- Ribbon2 Ribbon5 -->
        <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 } from "@/api/login";
import { baseUrl } from "@/utils/request";
import { useGlobalStore } from "@/store/useStore";

const globalStore = useGlobalStore();
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: "我的班级",
  },
];

const toTrainRegistration = () => {
  if (!globalStore.infoData.FMOBILE)
    return uni.navigateTo({
      url: "/pages/login/index",
    });
  uni.navigateTo({
    url: "/pages/trainRegistration/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.navigateTo({
    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;
    default:
      toClass();
      break;
  }
};

onMounted(async () => {
  // 获取微信公众号跳转h5获取code
  code.value = getUrlCode().code;
  // alert("获取的code值：" + code.value);
  if (code.value) {
    const data = {
      code: code.value,
      type: 1,
    };
    try {
      const { data: res } = await login(data);
      if (res.code == 200) {
        if (res.msg.split(":")[1]) {
          globalStore.setInfoData(res.data[0]);
          globalStore.setOpenId(res.msg.split(":")[1]);
        }
      } else if (res.code == 501 && !globalStore.infoData?.FSTUDENTID) {
        // 新用户，跳转到注册页面
        globalStore.setOpenId(res.data.split(":")[1]);
        uni.navigateTo({
          url: "/pages/login/register",
        });
      }
    } catch (error) {
      console.error("登录失败", error);
    }
  } else {
    // 如果无code跳转到登录页面
    if (!globalStore.openId && !globalStore.infoData.FMOBILE)
      uni.navigateTo({
        url: "/pages/login/index",
      });
  }
});
</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: #ffffff;
    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>
