<template>
  <div class="c-login">
    <div class="c-login__img" />
    <div class="c-login__main c-login-form">
      <div class="c-login-form__header">
        <div class="c-login-form__logo" />
        <div class="c-login-form__name">浪潮屏媒安播云平台</div>
      </div>
      <div class="c-login-form__wrapper">
        <label class="c-login-for__label">用户名</label>
        <div class="c-login-form__section">
          <input
            v-model="username"
            type="text"
            class="c-login-form__input"
            autocomplete="off"
          >
          <i class="c-login-form__icon el-icon-user" />
        </div>
      </div>
      <div class="c-login-form__wrapper">
        <label class="c-login-for__label">密码</label>
        <div class="c-login-form__section">
          <input
            type="password"
            hidden
          >
          <input
            v-model="password"
            type="password"
            class="c-login-form__input"
            autocomplete="new-password"
          >
          <i class="c-login-form__icon el-icon-lock" />
        </div>
      </div>
      <button
        class="c-login-form__submit u-pointer"
        @click="login"
      >
        <i
          v-if="loading"
          class="el-icon-loading"
        />
        登 录
      </button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data () {
    return {
      username: 'admin',
      password: '111111',
      loading: false,
      redirect: null,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler (route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  methods: {
    login () {
      if (this.loading) {
        return
      }
      if (!this.username) {
        this.$message({
          type: 'warning',
          message: '用户名不能为空'
        })
        return
      }
      if (!this.password) {
        this.$message({
          type: 'warning',
          message: '密码不能为空'
        })
        return
      }
      this.loading = true

      this.$store.commit('user/SET_TOKEN', 'test')
      this.$store.commit('user/SET_NAME', 'Super Admin')
      this.$store.commit('user/SET_AVATAR', 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif')
      this.$store.commit('user/SET_ROLES', ['admin'])

      this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
    },
    getOtherQuery (query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

<style lang="scss" scoped>
.c-login {
  display: flex;
  min-height: 100%;
  width: 100%;

  &__img {
    flex: 1 1 400px;
    display: inline-block;
    background: url("~@/assets/illustration.png") 50% 50% / 757px 613px
      no-repeat;
  }

  &__main {
    flex: 1 0 auto;
  }
}

@media screen and (max-width: 900px) {
  .c-login__img {
    display: none;
  }
}

.c-login-form {
  display: inline-flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 40px 0 80px;
  background-color: #fff;
  overflow: auto;

  &__header {
    display: inline-flex;
    align-items: center;
    margin-bottom: 100px;
  }

  &__logo {
    display: inline-block;
    width: 142px;
    height: 36px;
    background: url("~@/assets/logo.png") 0 0 / 100% 100% no-repeat;
  }

  &__name {
    padding-left: 10px;
    color: $blue;
    font-size: 28px;
    font-weight: bold;
    line-height: 1;
  }

  &__wrapper {
    display: inline-flex;
    flex-direction: column;
    color: $gray;
    border-bottom: 1px solid currentColor;

    & + & {
      margin-top: 30px;
    }
  }

  &__label {
    font-size: 24px;
    line-height: 1;
  }

  &__section {
    display: inline-flex;
    align-items: center;
    padding: 8px 0 2px;
  }

  &__input {
    width: 320px;
    height: 30px;
    font-size: 18px;
    line-height: 30px;
  }

  &__icon {
    padding-left: 10px;
    font-size: 24px;
  }

  &__submit {
    display: inline-flex;
    justify-content: center;
    align-items: center;
    width: 360px;
    height: 40px;
    margin-top: 80px;
    color: #fff;
    font-size: 18px;
    background-color: $blue;
    border-radius: $radius;
  }
}
</style>
