<script setup>
/*----------------------------------依赖引入-----------------------------------*/
import { onLoad, onShow, onHide, onLaunch, onReady, onBackPress } from "@dcloudio/uni-app";
import { defineComponent, getCurrentInstance, inject, nextTick, onMounted, watchEffect, ref } from "vue";
/*----------------------------------接口引入-----------------------------------*/
/*----------------------------------组件引入-----------------------------------*/
/*----------------------------------store引入-----------------------------------*/
/*----------------------------------公共方法引入-----------------------------------*/
import config from "./config";
import { storageSystem } from "@/utils/storage";
/*----------------------------------公共变量-----------------------------------*/
const { proxy } = getCurrentInstance();
/*----------------------------------变量声明-----------------------------------*/
function initApp() {
  proxy.$settingStore.initThemeColor(storageSystem.get("themeColor")); //初始化默认主题
  proxy.$settingStore.systemHeightTop(); //初始化获取安全区高度
  //#ifdef APP-PLUS
  stteingInit();
  //#endif
}

//设置初始化
function stteingInit() {
  plus.screen.unlockOrientation(); //解除屏幕方向的锁定，但是不一定是竖屏；

  // 综合智慧云
  if (config.appInfo.appid === "__UNI__36DE3A0") {
    plus.screen.lockOrientation("portrait-primary"); //设置屏幕方向（1.竖屏正方向：portrait-primary 2.竖屏反方向：portrait-secondary 3.横屏正方向：landscape-primary 4.横屏反方向：landscape-secondary 5.自然方向：default）
  }
  // 智能巡更
  else if (config.appInfo.appid === "__UNI__BF1A1F0") {
    plus.screen.lockOrientation("portrait-primary"); //设置屏幕方向（1.竖屏正方向：portrait-primary 2.竖屏反方向：portrait-secondary 3.横屏正方向：landscape-primary 4.横屏反方向：landscape-secondary 5.自然方向：default）
    proxy.$keyListen.startListen(true); //开启物理按钮监听
  }
  // 智能会议
  else if (config.appInfo.appid === "__UNI__F3963F8") {
    plus.screen.lockOrientation("landscape-primary"); //设置屏幕方向（1.竖屏正方向：portrait-primary 2.竖屏反方向：portrait-secondary 3.横屏正方向：landscape-primary 4.横屏反方向：landscape-secondary 5.自然方向：default）
    plus.navigator.hideSystemNavigation(); //隐藏安卓底部虚拟导航键
  }

  proxy.$settingStore.pushListener(); //开启消息推送监听
}

// 设置手机通知权限
function setPermissions() {
  // #ifdef APP-PLUS
  if (plus.os.name == "Android") {
    // 判断是Android
    var main = plus.android.runtimeMainActivity();
    var pkName = main.getPackageName();
    var uid = main.getApplicationInfo().plusGetAttribute("uid");
    var NotificationManagerCompat = plus.android.importClass("android.support.v4.app.NotificationManagerCompat");
    //android.support.v4升级为androidx
    if (NotificationManagerCompat == null) {
      NotificationManagerCompat = plus.android.importClass("androidx.core.app.NotificationManagerCompat");
    }
    var areNotificationsEnabled = NotificationManagerCompat.from(main).areNotificationsEnabled();
    // 未开通‘允许通知’权限，则弹窗提醒开通，并点击确认后，跳转到系统设置页面进行设置
    if (!areNotificationsEnabled) {
      uni.showModal({
        title: "通知权限开启提醒",
        content: "您还没有开启通知权限，无法接受到消息通知，请前往设置！",
        showCancel: false,
        confirmText: "去设置",
        success: function (res) {
          if (res.confirm) {
            var Intent = plus.android.importClass("android.content.Intent");
            var Build = plus.android.importClass("android.os.Build");
            //android 8.0引导
            if (Build.VERSION.SDK_INT >= 26) {
              var intent = new Intent("android.settings.APP_NOTIFICATION_SETTINGS");
              intent.putExtra("android.provider.extra.APP_PACKAGE", pkName);
            } else if (Build.VERSION.SDK_INT >= 21) {
              //android 5.0-7.0
              var intent = new Intent("android.settings.APP_NOTIFICATION_SETTINGS");
              intent.putExtra("app_package", pkName);
              intent.putExtra("app_uid", uid);
            } else {
              //(<21)其他--跳转到该应用管理的详情页
              intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
              var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
              intent.setData(uri);
            }
            // 跳转到该应用的系统通知设置页
            main.startActivity(intent);
          }
        },
      });
    }
  } else if (plus.os.name == "iOS") {
    // 判断是ISO
    var isOn = undefined;
    var types = 0;
    var app = plus.ios.invoke("UIApplication", "sharedApplication");
    var settings = plus.ios.invoke(app, "currentUserNotificationSettings");
    if (settings) {
      types = settings.plusGetAttribute("types");
      plus.ios.deleteObject(settings);
    } else {
      types = plus.ios.invoke(app, "enabledRemoteNotificationTypes");
    }
    plus.ios.deleteObject(app);
    isOn = 0 != types;
    if (isOn == false) {
      uni.showModal({
        title: "通知权限开启提醒",
        content: "您还没有开启通知权限，无法接受到消息通知，请前往设置！",
        showCancel: false,
        confirmText: "去设置",
        success: function (res) {
          if (res.confirm) {
            var app = plus.ios.invoke("UIApplication", "sharedApplication");
            var setting = plus.ios.invoke("NSURL", "URLWithString:", "app-settings:");
            plus.ios.invoke(app, "openURL:", setting);
            plus.ios.deleteObject(setting);
            plus.ios.deleteObject(app);
          }
        },
      });
    }
  }
  // #endif
}

watchEffect(() => {
  //#ifdef APP-PLUS || MP-WEIXIN
  if (uni.getStorageSync("serveUrl")) {
    config.baseUrl = "https://" + uni.getStorageSync("serveUrl") + "/prod-api";
  }
  //#endif
});

onLaunch(() => {
  console.log("App Launch");
  initApp();
});

onReady(() => {});

onShow(() => {
  console.log("App Show");
});

onHide(() => {
  console.log("App Hide");
});
</script>

<style lang="scss">
@import "@/static/scss/index.scss";
</style>

<style lang="scss">
@import "@/uni_modules/uview-plus/index.scss";

uni-page-body,
uni-page-refresh {
  height: 100%;
}

//默认返回按钮样式
:deep(.uni-page-head-btn) {
  background-color: rgba(0, 0, 0, 0) !important;
}

//默认头部导航title样式
:deep(.uni-page-head__title) {
  font-size: $uni-font-size-lg;
  font-weight: bold;
}
</style>
