<template>
  <div class="bg-F5">
    <div class="topDiv">
      <div class="container">
        <span class="title">中国钢制家具产业聚集网</span>
        <span class="line"></span>
        <span class="info">用户注册</span>
      </div>
    </div>
    <div class="bg">
      <div class="container  ">
        <img src="@assets/loginbg.png" />
        <div class="loginDiv">
          <div class="x-title">用户注册</div>
          <el-form :model="form" :rules="rules" class="form" ref="form">
            <el-form-item class="formItem" prop="loginAccount">
              <img class="icon" src="@assets/user.png" alt="" />
              <el-input
                v-model="form.loginAccount"
                prefix-icon="x-el-icon-user"
                placeholder="登录账号"
              />
            </el-form-item>
            <el-form-item class="formItem" prop="loginPassword">
              <img class="icon" src="@assets/lock.png" alt="" />
              <el-input
                v-model="form.loginPassword"
                prefix-icon="x-el-icon-lock"
                placeholder="登录密码"
              />
            </el-form-item>
            <el-form-item class="formItem" prop="phone">
              <img class="icon" src="@assets/phone.png" alt="" />
              <el-input
                v-model="form.phone"
                prefix-icon="x-el-icon-lock"
                placeholder="手机号码"
              />
            </el-form-item>
            <el-form-item class="formItem yzm" prop="validateCode">
              <img class="icon" src="@assets/yz.png" alt="" />
              <el-input
                v-model="form.validateCode"
                prefix-icon="icon-yzm"
                placeholder="验证码"
              />
              <div class="getyzm">获取验证码</div>
            </el-form-item>
            <el-form-item class="formItem check " prop="check">
              <el-checkbox v-model="form.check">我已阅读并同意</el-checkbox
              ><a class="xieyi" href="javascript:;"
                >《用户服务协议和平台免责条款》</a
              >
            </el-form-item>

            <el-button class="loginBtn" @click="validate">注册</el-button>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>
<script lang="ts">
import { Component, Vue, Watch } from "vue-property-decorator";
import user from "@/store/modules/user";
import { RegAccount } from "@/utils";
import { ElForm } from "element-ui/types/form";
@Component
export default class extends Vue {
  private verifyImg = "";
  private form: ILogin = {
    loginAccount: "",
    loginPassword: "",
    validateCode: "",
    autoLogin: false
  };
  private rules = {
    loginAccount: [
      { required: true, message: "请输入用户名", trigger: "blur" },
      {
        validator: (rule: any, value: string, callback: any) =>
          callback(
            RegAccount.test(value)
              ? undefined
              : new Error("用户名必须为9到16位由数字字母组合")
          ),
        trigger: "blur"
      }
    ],
    loginPassword: [
      { required: true, message: "请输入密码", trigger: "blur" },
      {
        validator: (rule: any, value: string, callback: any) =>
          callback(
            RegAccount.test(value)
              ? undefined
              : new Error("密码必须为9到16位由数字字母组合")
          ),
        trigger: "blur"
      }
    ],
    phone: [
      { required: true, message: "请输入手机号码", trigger: "blur" },
      {
        validator: (rule: any, value: string, callback: any) =>
          callback(
            /^1[3|4|5|7|8][0-9]\d{8}$/.test(value)
              ? undefined
              : new Error("请输入正确的手机号")
          ),
        trigger: "blur"
      }
    ],
    validateCode: [
      { required: true, message: "请输入验证码", trigger: "blur" }
    ],
    check: [
      {
        validator: (rule: any, value: boolean, callback: any) =>
          callback(value ? undefined : new Error("请同意免责条款")),
        trigger: "blur"
      }
    ]
  };

  validate() {
    (this.$refs.form as ElForm).validate(x => x && this.submit());
  }

  async submit() {
    const [err] = await this.$post("/member/info/userRegister", this.form);
    if (err) return;
    this.$message.success("注册成功 请登陆");
    (this.$refs.form as ElForm).resetFields();
    this.$router.push("/login");
  }
}
</script>
<style lang="scss" scoped>
.bg-F5 {
  .topDiv {
    height: 9rem;
    line-height: 9rem;
    background: #fff;
    .title {
      font-size: 2.2rem;
      font-weight: bold;
      color: #fd5522;
    }
    .line {
      display: inline-block;
      height: 3rem;
      width: 0.1rem;
      background: #e5e5e5;
      margin: 0 2rem;
      vertical-align: middle;
    }
    .info {
      font-size: 1.8rem;
      font-weight: bold;
      color: #666;
    }
  }

  .bg {
    background: #84ccc9;
    padding: 75px 0;
    .container {
      overflow: hidden;
      > img {
        margin-left: 180px;
        margin-top: 50px;
        float: left;
      }
      .loginDiv {
        // width: 320px;
        // height: 338px;
        margin-left: 218px;
        float: left;
        background: #fff;

        box-sizing: border-box;
        border-radius: 0.4rem;
        box-shadow: 2px 2px 12px #ddd;
        .x-title {
          margin-top: 20px;
        }
        .form {
          padding: 15px 20px;
          .formItem {
            height: 40px;
            line-height: 40px;
            box-sizing: border-box;
            border-radius: 4px;
            margin-bottom: 20px;
            position: relative;
            .icon {
              position: absolute;
              left: 10px;
              top: 50%;
              transform: translateY(-50%);
              z-index: 1;
              width: 12px;
            }
            &.check {
              height: auto;
            }
          }
          .loginBtn {
            width: 100%;
            background: #fd5522;
            color: #fff;
            border-color: #fd5522;
          }
        }
      }
      .getyzm {
        height: 40px;
        line-height: 40px;
        width: 100px;
        font-size: 14px;
        color: #fd5522;
        text-align: center;
        float: right;
        background: #ffded5;
        border-radius: 0.4rem;
        cursor: pointer;
      }
      .xieyi {
        color: #fd5522;
      }
    }
  }
}
</style>
<style lang="scss">
.formItem.yzm {
  .el-input {
    width: 200px;
  }
  .verifyImg {
    height: 40px;
    width: 108px;
    margin-left: 10px;
    vertical-align: middle;
  }
}
</style>
