<template>
  <div class="LoginIDItem">
    <div class="LoginName tl">
      HI～欢迎使用<br />
      大数据精准教学诊断平台
    </div>
    <div class="itemName fs16 lh22 fw tl">账号密码登录</div>
    <el-form class="login-form">
      <el-form-item>
        <el-input size="medium" v-model="username" placeholder="请输入账号">
          <template #prefix>
            <span>
              <img
                src="@/assets/icon/user_icon.png"
                alt="User Icon"
                style="width: 18px; height: 18px"
              />
            </span>
          </template>
          <template #suffix v-if="username != ''">
            <span @click="clearInput" style="cursor: pointer">
              <img
                src="@/assets/icon/userName_clear.png"
                alt="Clear Icon"
                style="width: 18px; height: 18px"
              />
            </span>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <div class="inputDeep">
          <el-input
            v-model="password"
            :type="showPassword ? 'text' : 'password'"
            placeholder="请输入密码"
            @keydown.enter.native="login"
          >
            <template #prefix>
              <span>
                <img
                  src="@/assets/icon/passWord_icon.png"
                  alt="User Icon"
                  style="width: 18px; height: 18px"
                />
              </span>
            </template>
            <template #suffix>
              <span
                v-if="showPassword"
                @click="togglePasswordVisibility"
                class="display_style"
              >
                <img
                  src="@/assets/icon/passWord_show.png"
                  alt="User Icon"
                  style="width: 18px; height: 18px"
                />
              </span>
              <i
                v-else
                class="iconfont icon-ttubiao_hide"
                @click="togglePasswordVisibility"
              ></i>
            </template>
          </el-input>
        </div>
      </el-form-item>
      <el-form-item style="margin-bottom: -10px; margin-top: -10px">
        <div class="itmeWrap">
          <el-checkbox
            v-model="remeberPassWord"
            @change="changeRemeberPW"
            style="line-height: 39.6px"
            >记住密码</el-checkbox
          >
          <el-button type="text" class="forgetPassWord" @click="emailLogin(3)"
            >忘记密码</el-button
          >
        </div>
      </el-form-item>
      <el-form-item>
        <div class="btnWrap">
          <el-button type="primary" style="width: 100%" @click="login"
            >登 录</el-button
          >
        </div>
      </el-form-item>
    </el-form>
    <div class="other-login">
      <span class="line"></span>
      <span class="text">其他登录方式</span>
      <span class="line"></span>
    </div>
    <div
      class="text"
      style="
        font-size: 13px;
        cursor: pointer;
        display: flex;
        justify-content: center;
      "
    >
      <div style="padding-right: 42px">
        <img src="../../../assets/weChat.png" alt="" />
        <p class="margin_top4">微信</p>
      </div>
      <div style="padding-right: 42px">
        <img src="../../../assets/ding.png" alt="" />
        <p class="margin_top4">钉钉</p>
      </div>
      <div>
        <img src="../../../assets/email.png" alt="" @click="emailLogin(2)" />
        <p class="margin_top4">邮箱</p>
      </div>
    </div>
  </div>
</template>
<script>
import user from "@/http/api/user";
import { setToken } from "@/utils/auth";
export default {
  name: "LoginForID",
  data() {
    return {
      username: "",
      password: "",
      remeberPassWord: false,
      tabIndex: 1,
      phoneNumber: null,
      phoneCode: null,
      countTime: 60,
      isBegainGetCode: false,
      showPassword: false,
    };
  },
  mounted() {
    this.loadCredentials();
    
  },
 
  methods: {
    loadCredentials() {
      const userName = localStorage.getItem("userName");
      const password = localStorage.getItem("userPassWord");
      this.username = userName || "";
      this.password = password || "";
      if (this.username && this.password) {
        this.remeberPassWord = true;
      }
    },
    clearInput() {
      this.username = "";
    },
    login() {
      const params = {
        username: this.username.trim(),
        password: this.password,
      };

      if (!params.username) {
        this.$message({
          message: "用户名不能为空！",
          type: "warning",
        });
        return;
      }

      if (!params.password) {
        this.$message({
          message: "密码不能为空！",
          type: "warning",
        });
        return;
      }

      user
        .login(params)
        .then((res) => {
          if (res.code === 200) {
            console.log("打印登录结果", res);
            setToken(res.data.tokenValue);
            this.$store.dispatch("user/SET_TOKEN", res.data.tokenValue);
            const menuList = res.data.pcMenuVOS || [];
            this.$store.commit("user/SET_MENULIST", menuList); //设置菜单
            this.$store.commit("user/SET_USEAR_INFOLOGIN", res.data);
            this.$store.dispatch("user/getInfo"); //获取用户登录信息
            if (this.remeberPassWord) {
              localStorage.setItem("userName", params.username);
              localStorage.setItem("userPassWord", params.password);
            } else {
              localStorage.removeItem("userName");
              localStorage.removeItem("userPassWord");
            }

            const directUrl = this.$route.query.redirect || "/userInfo";
            this.$router.push({ path: directUrl });
          }
        })
        .catch((err) => {
          this.$message({
            message: "登录失败，请稍后再试。",
            type: "warning",
          });
          console.error(err);
        });
    },
    changeRemeberPW(e) {
      if (e) {
        localStorage.setItem("userName", this.username);
        localStorage.setItem("userPassWord", this.password);
      } else {
        localStorage.removeItem("userName");
        localStorage.removeItem("userPassWord");
        this.username = "";
        this.password = "";
      }
    },
    goHome() {},
    loginTabChange(index) {
      this.tabIndex = index;
    },
    emailLogin(newStatus) {
      this.$emit("update-nav-status", newStatus);
    },
    togglePasswordVisibility() {
      this.showPassword = !this.showPassword;
    },
  },
};
</script>

<style lang="scss" scoped>
.LoginIDItem {
  margin: 0 auto;
  width: 354px;

  .LoginName {
    font-size: 32px;
    text-align: left;
    color: #333333;
    line-height: 48px;
  }

  .itemName {
    color: #333333;
    margin-top: 14px;
  }

  .login-form {
    .el-form-item {
      margin-top: 20px;

      :deep() .el-input__inner {
        height: 53px;
        line-height: 53px;
        border-radius: 10px;
      }

      :deep() .el-form-item__content {
        line-height: 18px;
      }
    }

    .inputDeep {
      :deep() .el-input__suffix {
        line-height: 53px;
      }
    }
  }

  .itmeWrap {
    display: flex;
    justify-content: space-between;
  }

  .btnWrap {
    height: 53px;

    .el-button {
      height: 53px;
      border-radius: 10px;
      font-size: 16px;
    }
  }

  .forgetPassWord {
    color: rgba(25, 31, 37, 0.72);
  }
}

.login-container {
  text-align: center;
}

.other-login {
  display: flex;
  align-items: center;
  margin: 20px 0;
}

.line {
  flex: 1;
  height: 1px;
  background-color: #dcdfe6;
}

.text {
  margin: 0 10px;
  color: #909399;

  img {
    width: 32px;
  }
}

:deep() .el-checkbox__input.is-checked + .el-checkbox__label {
  color: rgba(25, 31, 37, 0.72);
}

.margin_top4 {
  margin-top: 4px;
}

:deep() .el-input__prefix,
:deep() .el-input__suffix,
:deep() .el-input__suffix-inner,
.display_style {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

:deep() .el-input__prefix {
  margin-left: 12px;
}

:deep() .el-input__suffix {
  margin-right: 10px;
}

:deep() .el-input--prefix .el-input__inner {
  padding-left: 44px;
  font-size: 14px;
  padding-right: 35px;
}

:deep() .iconfont.icon-ttubiao_hide {
  font-size: 18px;
}
</style>
