<template>
  <view class="mine-container">
    <NavBar :title="pageTitle" />
    <view class="header-section">
      <view class="flex padding justify-between">
        <view class="flex align-center">
          <view v-if="!avatar" class="cu-avatar xl round bg-white">
            <view class="iconfont icon-people text-gray icon"></view>
          </view>
          <image v-if="avatar" @click="handleToAvatar" :src="avatar" class="cu-avatar xl round" mode="widthFix">
          </image>
          <view v-if="!name" @click="handleToLogin" class="login-tip">
            {{ t('mine.clickLogin') }}
          </view>
          <view v-if="name" @click="handleToInfo" class="user-info">
            <view class="u_title">
              {{ translateWithParams('mine.username', { name }) }}
            </view>
          </view>
        </view>
        <view @click="handleToInfo" class="flex align-center">
          <text>{{ t('mine.profile') }}</text>
          <view class="iconfont icon-right"></view>
        </view>
      </view>
    </view>

    <view class="content-section">
      <view class="menu-list">
        <view class="list-cell list-cell-arrow" @click="handleToEditInfo">
          <view class="menu-item-box">
            <view class="iconfont icon-user menu-icon"></view>
            <view>{{ t('mine.editProfile') }}</view>
          </view>
        </view>
        <!-- <view class="list-cell list-cell-arrow" @click="handleHelp">
          <view class="menu-item-box">
            <view class="iconfont icon-help menu-icon"></view>
            <view>{{ t('mine.faq') }}</view>
          </view>
        </view>
        <view class="list-cell list-cell-arrow" @click="handleAbout">
          <view class="menu-item-box">
            <view class="iconfont icon-aixin menu-icon"></view>
            <view>{{ t('mine.about') }}</view>
          </view>
        </view> -->
        <view class="list-cell list-cell-arrow" @click="handleToSetting">
          <view class="menu-item-box">
            <view class="iconfont icon-setting menu-icon"></view>
            <view>{{ t('mine.appSettings') }}</view>
          </view>
        </view>
      </view>
    </view>

    <view>
      <uni-popup ref="popup" type="dialog">
        <uni-popup-dialog type="info" :cancelText="t('common.close')" :confirmText="t('common.exit')" :title="t('common.notice')" :content="t('common.confirmLogout')"
                          @confirm="dialogConfirm" @close="dialogClose">
        </uni-popup-dialog>
      </uni-popup>
    </view>
    <TabBar />
  </view>
</template>

<script setup>
import { computed, onUnmounted, ref } from "vue";
import { useI18n } from 'vue-i18n'
import useUserStore from '@/store/modules/user'
import { onLocaleChange, offLocaleChange, translateWithParams } from '@/locales'
import NavBar from '@/components/common/NavBar.vue'
import TabBar from '@/components/common/TabBar.vue'

const userStore = useUserStore()
const name = userStore.name;
const { t } = useI18n()

const pageTitle = computed(() => t('nav.mine'))
const avatar = ref(userStore.avatar);
const popup = ref(null);

uni.$on('refresh', () => {
  avatar.value = userStore.avatar;
})

function handleToInfo() {
  uni.navigateTo({
    url: '/pages_mine/pages/info/index'
  });
};
function handleToEditInfo() {
  uni.navigateTo({
    url: '/pages_mine/pages/info/edit'
  });
};
function handleToSetting() {
  uni.navigateTo({
    url: '/pages_mine/pages/setting/index'
  });
};
function handleToLogin() {
  uni.reLaunch({
    url: '/pages/login'
  });
};
function handleToAvatar() {
  uni.navigateTo({
    url: '/pages_mine/pages/avatar/index'
  });
};
function handleLogout() {
  popup.value.open();
};
function dialogConfirm() {
  userStore.logOut().then(() => {
    uni.reLaunch({
      url: '/pages/login'
    });
  })
};
function dialogClose() {
};
function handleToPwd() {
  uni.navigateTo({
    url: '/pages_mine/pages/pwd/index'
  });
}
function handleHelp() {
  uni.navigateTo({
    url: '/pages_mine/pages/help/index'
  });
}

function handleAbout() {
  uni.navigateTo({
    url: '/pages_mine/pages/about/index'
  });
}
function handleBuilding() {
  uni.showToast({
    title: t('common.moduleBuilding'),
    mask: false,
    icon: "none",
    duration: 1000
  });
}
</script>

<style lang="scss">
page {
  background-color: #f5f6f7;
}

.mine-container {
  width: 100%;
  min-height: 100vh;

  .header-section {
    padding: 15px;
    background: linear-gradient(135deg, #1a3a5c 0%, #2d5a87 100%);
    color: white;

    .login-tip {
      font-size: 18px;
      margin-left: 10px;
    }

    .cu-avatar {
      border: 2px solid #eaeaea;

      .icon {
        font-size: 40px;
      }
    }

    .user-info {
      margin-left: 15px;

      .u_title {
        font-size: 18px;
        line-height: 30px;
      }
    }
  }

  .content-section {
    // position: relative;
    // top: -50px;

    .menu-list{
        margin: 0;
    }

    .mine-actions {
    //   margin: 15px 15px;
      padding: 20px 0px;
      border-radius: 8px;
      background-color: white;

      .action-item {
        .icon {
          font-size: 28px;
        }

        .text {
          display: block;
          font-size: 13px;
          margin: 8px 0px;
        }
      }
    }
  }
}
</style>

<style lang="scss">
page {
  background-color: #f5f6f7;
}

.mine-container {
  width: 100%;
  min-height: 100vh;

  .header-section {
    padding: 15px;
    background: linear-gradient(135deg, #1a3a5c 0%, #2d5a87 100%);
    color: white;

    .login-tip {
      font-size: 18px;
      margin-left: 10px;
    }

    .cu-avatar {
      border: 2px solid #eaeaea;

      .icon {
        font-size: 40px;
      }
    }

    .user-info {
      margin-left: 15px;

      .u_title {
        font-size: 18px;
        line-height: 30px;
      }
    }
  }
}
</style>