<template>
  <div
    class="rule-desc"
    @keydown.enter="submit"
  >
    <div class="wrapper-1">
      <button
        class="close"
        @click="$router.go(-1)"
      />
      <h1 class="">
        欢迎注册
      </h1>

      <div class="form-item">
        <img
          :class="{
            animate__animated: ifTipUserName,
            animate__headShake: ifTipUserName
          }"
          src="@/assets/images/username-icon.png"
          alt=""
          draggable="false"
        >
        <input
          ref="first-focus"
          v-model.trim="userName"
          type="text"
          placeholder="请输入账号..."
        >
      </div>
      <div class="form-item">
        <img
          :class="{
            animate__animated: ifTipPassword,
            animate__headShake: ifTipPassword
          }"
          src="@/assets/images/password-icon.png"
          alt=""
          draggable="false"
        >
        <input
          v-model="password"
          type="password"
          placeholder="请输入密码..."
          @keydown.stop
        >
      </div>
      <div class="form-item form-item-password-repeat">
        <img
          :class="{
            animate__animated: ifTipPasswordRepeat,
            animate__headShake: ifTipPasswordRepeat
          }"
          src="@/assets/images/password-icon.png"
          alt=""
          draggable="false"
        >
        <input
          v-model="passwordRepeat"
          type="password"
          placeholder="请再次输入密码..."
        >
        <div
          v-show="ifTipPasswordRepeatText"
          class="tip-text"
        >
          两次输入密码不一致！
        </div>
      </div>

      <button
        class="submit"
        @click="submit"
      >
        <div class="text-wrapper">
          立即注册
        </div>
      </button>
      <button
        class="sign-up"
        @click="$router.replace({name: 'Login'})"
      >
        <div class="text-wrapper">
          已有账号，去登录
        </div>
      </button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userName: '',
      password: '',
      passwordRepeat: '',

      ifTipUserName: false,
      ifTipPassword: false,
      ifTipPasswordRepeat: false,
    }
  },
  computed: {
    ifTipPasswordRepeatText() {
      if (this.password && this.passwordRepeat && this.password !== this.passwordRepeat) {
        return true
      } else {
        return false
      }
    }
  },
  mounted() {
    window.ifPreventKeyOperationInScene = true
    this.$refs['first-focus'].focus()
  },
  beforeDestroy() {
    window.ifPreventKeyOperationInScene = false
  },
  methods: {
    submit: globalUtils.throttle(function() {
      if (this.userName.length === 0) {
        this.ifTipUserName = true
        setTimeout(() => {
          this.ifTipUserName = false
        }, 1000)
        return
      }
      if (this.password.length === 0) {
        this.ifTipPassword = true
        setTimeout(() => {
          this.ifTipPassword = false
        }, 1000)
        return
      }
      if (this.passwordRepeat === 0 || this.ifTipPasswordRepeatText) {
        this.ifTipPasswordRepeat = true
        setTimeout(() => {
          this.ifTipPasswordRepeat = false
        }, 1000)
        return
      }

      globalApi.signUp(this.userName, this.password).then((res) => {
        this.$alert('即将为您自动登录...', '注册成功！', {
          confirmButtonText: '确定',
          callback: action => {
            if (action === 'confirm') {
              globalApi.login(this.userName, this.password).then((res) => {
                this.$router.go(-1)
              }).catch(error => {
                window.alert(error)
              })
            }
          }
        })
      }).catch(error => {
        window.alert(error)
      })
    }, 1500),
  },
}
</script>

<style lang="less" scoped>
.rule-desc {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(46,32,19,0.55);
  backdrop-filter: blur(5px);
  z-index: 10000;
  > .wrapper-1 {
    width: 624px;
    height: calc(624px * 525 / 637);
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    background-image: url(@/assets/images/sign-up-bg.png);
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center center;
    text-align: center;
    padding: 29px 100px 40px 100px;
    overflow: auto;
    > .close {
      position: absolute;
      top: 19px;
      right: 100px;
      width: 45px;
      height: 45px;
      opacity: 0;
    }
    > h1 {
      display: inline-block;
      background-image: url(@/assets/images/title-decorator-long.png);
      background-size: contain;
      background-repeat: no-repeat;
      background-position: center center;
      padding: 0 45px 20px 45px;
      font-size: 36px;
      font-family: LiSu-Regular, LiSu;
      font-weight: 400;
      color: #9A2D0A;
      margin-bottom: 30px;
    }
    > .form-item {
      height: 43px;
      width: 300px;
      border-bottom: 1px solid #A4573F;
      display: inline-flex;
      align-items: center;
      padding: 0 12px;
      margin-bottom: 10px;
      > img {
        flex: 0 0 auto;
        width: 26px;
        height: 26px;
        margin-right: 14px;
      }
      > input {
        flex: 1 0 1px;
        font-size: 16px;
        font-family: Source Han Sans CN-Regular, Source Han Sans CN;
        font-weight: 400;
        color: #9A2D0A;
      }
    }
    > .form-item-password-repeat {
      position: relative;
      > .tip-text {
        position: absolute;
        top: calc(100% + 3px);
        color: red;
        font-size: 14px;
      }
    }

    > button.submit {
      background-image: url(@/assets/images/button-border-long-transparent.png);
      background-size: contain;
      background-repeat: no-repeat;
      background-position: center center;
      height: 50px;
      width: 300px;
      padding: 4px 8px 4px 6px;
      margin-top: 20px;
      margin-bottom: 10px;
      > .text-wrapper {
        height: 100%;
        width: 100%;
        background: rgba(143, 72, 49, 0.8);
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 20px;
        font-family: LiSu-Regular, LiSu;
        font-weight: 400;
        color: #FFFFFF;
        letter-spacing: 4px;
      }
    }
    > button.sign-up {
      background-image: url(@/assets/images/button-border-long-transparent.png);
      background-size: contain;
      background-repeat: no-repeat;
      background-position: center center;
      height: 50px;
      width: 300px;
      padding: 4px 8px 4px 6px;
      margin-bottom: 10px;
      > .text-wrapper {
        height: 100%;
        width: 100%;
        background: transparent;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 20px;
        font-family: LiSu-Regular, LiSu;
        font-weight: 400;
        color: #955D4B;
      }
    }
  }
}

.mobile {
  .rule-desc {
    > .wrapper-1 {
      width: 140vw;
      height: calc(140vw * 407 / 460);
      background-image: url(@/assets/images/sign-up-bg-mobile.png);
      padding: 12vw 20vw 8vw 20vw;
      > .close {
        top: 4vw;
        right: 33.5vw;
        width: 7.5vw;
        height: 7.5vw;
      }
      > h1 {
        padding: 0 9vw 4vw 9vw;
        font-size: 6.4vw;
        margin-bottom: 10vw;
      }
      > .form-item {
        height: 8.6vw;
        width: 60vw;
        padding: 0 2.4vw;
        margin-bottom: 2vw;
        > img {
          width: 5.2vw;
          height: 5.2vw;
          margin-right: 2.8vw;
        }
        > input {
          font-size: 3.7vw;
        }
      }
      > .form-item-password-repeat {
        > .tip-text {
          top: calc(100% + 0.6vw);
          font-size: 2.8vw;
        }
      }

      > button.submit {
        height: 10vw;
        width: 60vw;
        padding: 0.8vw 1.6vw 0.8vw 1.2vw;
        margin-top: 12vw;
        margin-bottom: 2vw;
        > .text-wrapper {
          font-size: 4.3vw;
          letter-spacing: 0.8vw;
        }
      }
      > button.sign-up {
        height: 10vw;
        width: 60vw;
        padding: 0.8vw 1.6vw 0.8vw 1.2vw;
        margin-bottom: 2vw;
        > .text-wrapper {
          font-size: 4.3vw;
        }
      }
    }
  }
}
</style>