<template>
  <div class="login-container">
    <div class="login-form" style="padding: 39px 26px;">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        auto-complete="on"
      >
        <div class="title-container flex">
          <img :src="xing" alt="" style="width: 25px;height: 23px" />
          <h3 class="title ml5">欢迎登录</h3>
        </div>

        <el-form-item prop="userName" class="login-input" label="账号ID">
          <!--        <span class="svg-container">-->
          <!--          <svg-icon icon-class="user" />-->
          <!--        </span>-->
          <el-input
            ref="userName"
            v-model="loginForm.userName"
            placeholder="请输入用户名"
            name="userName"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>

        <el-form-item prop="password" class="login-input">
          <!--        <span class="svg-container">-->
          <!--          <svg-icon icon-class="password" />-->
          <!--        </span>-->
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="请输入密码"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <i v-if="passwordType === 'password'" class="el-icon-view"></i>
            <i
              v-else
              class="iconfont icon-yanjingxianshi"
              style="font-size: 20px"
            ></i>
            <!--          <svg-icon-->
            <!--            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"-->
            <!--          />-->
          </span>
        </el-form-item>
      </el-form>
      <div style="height: 127px"></div>
      <div>
        <el-button
          :loading="loading"
          type="primary"
          style="width:100%;margin-bottom: 20px;border-radius: 8px"
          :style="buttonStyle"
          @click.native.prevent="handleLogin"
          >登录 <i class="iconfont icon-chuansuoyou"></i
        ></el-button>
      </div>
    </div>

    <div class="tip">
      <p>
        版权所有©
        <a href="http://www.ebiz-interactive.com/" target="_blank"
          >天津易商数智科技股份有限公司2025</a
        >
      </p>
      <!--      <p>京公网安备 ICP许可证号：津ICP备14004859号-4</p>-->
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/assets/js/utils/validate.js'
import xing from '@/assets/images/xing.png'
// import { indexUser } from '@/api/app/user'
export default {
  name: 'Login',
  data() {
    const validateuserName = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能少于6位'))
      } else {
        callback()
      }
    }
    return {
      xing,
      loginForm: {
        userName: '',
        password: ''
      },
      loginRules: {
        userName: [
          { required: true, trigger: 'blur', message: '请输入用户名' }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  computed: {
    buttonStyle() {
      return {
        backgroundColor:
          this.loginForm.userName && this.loginForm.password
            ? '#4f47f5'
            : '#B1B9FB',
        border:
          this.loginForm.userName && this.loginForm.password
            ? '#4f47f5'
            : '#B1B9FB'
      }
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      // mock接口
      // indexUser()
      //   .then(res => {
      //     this.$router.push({ path: this.redirect || '/' })
      //     localStorage.token = res.token
      //   })
      //   .catch(err => this.$toast(err))

      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: '/home' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    }
  }
}
</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 {
  background-image: url('../../../../src/assets/images/login.png');
  background-size: cover;
  background-position: center;
  & .tip {
    position: fixed;
    bottom: 20px;
    width: 100%;
    text-align: center;
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    font-size: 13px;
    color: #3f4c75;
    line-height: 22px;
    font-style: normal;
  }
}

.login-input {
  position: relative;
  border: 1px solid #eaecf5;
  border-radius: 8px;

  & .el-form-item__label {
    position: absolute;
    top: -5px;
    font-size: 12px;
    z-index: 2;
    left: 0;
    font-weight: normal;
    background: #f5fbff;
    margin: 0 5px 0 10px;
    padding: 0 5px;
    line-height: 1;
    &:before {
      content: '';
      display: none;
    }
  }

  & .el-input {
    & input {
      // 清除 input 自动填充背景色
      background-color: transparent;
      color: #000;
      border: none;
      outline: none;

      &:-webkit-autofill {
        transition: background-color 5000s ease-in-out 0s;
      }

      &:focus {
        border: 1px solid #4f47f5;
      }
    }
  }

  // 添加 :focus-within 伪类来改变 el-form-item__label 的颜色
  &:focus-within .el-form-item__label {
    color: #4f47f5;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: #f0f4fa;
  overflow: hidden;

  & .login-form {
    border: 2px solid #fff;

    background: linear-gradient(
      180deg,
      rgba(255, 255, 255, 0.93) 0%,
      rgba(235, 246, 255, 0.82) 100%
    );
    width: 427px;
    margin: 0 auto;
    overflow: hidden;
    top: 15vh;
    right: 25vh;
    border-radius: 8px;
    position: fixed;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    margin-bottom: 50px;

    .title {
      font-size: 26px;
      color: #000;
      text-align: left;
      font-weight: bold;
    }
  }

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