<template>
  <div class="register">
    <div class="register-form" v-if="!isUpdate">
      <h1 class="title">注册</h1>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="form"
        label-position="top"
        hide-required-asterisk
        @submit.native.prevent
      >
        <el-form-item prop="role" style="text-align: center">
          <el-radio-group v-model="ruleForm.role">
            <el-radio label="1">我是学生</el-radio>
            <el-radio label="2">我是老师</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="school">
          <el-select v-model="ruleForm.school" placeholder="选择学校" style="width: 100%">
            <el-option v-for="item in schoolList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="college">
          <el-input v-model="ruleForm.college" placeholder="学院名称"></el-input>
        </el-form-item>
        <el-form-item prop="major">
          <el-input v-model="ruleForm.major" placeholder="专业名称"></el-input>
        </el-form-item>
        <el-form-item label="请选择证书等级" prop="grade">
          <el-checkbox-group v-model="ruleForm.grade">
            <el-checkbox label="初级"></el-checkbox>
            <el-checkbox label="中级"></el-checkbox>
            <el-checkbox label="高级"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item prop="real_name">
          <el-input v-model="ruleForm.real_name" placeholder="真实姓名"></el-input>
        </el-form-item>
        <el-form-item prop="id_number">
          <el-input v-model="ruleForm.id_number" placeholder="身份证号码" maxlength="18"></el-input>
        </el-form-item>
        <el-form-item prop="email">
          <el-input v-model="ruleForm.email" placeholder="常用邮箱"></el-input>
        </el-form-item>
        <el-form-item prop="mobile">
          <el-input v-model="ruleForm.mobile" placeholder="手机号" maxlength="11"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input v-model="ruleForm.code" placeholder="验证码" maxlength="4">
            <countdown
              slot="suffix"
              size="mini"
              :disabled="disabledSend"
              @start="sendCodeRequest"
              ref="countdown"
            ></countdown>
          </el-input>
        </el-form-item>
        <el-button type="primary" class="submit-button" :loading="loading" @click="handleSubmit">注册</el-button>
      </el-form>
    </div>

    <div class="register-form" v-else>
      <h1 class="title">设置用户名和密码</h1>
      <el-form
        size="medium"
        :model="ruleForm"
        :rules="rules"
        ref="updateForm"
        label-position="top"
        hide-required-asterisk
        @submit.native.prevent
      >
        <el-form-item label="设置用户名" prop="username">
          <el-input v-model="ruleForm.username" placeholder="6-20个字符" maxlength="20"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password" placeholder="密码" maxlength="20"></el-input>
        </el-form-item>
        <el-button type="primary" class="submit-button" :loading="loading" @click="handleUpdate">完成</el-button>
      </el-form>
    </div>
    <p class="back">
      <router-link to="login">直接登录</router-link>
    </p>
  </div>
</template>

<script>
import * as api from '@/api/account'
import countdown from '@/components/countdown.vue'

export default {
  name: 'Register',
  components: { countdown },
  data() {
    return {
      title: '用户注册',
      ruleForm: {
        role: '1',
        school: '',
        college: '',
        major: '',
        grade: [],
        real_name: '',
        id_number: '',
        email: '',
        mobile: '',
        code: '',
        username: '',
        password: ''
      },
      rules: {
        school: [{ required: true, message: '请选择学校', trigger: 'change' }],
        college: [{ required: true, message: '请输入学院名称', trigger: 'blur' }],
        major: [{ required: true, message: '请输入专业名称', trigger: 'blur' }],
        grade: [{ required: true, message: '请选择证书等级', trigger: 'change' }],
        real_name: [{ required: true, message: '请输入真实姓名', trigger: 'blur' }],
        id_number: [{ required: true, message: '请输入身份证号码', trigger: 'blur' }],
        email: [{ required: true, type: 'email', message: '请输入邮箱', trigger: 'blur' }],
        mobile: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 6, max: 20, message: '长度为6-20个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度为6-20个字符', trigger: 'blur' }
        ]
      },
      schoolList: [],
      loading: false,
      isUpdate: false
    }
  },
  computed: {
    disabledSend() {
      const value = this.ruleForm.mobile
      return !(/^1[3-9]\d{9}$/.test(value) || /@/.test(value))
    }
  },
  methods: {
    // 提交
    handleSubmit() {
      this.$refs.form.validate().then(this.handleRequest)
    },
    // 接口请求
    handleRequest() {
      this.loading = true
      const params = Object.assign({}, this.ruleForm)
      params.grade = this.ruleForm.grade.join(',')
      api
        .xlmsRegister(params)
        .then(response => {
          if (response.code === 0) {
            this.$message({ message: '注册成功', type: 'success' })
            this.isUpdate = true
          } else {
            this.$message.error(response.msg)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    // 验证码
    sendCodeRequest() {
      api
        .sendCode({ account: this.ruleForm.mobile })
        .then(response => {
          console.log(response)
          if (response.code === 0) {
            this.$message({ type: 'success', message: '验证码已发送' })
          } else {
            // 停止计时
            this.$refs.countdown.stop()
            this.$message({ type: 'error', message: response.msg })
          }
        })
        .catch(this.$refs.countdown.stop)
    },
    getSchool() {
      api.getSchool().then(response => {
        this.schoolList = response
      })
    },
    handleUpdate() {
      this.$refs.updateForm.validate().then(this.updateUser)
    },
    updateUser() {
      const params = {
        username: this.ruleForm.username,
        password: this.ruleForm.password
      }
      api.updateUser(params).then(response => {
        if (response.code === 0) {
          const { url } = webConf.others.xlearn
          window.location.href = url
        } else {
          this.$message.error(response.msg)
        }
      })
    }
  },
  created() {
    this.$parent.show = false
  },
  beforeMount() {
    this.getSchool()
  },
  destroyed() {
    this.$parent.show = true
  }
}
</script>

<style lang="scss" scoped>
.register {
  padding: 0 100px;
}
::v-deep .register-form .el-form-item--medium .el-form-item__label {
  line-height: 1;
}
.title {
  margin-bottom: 20px;
  text-align: center;
}
.back {
  margin-top: 10px;
  color: #007bad;
  text-align: right;
  a {
    color: currentColor;
    text-decoration: none;
  }
}
</style>
