<template>
  <div>
    <div style="border: none">
      <div class="container">
        <div class="title">账号密码登录</div>
        <div class="line"></div>
        <el-form
          :model="loginForm"
          :rules="loginFormRules"
          ref="loginFormRef"
          class="demo-ruleForm"
        >
          <el-form-item prop="phone">
            <img
              class="img_l"
              src="../../../assets/img/login/username.png"
              alt=""
            />
            <el-input
              v-model="loginForm.phone"
              placeholder="请输入手机号"
              maxlength="20"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <img
              class="img_l"
              src="../../../assets/img/login/password.png"
              alt=""
            />
            <el-input
              v-model="loginForm.password"
              :type="isShowPwd ? 'password' : 'text'"
              maxlength="20"
              placeholder="请输入密码"
            ></el-input>
            <img
              @click="changePwdStatus()"
              v-if="!isShowPwd"
              class="img_r"
              src="../../../assets/img/login/eye01.png"
              alt=""
            />
            <img
              @click="changePwdStatus()"
              v-if="isShowPwd"
              class="img_r"
              src="../../../assets/img/login/eye01.png"
              alt=""
            />
          </el-form-item>
          <el-form-item style="margin: 40px 0 0 0"></el-form-item>
          <el-form-item>
            <el-button @click="submitForm()">登 录</el-button>
          </el-form-item>
          <div class="box">
            <div class="fr" @click="register()">还没有账号？立即注册</div>
            <div class="fl" @click="forgetPwd()">忘记密码？</div>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
/* eslint-disable */

import { login } from "r/index/login";
import { mobileCheck } from "@/common/utils.js";

export default {
  name: "index-login",
  data() {
    return {
      isShowPwd: true, // 控制密码显示隐藏
      loginForm: {
        phone: "",
        password: "",
      },
      // 表单验证规则对象
      loginFormRules: {
        // 对手机号进行校验
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            validator: function (rule, value, callback) {
              if (mobileCheck(value) === false) {
                callback(new Error("请输入正确的手机号"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        // 对密码进行校验
        password: [
          {
            required: true,
            message: "请输入密码",
            transform: (value) => value,
            trigger: "blur",
          },
          {
            type: "string",
            message: "请输入不包含空格的字符",
            trigger: "blur",
            transform(value) {
              if (value && value.indexOf(" ") === -1) {
                return value;
              } else {
                return false;
              }
            },
          },
          {
            trigger: "blur",
            validator: (rule, value, callback) => {
              var passwordreg = /^[a-zA-Z0-9]{6,12}$/;
              // /(?=.*\d)(?=.*[a-zA-Z]).{6,12}/;
              if (!passwordreg.test(value)) {
                callback(new Error("请输入6-12位，包含数字/字母,"));
              } else {
                callback();
              }
            },
          },
        ],
      },
    };
  },
  created() {
    this.$emit("getStatus", false);
  },
  methods: {
    // 忘记账号/密码
    forgetPwd() {
      this.$router.replace({ name: "index-reset" });
    },

    // 去注册
    register() {
      this.$router.replace({ name: "index-register" });
    },
    // 改变密码显示状态
    changePwdStatus() {
      this.isShowPwd = !this.isShowPwd;
    },
    // 登录
    submitForm() {
      this.$refs["loginFormRef"].validate((valid) => {
        if (!valid) return;
        login({
          phone: this.loginForm.phone,
          password: this.loginForm.password,
        }).then((res) => {
          console.log(res, "login");
          if (res.data.code != 200) {
            return this.$message.error(res.data.message);
          }
          this.$message.success("登录成功");
          // token存储
          window.localStorage.setItem("index-token", res.data.token);
          window.localStorage.setItem("phone", this.loginForm.phone);
          // 报名信息
          window.localStorage.setItem("camp_name", res.data.cam.name);
          //获取主题色
          window.localStorage.setItem(
            "system_color",
            res.data.cam.system_color
          );
          // 跳转到报名
          this.$router.push({ path: "/signUp/examInfo" });
        });
      });
    },
  },
};
</script>

<style scoped lang="scss">
// 内容区域
.box_content {
  .content {
    max-width: 1150px;
    margin: 40px auto;
    padding: 0 50px;
    overflow: auto;
    .floatL {
      float: left;
      width: 670px;
      h3 {
        font-size: 20px;
        color: #4c4c4c;
        font-weight: inherit;
        padding-bottom: 15px;
        margin-bottom: 23px;
        border-bottom: 4px solid #e0823d;
      }
      ul {
        li {
          height: 40px;
          line-height: 40px;
          border-bottom: 1px solid #ddd;
          a {
            display: inline-block;
            width: 100%;
            font-size: 16px;
            color: #34538b;
            text-decoration: none;
            &:hover {
              text-decoration: underline;
            }
          }
        }
      }
      .ContentLReg {
        height: 60px;
        color: #333333;
        font-size: 14px;
        span {
          cursor: pointer;
          color: #563279;
          font-weight: bold;
          &:hover {
            text-decoration: underline;
          }
        }
      }
      .img_box {
        width: 100%;
        height: 156px;
        text-align: center;
        img {
          &:nth-child(1) {
            margin-right: 40px;
          }
        }
      }
    }
    .floatR {
      float: right;
      width: 374px;
      ul {
        margin-top: 36px;
        margin-left: 87px;
        li {
          font-size: 16px;
          width: 100%;
          height: 45px;
          line-height: 45px;
          padding-left: 70px;
          margin-bottom: 35px;
          // &:nth-child(1) {
          //   background: url("../../../assets/images/weidu.png") no-repeat 0px
          //     center;
          // }
          // &:nth-child(2) {
          //   background: url("../../../assets/images/fangshi.png") no-repeat 0px
          //     center;
          // }
          // &:nth-child(3) {
          //   background: url("../../../assets/images/xingtai.png") no-repeat 0px
          //     center;
          // }
        }
      }
    }
  }
}
.container {
  width: 440px;
  // height: 640px;
  background: #ffffff;
  box-shadow: 0px 1px 18px 0px rgba(0, 0, 0, 0.06);
  border-radius: 8px;
  position: absolute;
  top: 50%;
  right: 18%;
  // left: 10%;
  transform: translate(0, -50%);
  padding: 60px;
  margin-top: 0;
  // background: url("../../../assets/images/loginbgp.png") no-repeat 0 center;
  overflow: hidden;
  .title {
    margin: 19px 0 6px 0;
    width: 132px;
    height: 20px;
    font-size: 22px;
    font-weight: 500;
    color: var(--color);
    line-height: 20px;
  }
  .line {
    width: 125px;
    height: 3px;
    background: var(--color);
    border-radius: 2px;
  }
  .el-form {
    margin-top: 60px;
    .el-form-item {
      position: relative;
      margin-bottom: 30px;
      &:last-child {
        margin: 0;
      }

      img.img_l {
        position: absolute;
        left: 25px;
        top: 50%;
        transform: translate(-50%, -50%);
        z-index: 1000;
      }
      img.img_r {
        position: absolute;
        right: 15px;
        top: 50%;
        transform: translate(0, -50%);
        z-index: 1000;
      }
      ::v-deep .el-input__inner {
        height: 50px;
        line-height: 50px;
        padding: 0 10px 0 50px;
        border-radius: 0;
        color: #4d4d4d;
        border: 0;
        border-bottom: 2px solid #d9d9d9;
        &:focus {
          border-bottom-color: #563279;
        }
      }
      ::v-deep .el-form-item__error {
        color: #e0823d;
      }
      .el-button {
        width: 100%;
        height: 60px;
        background: var(--color);
        border-radius: 4px;
        opacity: 0.8;
        line-height: 50px;
        text-align: center;
        color: #fff;
        font-size: 16px;
        padding: 0;
        margin: 30px 0 0 0;
      }
    }
    .box {
      height: 18px;
      display: block;
      .fl {
        float: right;
        height: 18px;
        line-height: 18px;
        color: #999999;
        margin-top: -10px;
        cursor: pointer;
        &:hover {
          color: #563279;
          text-decoration: underline;
        }
      }
      .fr {
        float: left;
        height: 18px;
        line-height: 18px;
        color: #999999;
        margin-top: -10px;
        cursor: pointer;
        &:hover {
          color: #563279;
          text-decoration: underline;
        }
      }
    }
  }
}
@media screen and (max-width: 1601px) {
  .container {
    right: 18%;
  }
}
@media screen and (max-width: 1501px) {
  .container {
    right: 16%;
  }
}
@media screen and (max-width: 1401px) {
  .container {
    right: 14%;
  }
}
</style>
