<template>
  <div class="login-container">
    <section>
      <el-form ref="loginForm" :model="loginForm" class="login-form" :rules="rules">

        <div class="title-container">
          <h3 class="title">停车场管理系统-注册</h3>
        </div>

        <!-- 手机 -->
        <el-form-item prop="Phone">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input ref="Phone" v-model="loginForm.Phone" placeholder="请输入手机号" name="Phone" type="text" tabindex="4" />
        </el-form-item>

        <!-- 验证码 -->
        <el-form-item prop="verifycode">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input ref="verifycode" v-model="loginForm.verifycode" placeholder="请输入验证码" name="verifycode" type="text"
            tabindex="6" style="width:60%;" />
          <el-button type="primary" style="width:30%;" @click.native.prevent="SendCode">{{ codeStatus === 0 ? "发送验证码" :
            codeStatus + "秒后重新发送" }}</el-button>
        </el-form-item>
        <!-- 用户名 -->
        <el-form-item prop="UserName">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input ref="UserName" v-model="loginForm.UserName" placeholder="用户名" type="text" tabindex="1"
            @blur="reload" />
        </el-form-item>
        <!-- 密码 -->
        <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
          <el-form-item prop="PassWord">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input :key="passwordType" ref="PassWord" v-model="loginForm.PassWord" :type="passwordType"
              placeholder="密码" tabindex="2" autocomplete="on" @keyup.native="checkCapslock" @blur="capsTooltip = false"
              @keyup.enter.native="handleLogin" />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>
        </el-tooltip>
        <el-tooltip v-model="recapsTooltip" content="Caps lock is On" placement="right" manual>
          <el-form-item prop="RePassWord">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input :key="repasswordType" ref="RePassWord" v-model="loginForm.RePassWord" :type="repasswordType"
              placeholder="确认密码" tabindex="3" autocomplete="on" @keyup.native="recheckCapslock"
              @blur="recapsTooltip = false" />
            <span class="show-pwd" @click="reshowPwd">
              <svg-icon :icon-class="repasswordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>
        </el-tooltip>
        <el-button type="primary" style="width:100%;margin-bottom:30px;"
          @click.native.prevent="register">注册</el-button>
        <div class="tips">
          <router-link to="./login">已有账户? 点击登录</router-link>
        </div>
      </el-form>
    </section>
  </div>
</template>

<script>
// import { register } from '@/api/user'
import axios from 'axios'
export default {
  data() {
    return {
      loginForm: {
        // 手机号
        Phone: "",
        RePassWord: "",
        PassWord: "",
        UserName: "",
        verifycode: "",
      },
      capsTooltip: false,
      passwordType: 'password',
      recapsTooltip: false,
      repasswordType: 'password',
      codeStatus: 0, // 验证码倒计时状态，初始为0表示可以发送验证码
      timer: null,   // 定时器对象
      loading: false, // 加载状态
      // 校验规则
      rules: {
        Phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { min: 11, max: 11, message: "长度不超过11个数字", trigger: "blur" }
        ],
        verifycode: [
          { required: true, message: "请输入验证码", trigger: "blur" },
          { min: 4, max: 4, message: "长度不超过4个数字", trigger: "blur" }
        ],UserName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 12, message: "最小3个字符最大12个字符", trigger: "blur" }
        ], PassWord: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 12, message: "最小6个字符最大12个字符", trigger: "blur" },
        ], RePassWord: [
          { required: true, message: "请再次输入密码", trigger: "blur" },
          { validator: this.validateConfirmPassword, trigger: "blur" }
        ]
      }
    }
  },
  methods: {
    // 发送验证码
    async SendCode() {
      if (this.codeStatus !== 0) {
        // 如果倒计时状态不为0，表示还在倒计时中，直接返回
        return;
      };
      this.loading = true;
      try {
        const result = await this.$store.dispatch("user/sendcode", this.loginForm);
        if (result.status === 200) {
          this.codeStatus = 60; // 设置倒计时状态为60秒
          this.timer = setInterval(() => { // 启动定时器
            if (this.codeStatus > 0) {
              this.codeStatus--; // 更新倒计时状态
            } else {
              clearInterval(this.timer); // 倒计时结束，清除定时器
              this.timer = null;
            }
          }, 1000);
          this.$message({
            message: result.message,
            type: 'success'
          });
        }
      } catch (error) {
        this.$message.error('验证码发送失败');
      }

      this.loading = false;
    },
    // 注册接口
    async register() {
      this.loading = true;
      try {
        await this.$refs.loginForm.validate(); // 启用表单校验
        const result = await this.$store.dispatch("user/register", this.loginForm);
        // 注册成功，处理逻辑
        if (result.status == 200) {
          this.$confirm(result.message+'是否继续前往登录页面', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$router.push({ path: "/" });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });          
        })
      } }catch (error) {
        // 表单校验失败，不执行注册操作，显示错误信息
        this.$message.error(error.message);
      }
    
      },
    // 两次输入密码是否一直
    validateConfirmPassword(rule, value, callback) {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.loginForm.PassWord) {
        callback(new Error('两次输入密码不一致'));
      } else {
        callback();
      }
    },
    // 显示密码
    showPwd() {
      if (this.passwordType === 'PassWord') {
        this.passwordType = ''
      } else {
        this.passwordType = 'PassWord'
      }
      this.$nextTick(() => {
        this.$refs.PassWord.focus()
      })
    },
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    reshowPwd() {
      if (this.repasswordType === 'password') {
        this.repasswordType = ''
      } else {
        this.repasswordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.RePassWord.focus()
      })
    },
    recheckCapslock(e) {
      const { key } = e
      this.recapsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    reload() {
      this.rerifycodeImg = `${process.env.VUE_APP_BASE_API}/user-service/verifycode/${this.loginForm.username}?t=${new Date().getTime()}`
    },
    

  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

// $bg:#283443;
$bg: #e6eaef;
$light_gray: #fff;
$cursor: rgb(19, 19, 19);
// $cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {

  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      // background: transparent;
      // border: 0px;
      // -webkit-appearance: none;
      // border-radius: 0px;
      // padding: 12px 5px 12px 15px;
      // // color: $light_gray;
      //  color: rgb(97, 60, 60);
      // height: 47px;
      // caret-color: $cursor;
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 25px;
      padding: 12px 5px 12px 15px;
      color: rgb(97, 60, 60);
      height: 46px;
      caret-color: rgba(62, 119, 194, 0.8);

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    // border: 1px solid rgba(255, 255, 255, 0.1);
    // background: rgba(0, 0, 0, 0.1);
    background: rgba(172, 172, 172, 0.1);
    border-radius: 5px;
    // color: #454545;
    color: #886767;
  }
}
</style>

<style lang="scss" scoped>
// $bg:#2d3a4b;
// $dark_gray:#889aa4;
// $light_gray:#eee;
$bg: #514d4d;
$dark_gray: #889aa4;
$light_gray: #fff;

.login-container {
  // min-height: 100%;
  //   width: 100%;
  //   background-color: $bg;
  //   overflow: hidden;

  width: 100%;
  height: 100%;
  background-color: #2d3a4b;
  background-size: cover;
  background-position: center;
  position: center;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .header {
    border-bottom: 2px solid rgb(235, 232, 232);
    min-width: 980px;
    color: #666;

    header {
      margin: 0 auto;
      padding: 10px 0;
      width: 980px;

      .login {
        float: right;
      }

      .bold {
        font-style: normal;
        color: $light_gray;
      }
    }
  }

  >section {
    margin: 0 auto 30px;
    padding-bottom: 100px;
    width: 980px;
    min-height: 300px;
    box-sizing: border-box;

    .status {
      font-size: 12px;
      margin-left: 20px;
      color: #e6a23c;
    }

    .error {
      color: red;
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>

