<script setup>
import {ref} from 'vue';
import {ElButton, ElForm, ElFormItem, ElInput, ElMessage} from 'element-plus';
import 'element-plus/dist/index.css';
import axios from 'axios';
import {useRouter} from 'vue-router';

const username = ref('');
const phoneNumber = ref('');
const password = ref('');
const verificationCode = ref('');
const router = useRouter();

const handleRegister = async () => {
  console.log('Registering with', {
    username: username.value,
    phoneNumber: phoneNumber.value,
    password: password.value,
    verificationCode: verificationCode.value
  });

  try {
    const response = await axios.post('/api/register', {
      username: username.value,
      phoneNumber: phoneNumber.value,
      password: password.value,
      verificationCode: verificationCode.value,
    });

    if (response.status === 200) {
      ElMessage.success(response.data.message);
      router.push('/login');
    } else {
      ElMessage.error(response.data.message);
    }
  } catch (error) {
    ElMessage.error(error.response.data.message || '注册失败，请稍后再试');
  }
};

const sendVerificationCode = async () => {
  try {
    const response = await axios.post('/api/sendVerificationCode', {
      phoneNumber: phoneNumber.value,
    });

    if (response.status === 200) {
      verificationCode.value = response.data.code;
      ElMessage.success('验证码已发送');
    } else {
      ElMessage.error('发送验证码失败，请稍后再试');
    }
  } catch (error) {
    ElMessage.error('发送验证码失败，请稍后再试');
  }
};
</script>

<template>
  <div class="register-container">
    <div class="register-box">
      <h2>企业租户注册</h2>
      <ElForm @submit.prevent="handleRegister">
        <ElFormItem>
          <label for="userName">用户名称</label>
          <ElInput v-model="username" type="text" id="userName" placeholder="请输入企业名称" required/>
        </ElFormItem>
        <ElFormItem>
          <label for="phoneNumber">手机号码</label>
          <ElInput v-model="phoneNumber" type="text" id="phoneNumber" placeholder="请输入企业联系方式" required/>
        </ElFormItem>
        <ElFormItem>
          <label for="password">密码</label>
          <ElInput v-model="password" type="password" id="password" placeholder="请输入密码" required/>
        </ElFormItem>
        <ElFormItem>
          <label for="verificationCode">验证码</label>
          <ElInput v-model="verificationCode" type="text" id="verificationCode" placeholder="请输入验证码" required>
            <template #append>
              <ElButton type="primary" @click="sendVerificationCode" class="verification-button">获取验证码</ElButton>
            </template>
          </ElInput>
        </ElFormItem>
        <ElFormItem>
          <ElButton type="primary" native-type="submit" class="register-button">注册</ElButton>
        </ElFormItem>
        <ElFormItem>
          <ElButton type="text" @click="() => $router.push('/login')">返回登录</ElButton>
        </ElFormItem>
      </ElForm>
    </div>
  </div>
</template>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: url('background.jpg') no-repeat center center;
  background-size: cover;
}

.register-box {
  background: white;
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  text-align: center;
  width: 20%;
}

h2 {
  margin-bottom: 1rem;
}

.el-form-item {
  margin-bottom: 1rem;
  text-align: left;
}

.el-form-item label {
  display: block;
  margin-bottom: 0.5rem;
}

.el-input {
  width: 100%;
}

.verification-button {
  width: 120px; /* 确保按钮宽度合适 */
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 0; /* 去除默认内边距 */
}

.el-button {
  width: 100%;
  padding: 0.75rem;
}

.el-button--primary {
  background-color: #007bff;
  color: white;
}

.el-button--primary:hover {
  background-color: #0056b3;
}
</style>
