<template>
  <div class="login-page">
    <!-- 系统顶端信息栏 -->
    <div class="header">
      <div class="header-content">
        <span class="system-icon">🏥</span>
        <span class="system-name">智慧病房管理平台</span>
      </div>
    </div>

    <!-- 登录区域 -->
    <div class="login-wrapper">
      <div class="login-container">
        <h2 class="login-title">智慧病房管理平台</h2>
        
        <!-- 用户类型切换 -->
        <div class="login-tabs">
          <div 
            class="tab-item" 
            :class="{ active: loginType === 'user' }"
            @click="loginType = 'user'"
          >
            <span class="tab-icon">👤</span>
            用户登录
          </div>
          <div 
            class="tab-item" 
            :class="{ active: loginType === 'admin' }"
            @click="loginType = 'admin'"
          >
            <span class="tab-icon">👨‍💼</span>
            管理员登录
          </div>
        </div>

        <!-- 登录表单 -->
        <form @submit.prevent="handleLogin">
          <div class="form-item">
            <label>{{ loginType === 'admin' ? '管理员账号' : '用户名' }}</label>
            <input 
              v-model="form.username" 
              type="text" 
              :placeholder="loginType === 'admin' ? '请输入管理员账号' : '请输入用户名'" 
            />
          </div>
          <div class="form-item">
            <label>密码</label>
            <input 
              v-model="form.password" 
              type="password" 
              placeholder="请输入密码" 
            />
          </div>
          <button type="submit" class="login-btn">登 录</button>
        </form>
      </div>
    </div>

    <!-- 底部版权信息 -->
    <div class="footer">
      <p>© 2025 智慧病房管理平台 All Rights Reserved</p>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import request from '@/utils/request'

const router = useRouter()

// 登录类型：user-用户登录，admin-管理员登录
const loginType = ref('user')
const loading = ref(false)

const form = reactive({
  username: '',
  password: ''
})

const handleLogin = async () => {
  if (!form.username || !form.password) {
    alert('请输入用户名和密码')
    return
  }
  
  loading.value = true
  try {
    // 根据登录类型调用不同接口
    const url = loginType.value === 'admin' ? '/api/login/admin' : '/api/login/user'
    const res = await request.post(url, {
      username: form.username,
      password: form.password
    })
    
    if (res.code === 200) {
      // 保存登录信息
      localStorage.setItem('userInfo', JSON.stringify(res.data))
      localStorage.setItem('userType', res.userType)
      // 直接跳转到首页
      router.push('/home')
    } else {
      alert(res.message || '登录失败')
    }
  } catch (error) {
    console.error('登录失败：', error)
    alert('登录失败，请检查网络连接')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

/* 顶部信息栏 */
.header {
  background-color: rgba(255, 255, 255, 0.95);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  padding: 15px 30px;
}

.header-content {
  display: flex;
  align-items: center;
}

.system-icon {
  font-size: 28px;
  margin-right: 10px;
}

.system-name {
  font-size: 22px;
  font-weight: bold;
  color: #333;
}

/* 登录区域 */
.login-wrapper {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

.login-container {
  width: 420px;
  background: #fff;
  border-radius: 12px;
  padding: 40px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
}

.login-title {
  text-align: center;
  color: #333;
  font-size: 24px;
  margin-bottom: 30px;
}

/* 登录类型切换 */
.login-tabs {
  display: flex;
  margin-bottom: 30px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  overflow: hidden;
}

.tab-item {
  flex: 1;
  padding: 12px;
  text-align: center;
  cursor: pointer;
  background: #f5f5f5;
  color: #666;
  transition: all 0.3s;
  font-size: 15px;
}

.tab-item:first-child {
  border-right: 1px solid #e0e0e0;
}

.tab-item.active {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #fff;
}

.tab-icon {
  margin-right: 6px;
}

/* 表单 */
.form-item {
  margin-bottom: 22px;
}

.form-item label {
  display: block;
  margin-bottom: 8px;
  color: #333;
  font-size: 14px;
}

.form-item input {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #ddd;
  border-radius: 6px;
  box-sizing: border-box;
  font-size: 14px;
  transition: border-color 0.3s;
}

.form-item input:focus {
  outline: none;
  border-color: #667eea;
}

.login-btn {
  width: 100%;
  padding: 14px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 16px;
  cursor: pointer;
  transition: opacity 0.3s;
}

.login-btn:hover {
  opacity: 0.9;
}

/* 底部 */
.footer {
  text-align: center;
  padding: 20px;
  color: rgba(255, 255, 255, 0.8);
  font-size: 13px;
}
</style>
