<script setup lang="ts">
import { ref } from 'vue';
import { useLockScreenStore } from '@/stores/modules/lockScreen';
import { useUserStore } from '@/stores';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';

const lockStore = useLockScreenStore();
const userStore = useUserStore();
const router = useRouter();

const password = ref('');
const errorMsg = ref('');
const loading = ref(false);

const nickName = computed(() => userStore.userInfo?.nickName || userStore.userInfo?.username || '用户');

async function handleUnlock() {
  if (!password.value) {
    errorMsg.value = '请输入密码';
    return;
  }
  loading.value = true;
  errorMsg.value = '';
  try {
    const ok = await lockStore.unlock(password.value);
    if (ok) {
      ElMessage.success('解锁成功');
      password.value = '';
    } else {
      errorMsg.value = '密码错误，请重试';
    }
  } catch {
    errorMsg.value = '网络异常，请重试';
  } finally {
    loading.value = false;
  }
}

function handleLogout() {
  lockStore.unlockDirect();
  userStore.logout();
  router.replace('/login');
}

function onKeyup(e: KeyboardEvent) {
  if (e.key === 'Enter' && !loading.value) {
    handleUnlock();
  }
}
</script>

<template>
  <div class="lock-screen-overlay" @keyup="onKeyup">
    <div class="lock-container">
      <!-- 左侧装饰区 -->
      <div class="left-section">
        <div class="logo-wrap">
          <svg width="40" height="40" viewBox="0 0 28 28" fill="none">
            <rect width="28" height="28" rx="8" fill="#1d5af3"/>
            <path d="M8 14L12 10L16 14L20 10" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            <path d="M8 18L12 14L16 18L20 14" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" opacity="0.5"/>
          </svg>
          <span class="logo-text">HZHub 企业员工门户</span>
        </div>
        <div class="user-icon-area">
          <svg width="120" height="120" viewBox="0 0 120 120" fill="none">
            <circle cx="60" cy="45" r="28" fill="#1d5af3" opacity="0.9"/>
            <ellipse cx="60" cy="95" rx="42" ry="30" fill="#1d5af3" opacity="0.9"/>
          </svg>
        </div>
      </div>

      <!-- 右侧解锁区域 -->
      <div class="right-section">
        <div class="lock-card">
          <div class="lock-title">屏幕已锁定</div>
          <div class="lock-user">{{ nickName }}</div>
          <div class="lock-time" v-if="lockStore.lockTime">锁定于 {{ lockStore.lockTime }}</div>

          <div class="lock-form">
            <div class="input-wrapper">
              <svg class="lock-icon" viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="#a8a49c" stroke-width="2">
                <rect x="3" y="11" width="18" height="11" rx="2" ry="2"/>
                <path d="M7 11V7a5 5 0 0 1 10 0v4"/>
              </svg>
              <input
                v-model="password"
                type="password"
                placeholder="请输入登录密码解锁"
                class="lock-input"
                autofocus
                @keyup.enter="handleUnlock"
              />
            </div>

            <div v-if="errorMsg" class="lock-error">{{ errorMsg }}</div>

            <button class="unlock-btn" :disabled="loading" @click="handleUnlock">
              <span v-if="!loading">解锁</span>
              <span v-else>解锁中...</span>
            </button>
          </div>

          <div class="lock-footer">
            <span class="footer-text">或</span>
            <button class="logout-link" @click="handleLogout">退出登录</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.lock-screen-overlay {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f5f3f0;
}

.lock-container {
  display: flex;
  width: 738px;
  max-width: 90%;
  height: 416px;
  overflow: hidden;
  background-color: #fff;
  border-radius: 24px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.12);
}

.left-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 50%;
  padding: 32px;
  background: linear-gradient(233deg, rgba(113, 161, 255, 0.6) 17.67%, rgba(154, 219, 255, 0.6) 70.4%);

  .logo-wrap {
    display: flex;
    gap: 12px;
    align-items: center;
    margin-bottom: 32px;
  }

  .logo-text {
    font-size: 18px;
    font-weight: 700;
    color: #1a1a2e;
  }

  .user-icon-area {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 200px;
  }
}

.right-section {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 50%;
  padding: 32px;
}

.lock-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  max-width: 320px;
}

.lock-title {
  font-size: 20px;
  font-weight: 700;
  color: var(--color-text-primary);
  margin-bottom: 16px;
}

.lock-user {
  font-size: 16px;
  font-weight: 600;
  color: #1d5af3;
  margin-bottom: 4px;
}

.lock-time {
  font-size: 13px;
  color: var(--color-text-secondary);
  margin-bottom: 24px;
}

.lock-form {
  width: 100%;
}

.input-wrapper {
  display: flex;
  align-items: center;
  padding: 0 16px;
  height: 40px;
  background: #f5f3f0;
  border-radius: 10px;
  border: 1px solid transparent;
  transition: all 0.2s;

  &:focus-within {
    background: #fff;
    border-color: #1d5af3;
    box-shadow: 0 0 0 3px rgba(29, 90, 243, 0.08);
  }
}

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

.lock-input {
  flex: 1;
  height: 100%;
  padding: 0 12px;
  background: transparent;
  border: none;
  outline: none;
  color: var(--color-text-primary);
  font-size: 14px;

  &::placeholder {
    color: #a8a49c;
  }
}

.lock-error {
  margin-top: 8px;
  font-size: 13px;
  color: #ef4444;
  text-align: center;
}

.unlock-btn {
  width: 100%;
  height: 40px;
  margin-top: 16px;
  border: none;
  border-radius: 10px;
  background: linear-gradient(135deg, #1d5af3, #858bff);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;

  &:hover:not(:disabled) {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(29, 90, 243, 0.3);
  }

  &:disabled {
    opacity: 0.6;
    cursor: not-allowed;
  }
}

.lock-footer {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-top: 24px;
}

.footer-text {
  font-size: 13px;
  color: var(--color-text-secondary);
}

.logout-link {
  padding: 0;
  background: transparent;
  border: none;
  color: #1d5af3;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.2s;

  &:hover {
    text-decoration: underline;
  }
}

@media (width <= 800px) {
  .left-section {
    display: none !important;
  }

  .lock-container {
    height: auto;
    min-height: 400px;
  }

  .right-section {
    width: 100%;
  }
}
</style>
