<template>
  <div class="container">
    <div class="register-main">
      <div class="header-title">
        <span class="left-text">完善信息</span>
      </div>
      <el-row :gutter="10" class="register-body">
        <el-col
          :xs="24"
          :sm="12"
          :md="12"
          :lg="12"
          class="el-col-sm-push-6 el-col-md-push-6 el-col-lg-push-6"
        >
          <el-form
            ref="account"
            :model="account"
            :rules="rules"
            label-width="100px"
          >
            <el-form-item prop="username" label="用户名：">
              <el-input
                type="text"
                v-model="account.username"
                auto-complete="off"
                :autofocus="true"
                placeholder="请输入用户名"
              ></el-input>
            </el-form-item>
            <el-form-item prop="phone" label="手机号：">
              <el-input
                type="text"
                v-model="account.phone"
                auto-complete="off"
                :autofocus="true"
                placeholder="请输入手机号"
              ></el-input>
            </el-form-item>
            <el-form-item prop="email" label="邮箱：">
              <el-input
                type="text"
                v-model="account.email"
                auto-complete="off"
                :autofocus="true"
                placeholder="请输入邮箱"
                @change="time = 0"
              ></el-input>
            </el-form-item>
            <el-form-item style="margin-bottom: 0; width: 100%">
              <el-button
                type="primary"
                style="width: 100%"
                @click.native.prevent="handleModify"
                :loading="loading"
                >确定</el-button
              >
            </el-form-item>
            <el-form-item class="extra-text">
              <router-link
                :to="{ path: '/login' }"
                class="login-text"
                title="立即登录"
                >返回登录</router-link
              >
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
  </div>
</template>
<script>
import { ajax as request } from "@/request.js";
export default {
  data() {
    var validemail = (rule, value, callback) => {
      var regEmail =
        /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
      if (!regEmail.test(value)) {
        callback(new Error("邮箱格式不正确"));
      } else {
        callback();
      }
    };
    return {
      loading: false,
      account: {
        username: "",
        phone: "",
        email: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 5, max: 8, message: "长度在 5 到 8 个字符", trigger: "blur" },
        ],
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { min: 11, max: 11, message: "请输入11位手机号", trigger: "blur" },
        ],
        email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
          { validator: validemail, trigger: "blur" },
        ],
      },
    };
  },
  created() {},
  methods: {
    handleModify() {
      this.$refs.account.validate((valid) => {
        if (valid) {
          let that = this;
          request({
            url: "/user/update",
            method: "post",
            params: {
              username: that.account.username,
              phone: that.account.phone,
              email: that.account.email,
            },
          }).then(function (response) {
            //获取数据存入config
            if (response.data.success == true) {
              that.$router.push({ path: "/login" });
              that.$notify.success({
                title: "提示",
                message: "成功！",
              });
            } else {
              that.$notify.error({
                title: "提示",
                message: response.data.message,
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  watch: {
    account: {
      handler() {
        if (this.account.phone != "") {
          var reg = /\D/;
          if (reg.test(this.account.phone)) {
            this.$message.info("手机号只可为数字");
          }
          this.account.phone = this.account.phone.replace(/\D/, "");
        }
      },
      deep: true,
    },
  },
};
</script>
<style lang="scss" scoped>
#app {
  margin-top: 0 !important;
}
.container {
  position: fixed;
  height: 100%;
  width: 100%;
  background-color: #2d3a4b;
}
.register-main {
  margin: 200px auto;
  padding: 18px;
  max-width: 836px;
  min-width: 280px;
  background-clip: padding-box;
  background: #f2f6fc;
  border: 1px solid #eaeaea;

  background: -ms-linear-gradient(top, #ace, #00c1de); /* IE 10 */
  background: -moz-linear-gradient(top, #ace, #00c1de); /*火狐*/
  background: -webkit-gradient(
    linear,
    0% 0%,
    0% 100%,
    from(#ace),
    to(#00c1de)
  ); /*谷歌*/
  background: -webkit-linear-gradient(
    top,
    #ace,
    #00c1de
  ); /*Safari5.1 Chrome 10+*/
  background: -o-linear-gradient(top, #ace, #00c1de); /*Opera 11.10+*/
}

.header-title {
  position: relative;
  padding: 15px 10px 15px;
  border-bottom: 5px solid #ccc;

  .left-text {
    font-size: 1.7rem;
    color: #444;
  }

  .right-text {
    position: absolute;
    top: 18px;
    right: 10px;
    font-size: 1.28rem;
  }
}

.register-body {
  padding: 40px 3%;
}
.extra-text {
  position: relative;
  margin-bottom: 0;
  padding-left: 2px;
}

.login-text {
  position: absolute;
  top: 4px;
  right: 2px;
  font-size: 12px;
  color: #aaa;
}

@media all and (max-width: 768px) {
  .register-main {
    margin: 0 auto 24px;
  }
}
</style>
