<template>
  <div class="register">
    <div class="register__container">
      <div class="register__icon">
        <i :class="submitted ? 'el-icon-check' : 'el-icon-edit-outline'"></i>
      </div>
      <h2 class="register__title">{{ submitted ? '提交成功' : '咨询信息登记' }}</h2>
      <p class="register__subtitle">{{ submitted ? '我们已收到您的信息' : '请填写您的姓名和联系电话' }}</p>

      <el-card v-if="!submitted" class="register__card" shadow="hover">
        <!-- 成功状态 -->
        <div v-if="submitted" class="register__success">
          <div class="register__success-icon">
            <i class="el-icon-circle-check"></i>
          </div>
          <el-button
            type="primary"
            class="register__btn register__btn--success"
            @click="resetForm"
          >
            继续登记
          </el-button>
        </div>

        <!-- 表单 -->
        <el-form v-if="!submitted" :model="form" ref="formRef">
          <el-form-item prop="name">
            <el-input
              v-model="form.name"
              placeholder="请输入姓名"
              
              size="large"
              prefix-icon="el-icon-user"
            />
          </el-form-item>
          <el-form-item prop="phone">
            <el-input
              v-model="form.phone"
              placeholder="请输入手机号"
              maxlength="11"
              
              size="large"
              prefix-icon="el-icon-mobile-phone"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              :loading="loading"
              @click="handleSubmit"
              class="register__btn"
            >
              <i v-if="!loading" class="el-icon-check"></i>
              确定
            </el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Register',
  data() {
    return {
      form: {
        name: '',
        phone: ''
      },
      loading: false,
      submitted: false
    }
  },
  methods: {
    async handleSubmit() {
      const name = (this.form.name || '').trim()
      const phone = (this.form.phone || '').trim()

      if (!name) {
        this.$message.warning('请填写姓名')
        return
      }
      if (!phone) {
        this.$message.warning('请填写联系电话')
        return
      }
      if (!/^1[3-9]\d{9}$/.test(phone)) {
        this.$message.warning('请填写有效联系电话')
        return
      }

      this.loading = true
      try {
        const { createRegistration } = await import('@/api/registrations')
        await createRegistration({
          customerName: name,
          contact: phone
        })
        this.$message.success('提交成功')
        this.submitted = true
      } catch (err) {
        this.$message.error('网络错误，请检查连接')
        // eslint-disable-next-line no-console
        console.error(err)
      } finally {
        this.loading = false
      }
    },
    resetForm() {
      this.form.name = ''
      this.form.phone = ''
      this.submitted = false
    }
  }
}
</script>

<style scoped>
.register {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.register__container {
  text-align: center;
}

.register__icon {
  width: 80px;
  height: 80px;
  margin: 0 auto 20px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.register__icon i {
  font-size: 36px;
  color: #fff;
}

.register__title {
  color: #fff;
  font-size: 28px;
  font-weight: 600;
  margin: 0 0 8px;
}

.register__subtitle {
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
  margin: 0 0 30px;
}

.register__card {
  width: 360px;
  border-radius: 12px;
  border: none;
}

.register__card ::v-deep .el-card__body {
  padding: 30px;
}

.register__card ::v-deep .el-input__inner {
  height: 48px;
  line-height: 48px;
  font-size: 16px;
  border-radius: 8px;
}

.register__card ::v-deep .el-input__prefix {
  left: 12px;
  font-size: 18px;
  color: #909399;
}

.register__card ::v-deep .el-input--prefix .el-input__inner {
  padding-left: 40px;
}

.register__btn {
  width: 100%;
  height: 48px;
  font-size: 16px;
  border-radius: 8px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
}

.register__btn:hover {
  opacity: 0.9;
}

.register__btn i {
  margin-right: 6px;
}

.register__btn--success {
  background: linear-gradient(135deg, #22c55e 0%, #16a34a 100%);
}

.register__success {
  text-align: center;
  padding: 20px 0;
}

.register__success-icon {
  width: 72px;
  height: 72px;
  margin: 0 auto 24px;
  background: rgba(22, 163, 74, 0.1);
  border: 2px solid rgba(22, 163, 74, 0.3);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.register__success-icon i {
  font-size: 36px;
  color: #22c55e;
}

@media (max-width: 480px) {
  .register__card {
    width: 100%;
    max-width: 360px;
  }

  .register__title {
    font-size: 24px;
  }

  .register__icon {
    width: 64px;
    height: 64px;
  }

  .register__icon i {
    font-size: 28px;
  }
}
</style>
