<template>
  <div class="register-container">
    <div class="register-box">
      <div class="register-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>
        </div>
      </div>
      <div class="register-right">
        <div class="register-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"
              />
            </el-form-item>
            <el-form-item prop="name">
              <el-input
                size="large"
                :prefix-icon="User"
                placeholder="请输入姓名"
                v-model="data.form.name"
              />
            </el-form-item>
            <el-form-item prop="phone">
              <el-input
                size="large"
                :prefix-icon="Phone"
                placeholder="请输入手机号"
                v-model="data.form.phone"
              />
            </el-form-item>
            <el-form-item prop="email">
              <el-input
                size="large"
                :prefix-icon="Message"
                placeholder="请输入邮箱（选填）"
                v-model="data.form.email"
              />
            </el-form-item>
            <el-form-item prop="sex">
              <el-radio-group v-model="data.form.sex" size="large">
                <el-radio label="男">男</el-radio>
                <el-radio label="女">女</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item prop="doctorId">
              <el-select
                v-model="data.form.doctorId"
                size="large"
                style="width: 100%"
                placeholder="请选择您的主治医生"
                :loading="data.doctorsLoading"
              >
                <el-option
                  v-for="doctor in data.doctors"
                  :key="doctor.id"
                  :label="doctor.name + (doctor.professional ? ` (${doctor.professional})` : '')"
                  :value="doctor.id"
                >
                  <div style="display: flex; align-items: center;">
                    <img v-if="doctor.avatar" :src="doctor.avatar" style="width: 30px; height: 30px; border-radius: 50%; margin-right: 10px;" />
                    <div>
                      <div>{{ doctor.name }}</div>
                      <div style="font-size: 12px; color: #999;" v-if="doctor.professional">{{ doctor.professional }}</div>
                    </div>
                  </div>
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button
                size="large"
                type="primary"
                class="register-btn"
                @click="register()"
                :loading="data.loading"
              >
                注 册
              </el-button>
            </el-form-item>
            <el-form-item class="login-link">
              已有账号？去 <router-link to="/login">登录</router-link>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
    <div class="footer">
      © {{ new Date().getFullYear() }} 抑郁症患者辅助治疗系统 - 版权所有
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from "vue";
import { User, Lock, Phone, Message, MagicStick, Document, 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: {
    sex: "男",
    role: "patient" // 默认为患者角色
  },
  doctors: [],
  doctorsLoading: false,
  loading: false,
  rules: {
    username: [{ required: true, message: "请输入账号", trigger: "blur" }],
    password: [{ required: true, message: "请输入密码", trigger: "blur" }],
    phone: [{ required: true, message: "请输入手机号", trigger: "blur" }],
    name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
    doctorId: [{ required: true, message: "请选择主治医生", trigger: "change" }],
    sex: [{ required: true, message: "请选择性别", trigger: "change" }],
  }
});

const formRef = ref();

// 加载医生列表
const loadDoctors = () => {
  data.doctorsLoading = true;
  request.get("/doctor").then((res) => {
    if (res.code === "200") {
      data.doctors = res.data || [];
    } else {
      ElMessage.error("加载医生列表失败");
    }
  }).finally(() => {
    data.doctorsLoading = false;
  });
};

onMounted(() => {
  loadDoctors();
});

const register = () => {
  formRef.value
    .validate((valid) => {
      if (valid) {
        data.loading = true;
        request.post("/register", data.form).then((res) => {
          if (res.code === "200") {
            ElMessage.success("注册成功，请登录");
            router.push("/login");
          } else {
            ElMessage.error(res.msg || "注册失败，请稍后再试");
          }
        }).finally(() => {
          data.loading = false;
        });
      }
    })
    .catch((error) => {
      console.error(error);
    });
};
</script>

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

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

.register-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;
}

.register-right {
  flex: 1;
  display: flex;
  align-items: center;
  padding: 40px;
  overflow-y: auto;
}

.register-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;
}

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

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

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

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

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

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

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