<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-left">
        <div class="overlay">
          <div class="system-title">
            <h1>抑郁症患者辅助治疗系统</h1>
            <p>Depression Auxiliary Treatment System</p>
          </div>
          <div class="system-features">
            <div class="feature-item">
              <el-icon><User /></el-icon>
              <span>心理健康评估</span>
            </div>
            <div class="feature-item">
              <el-icon><Document /></el-icon>
              <span>症状记录与追踪</span>
            </div>
            <div class="feature-item">
              <el-icon><Reading /></el-icon>
              <span>心理资源推荐</span>
            </div>
            <div class="feature-item">
              <el-icon><ChatDotRound /></el-icon>
              <span>医患沟通平台</span>
            </div>
            <div class="feature-item">
              <el-icon><Histogram /></el-icon>
              <span>数据统计分析</span>
            </div>
          </div>
        </div>
      </div>
      <div class="login-right">
        <div class="login-form">
          <div class="form-header">
            <div class="logo">
              <el-icon><MagicStick /></el-icon>
            </div>
            <h2>欢迎使用</h2>
            <p>我们与你同行，每一步都有希望</p>
          </div>
          <el-form :model="data.form" ref="formRef" :rules="data.rules">
            <el-form-item prop="username">
              <el-input
                size="large"
                :prefix-icon="User"
                placeholder="请输入账号"
                v-model="data.form.username"
              />
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                size="large"
                :prefix-icon="Lock"
                show-password
                placeholder="请输入密码"
                v-model="data.form.password"
                @keyup.enter="login"
              />
            </el-form-item>
            <el-form-item prop="role">
              <el-select
                size="large"
                style="width: 100%"
                placeholder="请选择角色"
                v-model="data.form.role"
              >
                <el-option value="admin" label="管理员" />
                <el-option value="doctor" label="医生" />
                <el-option value="patient" label="患者" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button
                size="large"
                type="primary"
                class="login-btn"
                @click="login()"
                :loading="data.loading"
              >
                登 录
              </el-button>
            </el-form-item>
            <el-form-item class="register-link">
              还没有账号？去 <router-link to="/register">注册</router-link>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
    <div class="footer">
      © {{ new Date().getFullYear() }} 抑郁症患者辅助治疗系统 - 版权所有
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { User, Lock, MagicStick, Document, Histogram, ChatDotRound, Reading } from "@element-plus/icons-vue";
import request from "@/utils/request";
import { ElMessage } from "element-plus";
import router from "@/router";

const data = reactive({
  form: {
    role: "patient" // 默认选择患者角色
  },
  rules: {
    username: [{ required: true, message: "请输入账号", trigger: "blur" }],
    password: [{ required: true, message: "请输入密码", trigger: "blur" }],
    role: [{ required: true, message: "请选择角色", trigger: "change" }]
  },
  loading: false
});

const formRef = ref();

// 点击登录按钮的时候会触发这个方法
const login = () => {
  if (!data.form.username || !data.form.password || !data.form.role) {
    ElMessage.warning("请填写完整的登录信息");
    return;
  }
  
  formRef.value.validate((valid) => {
    if (valid) {
      data.loading = true;
      // 调用后台的接口
      request.post("/login", data.form).then((res) => {
        if (res.code === "200") {
          // 先设置用户菜单缓存
          localStorage.setItem("xm-user", JSON.stringify(res.data));
          ElMessage.success("登录成功");
          router.push("/manager/home");
        } else {
          ElMessage.error(res.msg);
        }
      }).finally(() => {
        data.loading = false;
      });
    }
  }).catch((error) => {
    console.error(error);
  });
};
</script>

<style scoped>
.login-container {
  height: 100vh;
  background-color: #f5f7fa;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
}

.login-box {
  width: 1000px;
  height: 600px;
  background: #fff;
  border-radius: 12px;
  display: flex;
  overflow: hidden;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
}

.login-left {
  flex: 1.2;
  background-image: url('@/assets/imgs/VCG41N1408837996.jpg');
  background-size: cover;
  background-position: center;
  position: relative;
}

.overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, rgba(91, 133, 216, 0.9) 0%, rgba(118, 99, 207, 0.8) 100%);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 40px;
  color: white;
}

.system-title {
  text-align: center;
  margin-bottom: 40px;
}

.system-title h1 {
  font-size: 32px;
  font-weight: 600;
  margin-bottom: 10px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.system-title p {
  font-size: 16px;
  opacity: 0.9;
}

.system-features {
  width: 100%;
}

.feature-item {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  background: rgba(255, 255, 255, 0.1);
  padding: 12px 20px;
  border-radius: 8px;
  transition: all 0.3s;
}

.feature-item:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: translateX(5px);
}

.feature-item .el-icon {
  font-size: 20px;
  margin-right: 12px;
}

.login-right {
  flex: 1;
  display: flex;
  align-items: center;
  padding: 40px;
}

.login-form {
  width: 100%;
}

.form-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo {
  width: 60px;
  height: 60px;
  background-color: #6563c0;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 20px;
}

.logo .el-icon {
  font-size: 30px;
  color: white;
}

.form-header h2 {
  font-size: 24px;
  color: #1f2937;
  margin-bottom: 8px;
}

.form-header p {
  font-size: 14px;
  color: #6b7280;
}

.el-form-item {
  margin-bottom: 20px;
}

.login-btn {
  width: 100%;
  background-color: #6563c0;
  border: none;
  height: 45px;
  font-size: 16px;
  transition: all 0.3s;
}

.login-btn:hover {
  background-color: #5758a7;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(101, 99, 192, 0.3);
}

.register-link {
  text-align: center;
  margin-top: 10px;
  color: #6b7280;
}

.register-link a {
  color: #6563c0;
  text-decoration: none;
  font-weight: 500;
}

.register-link a:hover {
  text-decoration: underline;
}

.footer {
  position: absolute;
  bottom: 20px;
  color: #6b7280;
  font-size: 14px;
}

/* 响应式调整 */
@media (max-width: 1024px) {
  .login-box {
    width: 90%;
    height: auto;
    flex-direction: column;
  }
  
  .login-left {
    height: 200px;
  }
  
  .system-features {
    display: none;
  }
  
  .login-right {
    padding: 30px;
  }
}
</style>