<template>
  <div class="register-container">
    <article class="header">
      <header>
        <el-avatar icon="el-icon-user-solid" shape="circle" />
        <span class="login">
          <em class="bold">已有账号？</em>
          <a href="/login">
            <el-button type="primary" size="mini">登录</el-button>
          </a>
        </span>
      </header>
    </article>
    <section>
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
        autocomplete="off"
        :hide-required-asterisk="true"
        size="medium"
      >
        <div class="picture">
          <!-- 力山特起重机智能运维系统用户注册中心 -->
          <img src="./picture/picture.jpg">
        </div>
        <div class="title">
          力山特起重机智能运维系统用户注册中心
        </div>
        <div class="website">
          <a ref="url" href="https://lstzn.com/">公司官网</a>
        </div>
        <div class="contact">
          <a ref="ee" href="https://lstzn.com/">联系我们</a>
        </div>
        <div class="login_small">
          <a ref="we">
            关于我们
          </a>
        </div>
        <div style="padding-top: 60px">
          <el-form-item label="用户名" prop="username" :rules="username">
            <el-col :span="10">
              <el-input
                v-model="ruleForm.username"
                placeholder="请输入用户名"
              />
            </el-col>
          </el-form-item>
          <!-- <el-form-item label="姓名" prop="name">
            <el-col :span="10">
              <el-input v-model="ruleForm.name" placeholder="请输入姓名" />
            </el-col>
          </el-form-item>
          <el-form-item label="身份证号" prop="IdCard" :rules="rules" :model="headerForm">
            <el-col :span="10">
              <el-input
                v-model="ruleForm.IdCard"
                placeholder="身份证号"
                maxlength="16"
                minlength="16"
              />
            </el-col>
          </el-form-item>
          <el-form-item label="部门" prop="department">
            <el-col :span="10">
              <el-input v-model="ruleForm.IdCard" placeholder="部门" />
            </el-col>
          </el-form-item>
          <el-form-item label="职位" prop="position">
            <el-col :span="10">
              <el-input v-model="ruleForm.position" placeholder="职位" />
            </el-col>
          </el-form-item> -->

          <el-form-item label="密码" prop="pwd">
            <el-col :span="10">
              <el-input
                v-model="ruleForm.pwd"
                placeholder="密码必须是数字、字母组合,长度8~20位"
                type="password"
                maxlength="8"
                minlength="20"
              />
            </el-col>
          </el-form-item>
          <el-form-item label="确认密码" prop="cpwd">
            <el-col :span="10">
              <el-input
                v-model="ruleForm.cpwd"
                placeholder="请再次输入密码"
                type="password"
              />
            </el-col>
          </el-form-item>
          <!-- <el-form-item label="手机号" prop="phoneId">
            <el-col :span="10">
              <el-input
                v-model="ruleForm.phoneId"
                placeholder="输入手机号并点击发送验证码"
              />
            </el-col>
            <el-button
              :loading="codeLoading"
              :disabled="isDisable"
              size="small"
              round
              @click="sendMsg"
            >发送验证码不可用</el-button>

            <span class="status">{{ statusMsg }}</span>
          </el-form-item> -->
          <el-form-item>
            <el-button
              type="primary"
              style="width: 40%"
              @click="register"
            >注册</el-button>
          </el-form-item>
        </div>
      </el-form>
    </section>

    <div class="error">{{ error }}</div>
  </div>

</template>

<script>
import request from '@/utils/request'
export default {
  name: 'Register',
  data() {
    return {
      statusMsg: '',
      error: '',
      isDisable: false,
      codeLoading: false,
      ruleForm: {
        username: '',
        name: '',
        IdCard: '',
        department: '',
        position: '',
        phoneId: '',
        code: '',
        pwd: '',
        cpwd: ''
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        name: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        IdCard: [
          { required: true, message: '身份证号不能为空', trigger: 'blur' }
        ],
        department: [
          { required: true, message: '部门不能为空', trigger: 'blur' }
        ],
        position: [
          { required: true, message: '职位不能为空', trigger: 'blur' }
        ],
        phoneId: [
          {
            required: true,
            type: 'phoneId',
            message: '请输入手机号',
            trigger: 'blur'
          }
        ],
        code: [
          {
            required: true,
            type: 'string',
            message: '请输入验证码',
            trigger: 'blur'
          }
        ],
        pwd: [
          {
            required: true,
            message: '创建密码',
            trigger: 'blur'
          },
          {
            pattern: /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,20}$/,
            message: '密码必须同时包含数字与字母,且长度为 8-20位'
          }
        ],
        cpwd: [
          {
            required: true,
            message: '确认密码',
            trigger: 'blur'
          },
          {
            validator: (rule, value, callback) => {
              if (value === '') {
                callback(new Error('请再次输入密码'))
              } else if (value !== this.ruleForm.pwd) {
                callback(new Error('两次输入密码不一致'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    // 用户注册
    register() {
      if (this.form.password !== this.form.confirm) {
        this.$message({
          type: 'error',
          message: '2次密码输入不一致！'
        })
        return
      }

      this.$refs['form'].validate((valid) => {
        if (valid) {
          request.post('/user/register', this.form).then(res => {
            if (res.code === '0') {
              this.$message({
                type: 'success',
                message: '注册成功'
              })
              this.$router.push('/login') // 登录成功之后进行页面的跳转，跳转到主页
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
    // register: function() {
    //   this.$refs['ruleForm'].validate((valid) => {
    //     if (valid) {
    //       const user = {
    //         // code: this.ruleForm.code,
    //         // phoneId: this.ruleForm.phoneId
    //         // password: encrypt(this.ruleForm.pwd)
    //       }
    //       // eslint-disable-next-line no-undef
    //       register(user)
    //         .then((res) => {
    //           console.log(res)
    //           this.$message({
    //             showClose: true,
    //             message: '注册成功，正在跳转到登录界面...',
    //             type: 'success'
    //           })
    //           setTimeout(() => {
    //             this.$router.push('/')
    //           }, 2000)
    //         })
    //         .catch((err) => {
    //           console.log(err.response.data.message)
    //         })
    //     }
    //   })
    // }
  }
}
</script>

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

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

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

/* reset element-ui css */
.register-container {
  .el-input {
    display: inline-block;
    height: 1px;
    width: 100%;

    input {
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      border: 1px solid rgba(255, 255, 255, 0.1);
      -webkit-appearance: none;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

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

  .el-form-item {
    label {
      font-style: normal;
      font-size: 12px;
      color: $light_gray;
    }
  }
}
</style>

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

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

  .header {
    border-bottom: 2px solid rgb(235, 232, 232);
    min-width: 980px;
    color: #666;
    header {
      margin: 0 auto;
      margin-top: 0px;
      padding: 10px 0;
      width: 980px;

      .login {
        float: right;
        margin-top: 7px;
      }

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

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

    .yello{
      color: #1890ff

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

    .picture {
      position: fixed;
      margin-top: 120px;
      margin-left: 600px;
    }

    .website {
      position: fixed;
      margin-left: 400px;
      margin-top: 350px;
      margin-left: 690px;
      font-size: 14px;
      color: #fff;
    }

    .login_small {
      position: fixed;
      margin-left: 400px;
      margin-top: 410px;
      margin-left: 690px;
      font-size: 14px;
      color: #fff;
    }
    .contact {
      position: fixed;
      margin-left: 400px;
      margin-top: 380px;
      margin-left: 690px;
      font-size: 14px;
      color: #fff;
    }

    .title {
      position: fixed;
      margin-top: 0px;
      margin-left: 245px;
      font-size: 24px;
      color: #fff;
    }
    .tips_center {
      position: fixed;
      margin-top: 120px;
      margin-left: 800px;
      font-size: 24px;
      color: #fff;
    }

    .error {
      color: red;
    }
  }

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

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
}
</style>

<style scoped>
/* 修改验证器样式 */
::v-deep .el-form-item.is-error .el-input__inner {
  border-color: #889aa4;
}
::v-deep .el-form-item.is-error .el-input__validateIcon {
  color: #889aa4;
}
::v-deep .el-form-item__error {
  color: #e6a23c;
}
</style>
