<template>
  <div class="register">
    <div class="top-background"></div>
    <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form" label-position="top">
      <div class="back-icon" @click="$router.back()">
        <i class="el-icon-back"></i>
      </div>
      <div class="title">创建账号</div>
      <el-form-item prop="username" label="账号">
        <el-input v-model="registerForm.username" type="text" auto-complete="off" placeholder="账号">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="mobilePhone" label="电话号码">
        <el-input v-model="registerForm.mobilePhone" placeholder="请输入电话号码" maxlength="11" type="text" auto-complete="off">
          <i slot="prefix" class="el-icon-mobile-phone" />
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaEnabled" label="验证码">
        <el-input
          v-model="registerForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleRegister"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="register-code">
          <el-button @click="verifyBtnClick" round :type="verifyStatus?'success':'default'">{{ verifyStatus ? '验证通过' : '点击完成安全验证' }}</el-button>
        </div>
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input
          v-model="registerForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleRegister"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="confirmPassword" label="确认密码">
        <el-input
          v-model="registerForm.confirmPassword"
          type="password"
          auto-complete="off"
          placeholder="确认密码"
          @keyup.enter.native="handleRegister"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="protocol">
        <el-checkbox-group v-model="registerForm.protocol">
          <el-checkbox name="protocol">我同意
            <span class="link-type" v-for="item in agreementList" :key="item.id" @click.prevent="previewPolicy(item)">
              <template v-if="item.type !=='2'">
                《{{ item.name }}》
              </template>
            </span>
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          class="register-button"
          @click.native.prevent="handleRegister"
        >
          <span v-if="!loading">完 成 注 册</span>
          <span v-else>注 册 中...</span>
        </el-button>
        <div class="register-tip">
          <span>已经有账号了？</span>
          <router-link class="link-type" :to="'/login'">立即登入</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-register-footer">
      <span>Copyright © 2018-2022 xxx All Rights Reserved.</span>
    </div>
    <!-- 拼图验证码 -->
    <Verify
      ref="verify"
      class="verify-con"
      :verifyType="verifyType"
      @change="verifyChange"
    ></Verify>
    <!--  协议弹窗  -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      fullscreen
    >
      <span v-html="policyContent"></span>
    </el-dialog>
  </div>
</template>

<script>
import { register, sendSms } from '@/api/login'
import Verify from '@/components/Verify'
import { getAgreement, listAgreement } from '@/api/buyer/agreement'

export default {
  name: 'Register',
  components: { Verify },
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.registerForm.password !== value) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    return {
      codeUrl: '',
      registerForm: {
        username: '',
        mobilePhone: '',
        password: '',
        confirmPassword: '',
        code: '',
        uuid: '',
        protocol: []
      },
      registerRules: {
        username: [
          { required: true, trigger: 'blur', message: '请输入您的账号' },
          { min: 2, max: 20, message: '用户账号长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        mobilePhone: [
          {
            required: true,
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: '请输入正确的手机号码',
            trigger: 'blur'
          }
        ],
        protocol: [
          { type: 'array', required: true, message: '未勾选同意协议，请勾选后完成注册', trigger: 'change' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '请输入您的密码' },
          { min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, trigger: 'blur', message: '请再次输入您的密码' },
          { required: true, validator: equalToPassword, trigger: 'blur' }
        ],
        code: [{ required: true, trigger: 'change', message: '请输入验证码' }]
      },
      loading: false,
      captchaEnabled: true,
      verifyStatus: false, // 是否验证通过
      verifyType: 'REGISTER', // 验证状态
      agreementList: [],
      open: false,
      policyContent: null,
      title: null
    }
  },
  created() {
    // this.getCode()
    this.getPolicyList()
  },
  methods: {
    // 查看协议内容
    previewPolicy(item) {
      const id = item.id || this.ids
      getAgreement(id).then(response => {
        this.policyContent = response.data.value
        this.open = true
        this.title = item.name
      })
    },
    /** 查询系统协议列表 */
    getPolicyList() {
      listAgreement().then(response => {
        this.agreementList = response.rows
      })
    },
    // 打开图片验证码
    verifyBtnClick() {
      this.$refs.registerForm.validateField('mobilePhone', errorMsg => {
        if (!errorMsg) {
          if (!this.verifyStatus) {
            this.$refs.verify.init()
          }
        }
      })
    },
    // 图片验证码成功回调
    verifyChange(con) {
      if (!con.status) return
      this.$refs.verify.show = false
      this.verifyStatus = true
      sendSms({ mobile: this.registerForm.mobilePhone, verificationEnums: 'REGISTER' })
        .then(res => {

        })

    },
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true
          const params = {
            username: this.registerForm.username,
            mobilePhone: this.registerForm.mobilePhone,
            password: this.registerForm.password,
            confirmPassword: this.registerForm.confirmPassword,
            code: this.registerForm.code,
            uuid: this.registerForm.uuid
          }
          register(params).then(res => {
            const username = this.registerForm.username
            this.$alert('<font color=\'red\'>恭喜你，您的账号 ' + username + ' 注册成功！</font>', '系统提示', {
              dangerouslyUseHTMLString: true,
              type: 'success'
            }).then(() => {
              this.$router.push('/login')
            }).catch(() => {})
          }).catch((err) => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  //background-image: url("../assets/images/login-background.jpg");
  //background-size: cover;
}


.top-background {
  position: fixed;
  top: 0;
  height: 434px;
  background: #6e00ff;
  border-radius: 0 0 10px 10px;
  width: 100%;
}

.title {
  text-align: left;
  font-size: 24px;
  font-weight: bold;
  color: rgb(82, 77, 104);
  margin: 10px 0;
}

.register-form {
  background: #ffffff;
  padding: 50px 50px 20px;
  width: 565px;
  border-width: 1px;
  border-style: solid;
  border-color: rgba(255, 255, 255, 1);
  border-radius: 5px;
  box-shadow: 0px 5px 20px rgb(110 0 255 / 10%);
  position: fixed;

  .back-icon {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    border: 1px solid #6d00ff;
    display: flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    top: 20px;
    left: 25px;
    cursor: pointer;

    .el-icon-back {
      color: #6d00ff;
      font-weight: bold;
    }
  }

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }

  .register-button {
    height: 40px;
    padding: 0 20px;
    background-color: rgba(110, 0, 255, 1);
    border: 1px solid rgba(110, 0, 255, 1);
    border-radius: 35px;
    -moz-box-shadow: none;
    -webkit-box-shadow: none;
    box-shadow: none;
    font-weight: 650;
    font-size: 14px;
    color: #FFFFFF;
    text-align: center;
  }
}

.register-tip {
  font-weight: 400;
  color: #8494A9;
  font-size: 12px;
  margin-top: 20px;
}

.register-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.register-code-img {
  height: 38px;
}
</style>
