<template>
  <div>
    <!--<v-row justify="center">
      <v-col cols="10">
        <v-text-field
          v-model="userRegistry.username"
          placeholder="请输入用户名"
          label="用户名"
          :rules="[() => userRegistry.username != null || '用户名不能为空']"
          clearable
          @input="checkUsername"
          @blur="selectUsername"
        />
      </v-col>
    </v-row>-->
    <v-row justify="center">
      <v-col cols="10">
        <v-text-field
          v-model="userRegistry.email"
          placeholder="请输入邮箱"
          label="邮箱"
          :rules="[() => userRegistry.email != null || '邮箱不能为空']"
          type="email"
          clearable
          @blur="checkEmail"
        />
      </v-col>
    </v-row>
    <v-row justify="center">
      <v-col cols="10">
        <v-row justify="center">
          <v-col cols="5">
            <v-text-field
              v-model="userRegistry.regVerifyCode"
              placeholder="请输入邮件验证码"
              label="邮件验证码"
              :rules="[() => userRegistry.regVerifyCode != null || '邮件验证码不能为空']"
              type="email"
              clearable
            />
          </v-col>
          <v-col cols="5">
            <v-btn color="primary" @click="getVerifyCode">获取邮件验证码</v-btn>
          </v-col>
        </v-row>
      </v-col>
    </v-row>
    <v-row justify="center">
      <v-col cols="10">
        <v-text-field
          v-model="userRegistry.password"
          placeholder="请输入密码"
          label="密码"
          :rules="[() => userRegistry.password != null || '密码不能为空']"
          clearable
          type="password"
        />
      </v-col>
    </v-row>
    <v-row justify="center">
      <v-col cols="5">
        <img :src="captchaUrl" alt="验证码" title="点击刷新" style="cursor:pointer;" @click="getCaptcha">
        <!--<img :src="captchaBase64" alt="验证码" title="点击刷新" style="cursor:pointer;" @click="getCaptcha">-->
      </v-col>
      <v-col cols="5">
        <v-text-field
          v-model="userRegistry.captchaCode"
          placeholder="验证码"
          label="验证码"
          :rules="[() => userRegistry.captchaCode != null || '验证码不能为空']"
          clearable
        />
      </v-col>
    </v-row>
    <v-row justify="center">
      <v-btn color="primary" @click="submitRegister">注册</v-btn>
    </v-row>
  </div>
</template>

<script>
import { randomString, getCaptchaUrl } from '@/utils'
import { getVerifyCode, isUsernameExist, selectUsername, isEmailExist } from '@/api/user/account'

export default {
  name: 'Register',
  data() {
    return {
      captchaUrl: '',
      captchaBase64: '',
      showMessage: false,
      userRegistry: {
        regType: 1,
        email: '',
        password: '',
        regVerifyCode: '',
        captchaCode: '',
        r: ''
      }
    }
  },
  created() {
    this.getCaptcha()
  },
  methods: {
    checkUsername() {
      isUsernameExist(this.userRegistry.username)
        .then(res => {
          if (res.code === 0) {
            console.log(res.msg)
          } else {
            alert(res.data)
          }
        })
        .catch(error => {
          console.error(error.message)
        })
    },
    selectUsername() {
      selectUsername(this.userRegistry.username).then(res => {
        if (res.code === 0) {
          console.log(res.msg)
        } else {
          alert(res.data)
        }
      })
        .catch(error => {
          console.error(error.message)
        })
    },
    checkEmail() {
      const email = this.userRegistry.email
      if (email === null || email === '') {
        return
      }

      isEmailExist(this.userRegistry.email)
        .then(res => {
          if (res.code === 0) {
            console.log(res.msg)
          } else {
            alert(res.data)
          }
        })
        .catch(error => {
          console.error(error.message)
        })
    },
    submitRegister() {
      var re = /^(([^()[\]\\.,;:\s@\"]+(\.[^()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
      if (!re.test(this.userRegistry.email)) {
        console.log('email 字段不符合要求')
        return
      } else if (this.userRegistry.password === '' || this.userRegistry.password.length < 6) {
        console.log('password 字段不符合要求')
        return
      } else if (this.userRegistry.captchaCode === '') {
        alert('captchaCode 或 username 字段不符合要求')
        return
      }

      if (this.$store.state.webInfo.openInvitationRegister === 1 && this.userRegistry.invitationCode === '') {
        console.log('openInvitationRegister 或 invitationCode 字段不符合要求')
        return
      }
      // 返回到父组件
      this.$emit('register', this.userRegistry)
    },
    getCaptcha() {
      const randomStr = randomString(10)
      this.userRegistry.r = randomStr
      // 图片上发送点击事件时，captchaUrl 的值发生变化，然后才去请求后端
      this.captchaUrl = getCaptchaUrl(randomStr)
      /* getBase64Captcha(this.captchaUrl)
        .then(res => {
          if (res.code === 0) {
            this.captchaBase64 = 'data:image/jpg;base64,' + res.data
          } else {
            alert(res.data)
          }
        })
        .catch(error => {
          console.error(error.message)
        })*/
    },
    getVerifyCode() {
      const email = this.userRegistry.email
      if (email == null || email === '') {
        alert('请输入邮箱地址')
        return
      }

      const verifyCodeReq = {}
      verifyCodeReq.receiver = email
      verifyCodeReq.notifyType = 1
      getVerifyCode(verifyCodeReq)
        .then(res => {
          if (res.code === 0) {
            alert(res.msg)
          } else {
            console.error(res.msg)
          }
        })
        .catch(error => {
          console.error(error.message)
        })
    }
  }
}
</script>

<style>
</style>
