<template>
  <div class="page-login">
    <div class="page-login--layer page-login--layer-area">
      <ul class="circles">
        <li v-for="n in 10" :key="n"></li>
      </ul>
    </div>
    <div
      class="page-login--layer page-login--layer-time"
      flex="main:center cross:center">
      {{ time }}
    </div>
    <div class="page-login--layer">
      <div
        class="page-login--content"
        flex="dir:top main:justify cross:stretch box:justify">
        <div class="page-login--content-header">
          <p class="page-login--content-header-motto">
            {{ $t(key('time_is_most_precious')) }}
          </p>
        </div>
        <div
          class="page-login--content-main"
          flex="dir:top main:center cross:center">
          <img class="page-login--logo" :src="`${$baseUrl}image/logo/sc_logo.png`">
          <div class="page-login--form">
            <el-card shadow="never">
              <el-form
                ref="loginForm"
                label-position="top"
                :rules="rules"
                :model="formLogin"
                size="default">
                <el-form-item prop="username">
                  <el-input
                    type="text"
                    v-model="formLogin.username"
                    @keyup.enter.native="submit"
                    :placeholder="$t(key('username'))">
                    <i slot="prepend" class="fa fa-user-circle-o"></i>
                  </el-input>
                </el-form-item>
                <el-form-item prop="password">
                  <el-input
                    type="password"
                    v-model="formLogin.password"
                    @keyup.enter.native="submit"
                    :placeholder="$t(key('password'))">
                    <i slot="prepend" class="fa fa-keyboard-o"></i>
                  </el-input>
                </el-form-item>
                <el-button
                  size="default"
                  @click="submit"
                  type="primary"
                  class="button-login">
                  {{ $t(key('login')) }}
                </el-button>
              </el-form>
            </el-card>
          </div>
        </div>
        <div class="page-login--content-footer" />
      </div>
    </div>
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { mapActions } from 'vuex'
import util from '@/libs/util'
import localeMixin from '@/locales/mixin.js'
import { i18nMixin } from '@/composables/useI18n'
import '@/components/d2-icon/font-awesome-4.7.0/css/font-awesome.min.css'

export default {
  mixins: [
    localeMixin,
    i18nMixin('page.system.login')
  ],
  data () {
    return {
      // 实时时钟定时器
      timeInterval: null,
      // 当前时间显示
      time: dayjs().format('HH:mm:ss'),
      // 登录表单数据
      formLogin: {
        username: '',
        password: ''
      },
      // 表单校验规则
      rules: {
        username: [
          {
            required: true,
            message: this.$t(this.key('please_enter_username')),
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: this.$t(this.key('please_enter_password')),
            trigger: 'blur'
          }
        ]
      }
    }
  },
  mounted () {
    util.cookies.set('block', 'false')
    // 启动实时时钟
    this.timeInterval = setInterval(() => {
      this.refreshTime()
    }, 1000)
  },
  beforeDestroy () {
    // 销毁时清除时钟定时器
    clearInterval(this.timeInterval)
  },
  methods: {
    ...mapActions('d2admin/account', [
      'login'
    ]),
    // 刷新时间显示
    refreshTime () {
      this.time = dayjs().format('HH:mm:ss')
    },
    // 提交登录表单
    submit () {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          // 调用 Vuex login action 进行登录
          this.login({
            username: this.formLogin.username,
            password: this.formLogin.password
          })
            .then(() => {
              // 登录成功后跳转到目标页或首页
              this.$router.replace(this.$route.query.redirect || '/')
            })
            .catch((error) => {
              // 登录失败时清除残留的 cookie 并提示错误信息
              util.cookies.remove('token')
              util.cookies.remove('uuid')
              this.$message.error(error.message || this.$t(this.key('form_validation_failed')))
            })
        } else {
          this.$message.error(this.$t(this.key('form_validation_failed')))
        }
      })
    }
  }
}
</script>

<style lang="scss">
.page-login {
  @extend %unable-select;
  $backgroundColor: #F0F2F5;
  background-color: $backgroundColor;
  height: 100%;
  position: relative;

  .page-login--layer {
    @extend %full;
    overflow: auto;
  }

  .page-login--layer-area {
    overflow: hidden;
  }

  .page-login--layer-time {
    font-size: 24em;
    font-weight: bold;
    color: rgba(0, 0, 0, 0.03);
    overflow: hidden;
  }

  .page-login--content {
    height: 100%;
    min-height: 500px;
  }

  .page-login--content-header {
    padding: 1em 0;

    .page-login--content-header-motto {
      margin: 0px;
      padding: 0px;
      color: $color-text-normal;
      text-align: center;
      font-size: 12px;
    }
  }

  .page-login--logo {
    width: 240px;
    margin-bottom: 2em;
    margin-top: -2em;
  }

  .page-login--form {
    width: 280px;

    .el-card {
      margin-bottom: 15px;
    }

    .button-login {
      width: 100%;
    }

    .el-input-group__prepend {
      padding: 0px 14px;
    }
  }

  .page-login--content-footer {
    padding: 1em 0;
  }

  .circles {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    margin: 0px;
    padding: 0px;

    li {
      position: absolute;
      display: block;
      list-style: none;
      width: 20px;
      height: 20px;
      background: #FFF;
      animation: animate 25s linear infinite;
      bottom: -200px;

      @keyframes animate {
        0% {
          transform: translateY(0) rotate(0deg);
          opacity: 1;
          border-radius: 0;
        }
        100% {
          transform: translateY(-1000px) rotate(720deg);
          opacity: 0;
          border-radius: 50%;
        }
      }

      &:nth-child(1) {
        left: 15%;
        width: 80px;
        height: 80px;
        animation-delay: 0s;
      }

      &:nth-child(2) {
        left: 5%;
        width: 20px;
        height: 20px;
        animation-delay: 2s;
        animation-duration: 12s;
      }

      &:nth-child(3) {
        left: 70%;
        width: 20px;
        height: 20px;
        animation-delay: 4s;
      }

      &:nth-child(4) {
        left: 40%;
        width: 60px;
        height: 60px;
        animation-delay: 0s;
        animation-duration: 18s;
      }

      &:nth-child(5) {
        left: 65%;
        width: 20px;
        height: 20px;
        animation-delay: 0s;
      }

      &:nth-child(6) {
        left: 75%;
        width: 150px;
        height: 150px;
        animation-delay: 3s;
      }

      &:nth-child(7) {
        left: 35%;
        width: 200px;
        height: 200px;
        animation-delay: 7s;
      }

      &:nth-child(8) {
        left: 50%;
        width: 25px;
        height: 25px;
        animation-delay: 15s;
        animation-duration: 45s;
      }

      &:nth-child(9) {
        left: 20%;
        width: 15px;
        height: 15px;
        animation-delay: 2s;
        animation-duration: 35s;
      }

      &:nth-child(10) {
        left: 85%;
        width: 150px;
        height: 150px;
        animation-delay: 0s;
        animation-duration: 11s;
      }
    }
  }
}
</style>
