<template>
  <div class="login-container">
    <div class="login-form-main">
      <img src="@/assets/sheji/loginlan.png" alt="" />
      <div class="main-title">
        <h1>欢迎登录</h1>
        <h3>采销平台供应商端</h3>
      </div>
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        autocomplete="on"
        label-position="left"
      >
        <div class="title-container">
          <h3 class="title">
            采销平台供应商端<span>{{ ver }}</span>
          </h3>
        </div>

        <el-form-item prop="mobile">
          <span class="svg-container">
            <i class="el-icon-user"></i>
          </span>
          <el-input
            ref="mobile"
            v-model="loginForm.mobile"
            placeholder="手机号"
            name="mobile"
            type="text"
            tabindex="1"
            maxlength="11"
            autocomplete="on"
          />
        </el-form-item>

        <el-tooltip
          v-model="capsTooltip"
          content="Caps lock is On"
          placement="right"
          manual
        >
          <el-form-item prop="password">
            <span class="svg-container">
              <i class="el-icon-unlock"></i>
            </span>
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="密码"
              name="password"
              tabindex="2"
              autocomplete="on"
              maxlength="16"
              @keyup.native="checkCapslock"
              @blur="capsTooltip = false"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>
        </el-tooltip>

        <el-button
          :loading="loading"
          :disabled="loading"
          type="primary"
          style="width: 100%; margin-top: 5px"
          @click.native.prevent="handleLogin()"
          >登&nbsp;&nbsp;&nbsp;&nbsp;录</el-button
        >

        <div style="position: relative; padding: 12px 0 0 0">
          <div class="tips" style="float: left">
            <el-popover
              placement="top"
              title="初始密码："
              width="200"
              trigger="click"
              content="请联系管理员"
            >
              <i class="el-icon-info" slot="reference"></i>
            </el-popover>
          </div>
          <!-- <router-link to="/forget-password">
            <div class="tips" style="float: right">
              <span>忘记密码</span>
            </div>
          </router-link> -->
        </div>
      </el-form>
    </div>
    <div class="beian">
      <span>万宇恒通</span>
      <a target="_blank" href="https://beian.miit.gov.cn/">京ICP备2020033956号-1</a>
      <span> </span>
    </div>
  </div>
</template>

<script>
import resToken from "@/mixins/resToken";
import { isnumber, isAlphanumeric, validAlphabets, isMobile } from "@/utils/validate";
export default {
  name: "Login",
  mixins: [resToken],
  data() {
    const validatemobile = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("手机号不能为空！"));
      } else {
        if (!isMobile(value)) {
          callback(new Error("请输入正确的手机号"));
        } else {
          callback();
        }
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("密码不能为空！"));
      } else {
        if (!isAlphanumeric(value)) {
          callback(new Error("密码为6-18位数字字母组合！"));
        } else if (value.length < 6 || value.length > 18) {
          callback(new Error("密码为6-18位数字字母组合！"));
        } else if (isnumber(value)) {
          callback(new Error("密码不能为纯数字！"));
        } else if (validAlphabets(value)) {
          callback(new Error("密码不能为纯字母！"));
        } else {
          callback();
        }
      }
    };
    return {
      ver: "",
      show: false,
      loginForm: {
        mobile: "",
        password: "",
      },
      loginRules: {
        mobile: [{ required: true, trigger: "blur", validator: validatemobile }],
        password: [{ required: true, trigger: "blur", validator: validatePassword }],
      },
      passwordType: "password",
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query;
        if (query) {
          this.redirect = query.redirect;
          this.otherQuery = this.getOtherQuery(query);
        }
      },
      immediate: true,
    },
  },
  mounted() {
    // console.log(process.env.NODE_ENV);
    // if (process.env.NODE_ENV === "development") {
    //   this.loginForm = {
    //     mobile: "17777777777",
    //     password: "uu2222",
    //   };
    // }
    if (this.loginForm.mobile === "") {
      this.$refs.mobile.focus();
    } else if (this.loginForm.password === "") {
      this.$refs.password.focus();
    }
  },
  methods: {
    setVisible(val) {
      this.show = val;
    },
    handleClick() {
      this.show = true;
    },
    handleSuccess() {
      this.show = false;
      this.handleLogin();
    },
    checkCapslock(e) {
      const { key } = e;
      this.capsTooltip = key && key.length === 1 && key >= "A" && key <= "Z";
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then((res) => {
              const { code, message } = res;
              if (code === 0) {
                // this.$router.push({
                //   path: this.redirect || "/",
                //   query: this.otherQuery,
                // });
                this.getMenu();
              } else {
                this.$message.warning(message);
                this.loading = false;
              }
            })
            .catch((err) => {
              console.log(err);
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    getMenu() {
      this.$store
        .dispatch("user/getMenuList", this)
        .then((res) => {
          if (res === "success") {
            console.log(res);
            window.vm.$router.push({
              path: this.redirect
                ? this.redirect
                : res === "success-dataV"
                ? "/bigScreen/datavScr"
                : "/welcome",
              query: this.otherQuery,
            });
          }

          this.loading = false;
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== "redirect") {
          acc[cur] = query[cur];
        }
        return acc;
      }, {});
    },
  },
};
</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)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #232323;
      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 {
    border: 1px solid #e9e9e9;
    // background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #232323;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #38c1e7;
$light_gray: #f2f2f2;

.login-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  min-width: 1000px;
  min-height: 700px;
  z-index: 2;
  zoom: 1;
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center 0;
  background-image: url("~@/assets/sheji/loginbg.png");
  text-align: center;
  .login-form-main {
    display: inline-block;
    width: 600px;
    height: 500px;
    margin: 26vh auto 0 auto;
    position: relative;
    img {
      width: 400px;
      height: 350px;
    }
    .main-title {
      position: absolute;
      top: 0;
      left: 0;
      z-index: 2;
      left: 170px;
      top: 125px;
      text-align: left;
      color: #fff;
      h1 {
        padding: 0 0 13px 0;
      }
      h3 {
        font-size: 22px;
      }
    }
    .login-form {
      position: absolute;
      width: 320px;
      top: -20px;
      left: 380px;
      z-index: 3;
      max-width: 100%;
      padding: 60px 28px 20px 28px;
      margin: 0 auto;
      overflow: hidden;
      background: #fff;
      border-radius: 20px;
      .el-button--primary {
        background-color: #38c1e7;
        border-color: #38c1e7;
        height: 46px;
        font-size: 16px;
        text-align: center;
      }
    }
  }
  // min-height: 100%;
  // width: 100%;
  // background-color: $bg;
  // overflow: hidden;
  .beian {
    position: absolute;
    width: 100%;
    bottom: 60px;
    color: #343434;
    font-size: 12px;
    padding: 0 0 0 15%;
    span {
      padding: 0 10px;
    }
    a {
      display: inline-block;
      color: #343434;
      vertical-align: top;
      font-size: 12px;
      &:hover {
        text-decoration: underline;
        text-decoration-color: #343434;
      }
    }
  }
  .tips {
    font-size: 14px;
    color: #04a2ce;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 8px;
      }
    }
    i {
      font-size: 20px;
      margin: 0 0 0 7px;
    }
  }
  .puzzle-box {
    position: absolute;
    top: 200px;
  }

  .svg-container {
    padding: 6px 5px 6px 6px;
    color: #38c1e7;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: #04a2ce;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
      span {
        display: inline-block;
        font-size: 18px;
        vertical-align: top;
        padding: 10px 0 0 4px;
      }
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
