<script setup lang="ts">
import { cssAssetUrl } from '@/commons/assets';
import { $save, $toast } from '@/commons/utils.ts';
import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const username = ref('')
const password = ref('')

const imageUrls = {
  bg1: cssAssetUrl('main-bg1.png'),
  logo: cssAssetUrl('game1/logo.png'),
  title1: cssAssetUrl('main-title.png'),
}

async function login() {
  let domain = localStorage.getItem('domain') || '';
  const res = await fetch(`${domain}/manager/login`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({ username: username.value, password: password.value }),
  })

  const data = await res.json()
  if (!res.ok || data.state !== 1) {
    $toast(data.msg)
    //alert(data.msg || 'Login failed')
    // throw new Error(data.msg || 'Login failed')
  }
  return data
}

const loginHandler = async () => {
  const { state, msg, data } = await login()

  if (state === 0) {
    // alert(msg)
    $toast(msg)
    return
  }
  for (let i in data) {
    $save(i, data[i]);
  }
  // alert('登录成功')
  $toast('登录成功')
  await router.replace(`/main`);
}

onMounted(() => {
})
</script>

<template>
  <div class="bg">
    <div class="img-logo"></div>
    <div class="img-title1"></div>
    <div class="login-page">
      <form class="login-panel" @submit.prevent="loginHandler">
        <div class="panel-header">
          <h1>管理登录</h1>
        </div>

        <label class="field">
          <span>用户名</span>
          <input v-model="username" type="text" autocomplete="username" placeholder="请输入用户名" />
        </label>

        <label class="field">
          <span>密码</span>
          <input v-model="password" type="password" autocomplete="current-password" placeholder="请输入密码" />
        </label>

        <button class="login-button" type="submit">登录</button>
      </form>
    </div>
  </div>
</template>

<style scoped>
.bg {
  width: 1920px;
  height: 1080px;
  background: v-bind('imageUrls.bg1') center / cover no-repeat;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;

  .img-logo {
    background: v-bind('imageUrls.logo');
    background-size: cover;
    width: 428px;
    height: 77px;
    position: absolute;
    left: 37px;
    top: 82px;
  }

  .img-title1 {
    background: v-bind('imageUrls.title1') center / cover no-repeat;
    width: 1024px;
    height: 143px;
    margin: 0 auto;
    position: relative;
    top: 50px;
  }
}

.login-page {
  position: absolute;
  z-index: 1;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.login-panel {
  box-sizing: border-box;
  width: 420px;
  padding: 42px 44px 38px;
  position: relative;
  z-index: 1;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 16px;
  background: rgba(0, 0, 0, 0.42);
  backdrop-filter: blur(8px);
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.34);
  color: #fff;
}

.panel-header {
  margin-bottom: 34px;
  text-align: center;
}


h1 {
  margin: 0;
  font-size: 34px;
  line-height: 1.2;
  font-weight: 800;
  text-shadow: 0 2px 12px rgba(0, 0, 0, 0.28);
}

.field {
  display: block;
  margin-bottom: 20px;
}

.field span {
  display: block;
  margin-bottom: 8px;
  color: rgba(255, 255, 255, 0.88);
  font-size: 15px;
  font-weight: 700;
}

.field input {
  box-sizing: border-box;
  width: 100%;
  height: 52px;
  padding: 0 16px;
  border: 1px solid rgba(255, 255, 255, 0.34);
  border-radius: 12px;
  outline: none;
  background: rgba(255, 255, 255, 0.84);
  color: #17304f;
  font-size: 16px;
  font-weight: 600;
  transition: border-color 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
}

.field input::placeholder {
  color: rgba(23, 48, 79, 0.48);
}

.field input:focus {
  border-color: rgba(255, 223, 158, 0.95);
  background: rgba(255, 255, 255, 0.96);
  box-shadow: 0 0 0 4px rgba(255, 223, 158, 0.22);
}

.login-button {
  width: 100%;
  height: 54px;
  margin-top: 10px;
  border: 0;
  border-radius: 12px;
  cursor: pointer;
  background: #ffdf9e;
  color: #7d1f00;
  font-size: 18px;
  font-weight: 800;
  box-shadow: 0 12px 28px rgba(125, 31, 0, 0.24);
  transition: transform 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
}

.login-button:hover {
  transform: translateY(-2px);
  background: #ffe8b8;
  box-shadow: 0 16px 34px rgba(125, 31, 0, 0.3);
}

.login-button:active {
  transform: translateY(0);
}

@media (max-width: 520px) {
  .login-panel {
    width: calc(100% - 32px);
    padding: 34px 24px 30px;
  }

  h1 {
    font-size: 30px;
  }
}
</style>
