<template>
  <view class="page-container">
    <view class="page-header">
      <PageHeader
        title="我的"
        :is-back="false"
        :border-bottom="false"
        :background="headerBackground"
      />
    </view>

    <scroll-view 
      class="page-main" 
      scroll-y
      enable-back-to-top
    >
      <view class="main-content">
        <div class="user-info">
          <div class="avatar">
            <u-avatar
              :src="teacherInfo.headSculptureUrl"
              size="148"
            ></u-avatar>
          </div>
          <div class="info">
            <div class="name">{{ teacherInfo.name }}</div>
            <div class="tag">
              <image class="tag-icon" src="@/static/notes/major-icon.png"></image>
              {{ teacherInfo.collegeName }}
            </div>
            <!-- <div class="tag">
              <image class="tag-icon" src="@/static/notes/major-icon.png"></image>
              {{ teacherInfo.professionalName }}
            </div> -->
          </div>
        </div>

        <div class="banner">
          <img src="@/static/notes/banner.png" alt="banner" />
        </div>

        <div class="menu-list">
          <div class="menu-item" @click="navigateTo('personal-info')">
            <div class="menu-icon">
              <image src="@/static/notes/menu1.png" class="menu-icon-img"></image>
            </div>
            <div class="menu-text">个人信息</div>
            <view class="arrow-icon">
              <u-icon name="arrow-right" color="#999" size="24"></u-icon>
            </view>
          </div>
           <div class="menu-item" @click="navigateTo('working-schedule')">
            <div class="menu-icon">
              <image src="@/static/notes/menu4.png" class="menu-icon-img"></image>
            </div>
            <div class="menu-text">我的排班</div>
            <view class="arrow-icon">
              <u-icon name="arrow-right" color="#999" size="24"></u-icon>
            </view>
          </div>
          <div class="menu-item" @click="navigateTo('change-password')">
            <div class="menu-icon">
              <image src="@/static/notes/menu2.png" class="menu-icon-img"></image>
            </div>
            <div class="menu-text">修改密码</div>
            <view class="arrow-icon">
              <u-icon name="arrow-right" color="#999" size="24"></u-icon>
            </view>
          </div>
          <div class="menu-item" @click="navigateTo('logout-records')">
            <div class="menu-icon">
              <image src="@/static/notes/menu3.png" class="menu-icon-img"></image>
            </div>
            <div class="menu-text">退出登录</div>
            <view class="arrow-icon">
              <u-icon name="arrow-right" color="#999" size="24"></u-icon>
            </view>
          </div>
        </div>
      </view>
    </scroll-view>

    <view class="page-tabbar">
      <TabBar :currentPath="'/pages/my/index'" @change="handleTabChange" />
    </view>
  </view>
</template>

<script>
import TabBar from "@/components/TabBar-optimized.vue";
import PageHeader from "@/components/PageHeader.vue";

export default {
  name: "MyPage",
  components: {
    TabBar,
    PageHeader,
  },
  data() {
    return {
      headerBackground: {
        background: "transparent",
      },
      teacherInfo: {
        name:'',
        collegeName:'',
        professionalName:'',
        headSculptureUrl:'/static/common/images/avatar_default.jpg',
      },
    };
  },
  onLoad() {
    this.getTeacherInfo();
  },
  methods: {
    navigateTo(route) {
      // 导航到指定路由
      if (route === 'personal-info') {
        uni.navigateTo({
          url: '/pages/my/personalInfo'
        });
      } else if (route === 'change-password') {
        uni.navigateTo({
          url: '/pages/my/change-password'
        });
      } else if (route === 'logout-records') {
        uni.showModal({
          title: '提示',
          content: '确定要退出登录吗？',
          success: (res) => {
            if (res.confirm) {
              // 清除用户信息
              // this.$u.vuex('vuex_teacherInfo', {});
              // this.$u.vuex('vuex_token', '');
              this.$store.commit("logout");
              // 跳转到登录页
              uni.reLaunch({
                // 与项目其余位置保持一致的登录页路径
                url: "/pages/login/login/index?type=" + this.vuex_userType,
              });
            }
          }
        });
      } else if (route === 'working-schedule') {
        uni.navigateTo({
          url: '/pages/my/schedule'
        });
      }
    },
    handleTabChange(path, index) {
      console.log("切换到标签页:", path, index);
    },
    getTeacherInfo() {
      const userInfo = this.vuex_user || {};
      console.log("userInfo:", userInfo);
      const userId = userInfo.id || userInfo.Id;
      if (!userId) {
        console.warn("getTeacherInfo 缺少用户Id");
        return;
      }
      this.$u.api
        .getTeacherInfo({ Id: userId })
        .then((res) => {
          const info = (res && res.data && res.data[0]) || {};
          this.teacherInfo = { ...this.teacherInfo, ...info };
          this.$u.vuex("vuex_teacherInfo", this.teacherInfo);
        });
    }
  },
};
</script>

<style scoped>
/* ===== 页面容器 - 主流三段式布局 ===== */
.page-container {
  /* 固定定位，占满整个视口 */
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  
  /* Flex 布局 */
  display: flex;
  flex-direction: column;
  
  /* 背景图片（保留特色） */
  background-image: url("@/static/notes/bg.png");
  background-position: center top;
  background-repeat: no-repeat;
  background-size: 100% auto;
  
  /* 防止溢出 */
  overflow: hidden;
}

/* ===== 头部导航 ===== */
.page-header {
  /* 不收缩，固定高度 */
  flex-shrink: 0;
  
  /* 层级 */
  z-index: 100;
}

/* ===== 内容区域 ===== */
.page-main {
  /* 占据剩余空间 */
  flex: 1;
  
  /* 重要：防止 flex 子元素溢出 */
  height: 0;
  
  /* 允许滚动 */
  overflow-y: auto;
  
  /* iOS 滚动优化 */
  -webkit-overflow-scrolling: touch;
}

/* ===== 内容内层 ===== */
.main-content {
  /* 底部留出 TabBar 空间 + 安全区域 */
  padding-bottom: calc(50px + env(safe-area-inset-bottom));
  
  /* 最小高度（确保可以滚动） */
  min-height: 100%;
}

/* ===== 底部导航 ===== */
.page-tabbar {
  /* 不收缩，固定高度 */
  flex-shrink: 0;
  
  /* 层级 */
  z-index: 100;
}

.user-info {
  display: flex;
  padding: 30rpx;
}

.avatar {
  width: 148rpx;
  height: 148rpx;
  border-radius: 50%;
  overflow: hidden;
  margin-right: 30rpx;
  /* background-color: #ddd; */
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.info .name {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 24rpx;
}

.tag-icon {
  width: 20rpx;
  height: 20rpx;
  margin-right: 16rpx;
}

.tag {
  font-size: 10px;
  font-weight: 500;
  color: #666;
  margin-bottom: 26rpx;
  display: flex;
  align-items: center;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20rpx 0 26rpx 0;
}

.stat-num {
  font-size: 20px;
  font-weight: bold;
  line-height: 40rpx;
  color: #333;
}

.stat-label {
  font-size: 10px;
  color: #666;
  margin-top: 16rpx;
}

.banner {
  padding: 0 26rpx 0 26rpx;
}

.banner img {
  width: 100%;
  border-radius: 8px;
  height: 100%;
  object-fit: cover;
}

.menu-list {
  background-color: #fff;
  border-radius: 8px;
  margin: 54rpx 30rpx 0 30rpx;
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 15px;
}

.menu-icon {
  margin-right: 10px;
  font-size: 20px;
}

.menu-text {
  flex: 1;
  font-size: 16px;
}

.arrow-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
}

.menu-icon-img {
  width: 28rpx;
  height: 28rpx;
}
</style>
