<template>
  <div>
    <q-layout view="hHh LpR fFf" :style="{ height: Screen.height, width: Screen.width }">
      <!-- 头部内容 reveal -->
      <q-header elevated class="bg-primary text-white">
        <q-toolbar class="glossy main-headers text-white rounded-borders">
          <!-- icon="arrow_left" -->
          <q-btn glossy push rounded class="text-weight-bolder" color="white" text-color="black"
            icon="img:statics/icons/back.png" label="返回" @click="router.go(-1)" v-show="zebraStore.showBackBtnFlag" />
          <!-- <q-toolbar-title>Quasar Framework</q-toolbar-title> -->
          <q-space />
          <template v-if="authin === '1'">
            <transition appear enter-active-class="animated zoomIn">
              <q-btn-dropdown stretch flat color="white-8" icon="account_circle" size="lg" @click="chat = false">
                <div class="row no-wrap q-pa-md">
                  <div class="column" style="width: 150px">
                    <div class="q-mb-md" style="
                      width: 100%;
                      white-space: nowrap;
                      overflow: hidden;
                      text-overflow: ellipsis;
                    ">
                      <span style="margin-left: 9%; font-weight: bold">用户名:</span>
                      <span style="margin-left: 6%; font-weight: bold">{{
                        login_name
                      }}</span>
                    </div>
                    <q-separator />
                    <q-btn glossy push rounded icon="img:statics/icons/checkUser.png" color="primary"
                      class="full-width q-mt-md text-weight-bolder" align="left" label="切换用户" @click="login = true">
                      <q-tooltip content-class="bg-amber text-black shadow-4" :offset="[10, 10]"
                        content-style="font-size: 12px">切换用户</q-tooltip>
                    </q-btn>
                    <q-btn glossy push rounded icon="img:statics/icons/logout4.png" color="negative"
                      class="full-width q-mt-md text-weight-bolder" align="left" label="退出登录" @click="Logout()">
                      <q-tooltip content-class="bg-amber text-black shadow-4" :offset="[10, 10]"
                        content-style="font-size: 12px">退出登录</q-tooltip>
                    </q-btn>
                  </div>
                </div>
              </q-btn-dropdown>
            </transition>
          </template>
          <template v-if="authin === '0'">
            <transition appear enter-active-class="animated zoomIn">
              <q-btn glossy push rounded label="登录" class="text-weight-bolder" icon="login" color="white"
                text-color="black" @click="login = true" style="margin-left: 10px">
                <q-tooltip content-class="bg-amber text-black shadow-4" :offset="[15, 15]"
                  content-style="font-size: 12px">登录</q-tooltip>
              </q-btn>
            </transition>
          </template>
        </q-toolbar>
      </q-header>

      <!-- <q-footer elevated>
        <q-toolbar class="glossy">
          <q-toolbar-title>Footer</q-toolbar-title>
        </q-toolbar>
      </q-footer> -->

      <!-- 身体内容 class="main-page" -->
      <q-page-container :style="{
        height: container_height,
        width: Screen.width,
      }">
        <router-view />
      </q-page-container>

      <!-- 登录模态窗 -->
      <q-dialog v-model="login" transition-show="jump-down" transition-hide="jump-up">
        <q-card style="min-width: 350px">
          <q-bar class="bg-light-blue-10 text-white rounded-borders" style="height: 50px">
            登录
            <q-space />
            <q-btn flat round icon="close" v-close-popup>
              <q-tooltip content-class="bg-amber text-black shadow-4" :offset="[20, 20]"
                content-style="font-size: 12px">{{
                  $t('index.close') }}</q-tooltip>
            </q-btn>
          </q-bar>
          <q-card-section class="q-mt-xs">
            <div>
              <q-input outlined rounded standout label="用户名" class="text-h6" v-model="adminlogin.username"
                @keyup.enter="Login()">
                <template v-slot:prepend>
                  <q-icon name="account_circle" />
                </template>
              </q-input>
            </div>
            <div class="q-mt-md">
              <q-input outlined rounded standout label="密码" class="text-h6" :type="isPwd ? 'password' : 'text'"
                v-model="adminlogin.password" @keyup.enter="Login()">
                <template v-slot:prepend>
                  <q-icon name="lock" />
                </template>
                <template v-slot:append>
                  <q-icon :name="isPwd ? 'visibility_off' : 'visibility'" class="cursor-pointer"
                    @click="isPwd = !isPwd" />
                </template>
              </q-input>
            </div>
          </q-card-section>
          <q-card-actions align="center" class="q-mb-md">
            <q-btn glossy push rounded unelevated color="primary" label="登录"
              style="font-size: 16px; margin: 0 8px; width: 100%" @click="Login()" />
          </q-card-actions>
        </q-card>
      </q-dialog>

    </q-layout>
  </div>
</template>
<script setup lang="ts">
import { loginApi } from 'src/api/login/loginApi';
import { Screen, LocalStorage, Notify } from 'quasar';
import { onMounted, ref } from 'vue';
import { useRouter } from 'vue-router';
import { useZebraStore } from '../stores/zebra-store'

const router = useRouter();
const zebraStore = useZebraStore()
const container_height = ref(Screen.height + '' + 'px');
const adminlogin = ref({
  username: 'lhr',
  password: '123456',
});
const chat = ref(false);
const isPwd = ref(true);
const authin = ref('0');
const login = ref(false);
const login_name = ref(LocalStorage.getItem('userName'));

const Login = () => {
  if (!adminlogin.value.username) {
    Notify.create({
      message: '请输入用户名',
      color: 'negative',
      icon: 'close',
    });
  } else {
    if (!adminlogin.value.password) {
      Notify.create({
        message: '请输入密码',
        color: 'negative',
        icon: 'close',
      });
    } else {
      loginApi
        .login(adminlogin.value)
        .then((res: any) => {
          LocalStorage.set('token', res.tokenValue);
          LocalStorage.set('userName', res.userName);
          login_name.value = res.userName;
          login.value = false; // 关闭登录模态窗
          authin.value = '1';
          Notify.create({
            message: '登录成功',
            icon: 'check',
            color: 'green',
          });
          router.push({ name: 'zebra' }); // 跳转至首页
        })
        .catch((err) => {
          Notify.create({
            message: err,
            icon: 'close',
            color: 'negative',
          });
        });
    }
  }
};

const isLoggedIn = () => {
  // 判断是否存在 token 否则自动打开登录模态窗
  if (!LocalStorage.getItem('token')) {
    login.value = true; // 打开登录模态窗
  }
};

const Logout = () => {
  authin.value = '0';
  login_name.value = '';
  LocalStorage.remove('token');
  // LocalStorage.remove('userName');
  Notify.create({
    message: '退出登录',
    icon: 'check',
    color: 'positive',
  });
  router.push({ name: 'scannerzebra' }); // 跳转至手机端登录页
  isLoggedIn();
};

onMounted(() => {
  if (LocalStorage.has('token')) {
    router.push({ name: 'zebra' }); // 跳转至首页
  }
  console.log('触发onMounted')
});
const created = () => {
  if (LocalStorage.has('token')) {
    authin.value = '1'; // 显示已登录状态的控件
  } else {
    authin.value = '0';
    isLoggedIn();
  }
};
created();


</script>
<style scoped>
.main-page {
  background: #fff;
}

.tabs .q-tab__indicator {
  width: 25%;
  height: 1.5px;
  margin: auto;
}

.tabs .absolute-bottom {
  bottom: 8px;
}
</style>
