<script>
import dbStore from "@/store/db";
import accountStore from "@/store/account";
import { globalUnreadListenerManager } from "@/utils/global-unread-listener.js";

export default {
  async onLaunch() {
    console.log("App Launch: ");

    // 获取 openId 并初始化 IM
    await this.initIMOnLaunch();
  },
  onShow: function () {
    const db = dbStore();
    if (db && typeof db.getJobs === "function") {
      db.getJobs();
    }
  },
  onHide: function () {
    console.log("App Hide");
  },
  methods: {
    async initIMOnLaunch() {
      try {
        const account = accountStore();
        // 检查是否已有账户信息（包含 openId）
        const storedAccount = uni.getStorageSync("account");
        const storedOpenId = uni.getStorageSync("openid");
        if (storedOpenId) {
          console.log("检测到已登录的 openId，开始初始化 IM:", storedOpenId);
          account.openid = storedOpenId;

          // 如果有完整的账户信息，也恢复
          if (storedAccount) {
            account.account = storedAccount;
          }

          // 初始化 IM
          const success = await account.initIMAfterLogin();
          if (success) {
            console.log("IM 初始化成功");
            // IM 初始化成功后，设置全局未读消息监听
            // globalUnreadListenerManager.setup();
          } else {
            console.warn("IM 初始化失败");
          }
        } else {
          console.log("未检测到 openId，跳过 IM 初始化");
        }
      } catch (error) {
        console.error("App Launch 初始化 IM 失败:", error);
      }
    },
  },
};
</script>

<style lang="scss">
$primary-color: #0074ff;

page {
  height: 100%;
  background: #f5f5f5;
}

.shadow-up {
  box-shadow: rgba(0, 0, 0, 0) 0px 0px 0px 0px, rgba(0, 0, 0, 0) 0px 0px 0px 0px,
    rgba(0, 0, 0, 0.1) 0px -10px 15px -3px, rgba(0, 0, 0, 0.1) 0px -4px 6px -4px;
}

.shadow-lg {
  box-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
}

.relative {
  position: relative;
}

.absolute {
  position: absolute;
}

.inset-0 {
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
}

.inline-block {
  display: inline-block;
}

.block {
  display: block;
}

.flex {
  display: flex;
}

.flex-col {
  flex-direction: column;
}

.flex-wrap {
  flex-wrap: wrap;
}

.flex-grow {
  flex-grow: 1;
}

.flex-shrink-0 {
  flex-shrink: 0;
}

.items-center {
  align-items: center;
}

.justify-center {
  justify-content: center;
}

.justify-between {
  justify-content: space-between;
}

.justify-around {
  justify-content: space-around;
}

.justify-end {
  justify-content: flex-end;
}

.bg-gray {
  background: #f5f5f5;
}

.bg-primary {
  background: $primary-color;
}

.bg-success {
  background: green;
}

.bg-white {
  background: #fff;
}

.bg-warning {
  background: orange;
}

.bg-danger {
  background: rgb(248 113 113);
}

.py-5 {
  padding-top: 10rpx;
  padding-bottom: 10rpx;
}

.p-15 {
  padding: 30rpx;
}

.p-10 {
  padding: 20rpx;
}

.px-5 {
  padding-left: 10rpx;
  padding-right: 10rpx;
}

.px-10 {
  padding-left: 20rpx;
  padding-right: 20rpx;
}

.px-12 {
  padding-left: 24rpx;
  padding-right: 24rpx;
}

.px-15 {
  padding-left: 30rpx;
  padding-right: 30rpx;
}

.pt-5 {
  padding-top: 10rpx;
}

.pt-15 {
  padding-top: 30rpx;
}

.break-all {
  word-break: break-all;
}

.pb-5 {
  padding-bottom: 10rpx;
}

.pb-10 {
  padding-bottom: 20rpx;
}

.py-10 {
  padding-top: 20rpx;
  padding-bottom: 20rpx;
}

.py-12 {
  padding-top: 24rpx;
  padding-bottom: 24rpx;
}

.py-15 {
  padding-top: 30rpx;
  padding-bottom: 30rpx;
}

.mr-5 {
  margin-right: 10rpx;
}

.mr-10 {
  margin-right: 20rpx;
}

.ml-10 {
  margin-left: 30rpx;
}

.ml-15 {
  margin-left: 30rpx;
}

.mb-5 {
  margin-bottom: 10rpx;
}

.mb-10 {
  margin-bottom: 20rpx;
}

.mt-10 {
  margin-top: 20rpx;
}

.mt-15 {
  margin-top: 30rpx;
}

.mt-12 {
  margin-top: 24rpx;
}

.mx-10 {
  margin-left: 20rpx;
  margin-right: 20rpx;
}

.mx-auto {
  margin-left: auto;
  margin-right: auto;
}

.rounded {
  border-radius: 16rpx;
}

.rounded-sm {
  border-radius: 8rpx;
}

.rounded-full {
  border-radius: 999px;
}

.text-center {
  text-align: center;
}

.text-right {
  text-align: right;
}

.text-gray {
  color: #999;
}

.text-black {
  color: #000;
}

.text-dark {
  color: #333;
}

.text-danger {
  color: #f56c6c;
}

.text-primary {
  color: $primary-color;
}

.text-success {
  color: #67c23a;
}

.text-white {
  color: #fff;
}

.text-warning {
  color: #f56c6c;
}

.text-sm {
  font-size: 24rpx;
}

.text-base {
  font-size: 28rpx;
}

.text-lg {
  font-size: 32rpx;
}

.text-xl {
  font-size: 36rpx;
}

.leading-normal {
  line-height: 1.5;
}

.border {
  border: 1px solid #eee;
}

.border-dashed {
  border: 1px dashed #eee;
}

.border-b {
  border-bottom: 1px solid #eee;
}

.border-auto {
  border: 1px solid;
}

.border-dashed-auto {
  border: 1px dashed;
}

.border-primary {
  border: 1px solid $primary-color;
}

.font-semibold {
  font-weight: bold;
}

.truncate {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.w-0 {
  width: 0;
}

.w-full {
  width: 100%;
}

.overflow-hidden {
  overflow: hidden;
}

.border-box {
  box-sizing: border-box;
}

.line-clamp-2 {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}

.line-clamp-3 {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
}

.h-full {
  height: 100%;
}

.safe-bottom-padding {
  height: env(safe-area-inset-bottom);
}
</style>
