<template>
  <div class="bg-F5">
    <div class="topDiv">
      <div class="container">
        <span class="title">{{ $t("title") }}</span>
        <span class="line"></span>
        <span class="info">{{ $t("page.views.account.login.topTitle") }}</span>
      </div>
    </div>
    <div class="bg">
      <div class="container  ">
        <img :src="require(`@assets/loginbg_${$i18n.locale}.png`)" />
        <div class="loginDiv">
          <div class="x-title">{{ $t("page.views.account.login.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="
                  `${$t(`inputPlaceholder`)}${$t(`form.loginAccount`)}`
                "
              />
            </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="
                  `${$t(`inputPlaceholder`)}${$t(`form.loginPassword`)}`
                "
                show-password
              />
            </el-form-item>
            <el-form-item class="formItem " prop="validateCode">
              <img class="icon" src="@assets/yz.png" alt="" />
              <el-input
                v-model="form.validateCode"
                prefix-icon="icon-yzm"
                :placeholder="
                  `${$t(`inputPlaceholder`)}${$t(`form.validateCode`)}`
                "
                class="yzm"
              />
              <img
                class="verifyImg"
                :src="verifyImg"
                @click="setVerifyImg"
                alt=""
              />
            </el-form-item>
            <el-form-item class="formItem autoLogin " prop="autoLogin">
              <el-checkbox v-model="form.autoLogin">{{
                $t("form.autoLogin")
              }}</el-checkbox>
            </el-form-item>
            <el-button class="loginBtn" @click="validate">{{
              $t("form.login")
            }}</el-button>
            <div class="as">
              <router-link class="forgetPass" to="/findPassword">{{
                $t("route.findPassword")
              }}</router-link>
              <span class="line"></span>
              <router-link class="register" to="/register">{{
                $t("route.register")
              }}</router-link>
            </div>
          </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";
import i18n from "@/lang";
@Component
export default class extends Vue {
  private verifyImg = "";
  private form: ILogin = {
    loginAccount: "",
    loginPassword: "",
    validateCode: "",
    autoLogin: false
  };
  private rules = {
    loginAccount: [
      {
        required: true,
        message: `${i18n.t(`inputPlaceholder`)}${i18n.t(`form.loginAccount`)}`,
        trigger: "blur"
      }
    ],
    loginPassword: [
      {
        required: true,
        message: `${i18n.t(`inputPlaceholder`)}${i18n.t(`form.loginPassword`)}`,
        trigger: "blur"
      }
    ],
    validateCode: [
      {
        required: true,
        message: `${i18n.t(`inputPlaceholder`)}${i18n.t(`form.validateCode`)}`,
        trigger: "blur"
      }
    ]
  };
  created() {
    this.setVerifyImg();
  }
  private setVerifyImg() {
    this.verifyImg = `${
      this.$basePath
    }/captcha/info/captchaImage?t=${Date.now()}`;
  }
  validate() {
    (this.$refs.form as ElForm).validate(x => x && this.submit());
  }

  async submit() {
    const token = await user.setToken(this.form);
    if (!token) return this.setVerifyImg();
    this.$message.success(`${this.$i18n.t("message.success.login")}`);
    (this.$refs.form as ElForm).resetFields();
    this.$router.push("/user");
  }
}
</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: 15px;
            position: relative;
            .icon {
              position: absolute;
              left: 10px;
              top: 50%;
              transform: translateY(-50%);
              z-index: 1;
              width: 12px;
            }
            &.autoLogin {
              height: auto;
            }
          }
          .loginBtn {
            width: 100%;
            background: #fd5522;
            color: #fff;
            border-color: #fd5522;
          }
          .as {
            margin-top: 15px;
            font-size: 1.2rem;
            text-align: right;
            .line {
              height: 1rem;
              width: 0.1rem;
              background: #e5e5e5;
              margin: 0 1rem;
              vertical-align: middle;
              display: inline-block;
            }
            .forgetPass {
              color: #666;
            }
            .register {
              color: #fd5522;
            }
          }
        }
      }
    }
  }
  .yzm {
    width: 150px;
  }
  .verifyImg {
    height: 40px;
    width: 108px;
    margin-left: 10px;
    vertical-align: middle;
  }
}
</style>
