<template>
  <div class="main" style="margin-top: 20%">
    <a-form
      id="formLogin"
      class="user-layout-login"
      ref="formLogin"
      :form="form"
      @submit="doLogin"
    >
      <!-- <a-alert v-if="isLoginError" type="error" showIcon style="margin-bottom: 24px;" message="账户或密码错误，请输入（admin/admin123 )" /> -->
      <a-form-item>
        <a-input
          size="large"
          type="text"
          style="  height: 50px;"
          placeholder="输入用户名: "
          v-decorator="[
            'username',
            {rules: [{ required: true, message: '用户名不能为空' }], validateTrigger: 'change'}
          ]"
        >
          <a-icon
            slot="prefix"
            type="user"
            :style="{ color: 'rgba(0,0,0,.25)' }"
          />
        </a-input>
      </a-form-item>
      <a-form-item>
        <a-input
          size="large"
          type="password"
          autocomplete="false"
          style="  height: 50px;"
          placeholder="输入密码: "
          v-decorator="[
            'password',
            {rules: [{ required: true, message: '请输入密码' }], validateTrigger: 'blur'}
          ]"
        >
          <a-icon
            slot="prefix"
            type="lock"
            :style="{ color: 'rgba(0,0,0,.25)' }"
          />
        </a-input>
      </a-form-item>
      <a-form-item>
        <a-checkbox
          style="color: #fff"
          v-decorator="['rememberName', { valuePropName: 'checked', initialValue: rememberName}]"
        >记住用户名</a-checkbox>
      </a-form-item>
      <a-form-item style="margin-top:24px">
        <a-button
          size="large"
          type="primary"
          htmlType="submit"
          class="login-button"
          :loading="state.loginBtn"
          :disabled="state.loginBtn"
        >登录</a-button>
      </a-form-item>

      <div class="user-login-other"/>
    </a-form>
  </div>
</template>

<script>
import md5 from 'md5'
import { mapActions } from 'vuex'
import { timeFix } from '../../../packages/utils/util'
import Vue from 'vue'
export default {
  components: {},
  data () {
    return {
      loginBtn: false,
      isLoginError: false,
      rememberName: Vue.ls.get('rememberName'),
      form: this.$form.createForm(this),
      loginParams: {},
      state: {
        time: 60,
        loginBtn: false
      }
    }
  },
  mounted () {
    if (this.rememberName) {
      const userName = Vue.ls.get('uname', '')
      this.form.setFieldsValue({
        username: userName
      })
    }
  },
  methods: {
    ...mapActions(['Login', 'Logout']),
    // 登录操作
    doLogin (e) {
      e.preventDefault()
      const {
        // form: { validateFields },
        state,
        Login
      } = this
      state.loginBtn = true
      // const validateFieldsKey = ['username', 'password']
      this.form.validateFields((err, values) => {
        if (!err) {
          this.loginParams = { ...values }
          this.loginParams.password = md5(values.password)
          Login(this.loginParams)
            .then(res => this.loginSuccess(res))
            .catch(err => this.requestFailed(err))
            .finally(() => {
              state.loginBtn = false
            })
        } else {
          setTimeout(() => {
            state.loginBtn = false
          }, 600)
        }
      })
    },
    // 登录成功
    loginSuccess (res) {
      // 注意设置默认路径
      this.$router.push({ path: '/' })
      // 延迟 1 秒显示欢迎信息
      setTimeout(() => {
        this.$notification.success({
          message: `欢迎`,
          description: `${timeFix()}，欢迎回来`,
          duration: 2
        })
      }, 1000)
      this.isLoginError = false
    },
    // 登录失败
    requestFailed (err) {
      this.isLoginError = true
      this.$notification['error']({
        message: '错误',
        description: err.message,
        duration: 3
      })
    }
  }
}
</script>

<style lang="less" scoped>

.ant-tabs2 /deep/ .ant-tabs-ink-bar{
  background-color: white;
}

//.ant-tabs2{
//  /deep/.ant-tabs-tab{
//    background:red;
//  }
//}

.user-layout-login {
  label {
    font-size: 12px;
  }

  .getCaptcha {
    display: block;
    width: 100%;
    height: 40px;
  }

  .forge-password {
    font-size: 12px;
  }

  button.login-button {
    background: #E83820;
    border-color: #E83820;
    padding: 0 15px;
    font-size: 16px !important;
    height: 55px;
    color: #fff;
    width: 100%;
  }

  .user-login-other {
    text-align: left;
    margin-top: 24px;
    line-height: 22px;

    .item-icon {
      font-size: 24px;
      color: rgba(0, 0, 0, 0.2);
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: #1890ff;
      }
    }

    .register {
      float: right;
    }
  }
  /deep/ .ant-select {
    height: 50px;
    font-size: 15px !important;
    .ant-select-selection {
      height: 100%;
    }
    .ant-select-selection__rendered {
      height: 100%;
      line-height: 48px;
    }
  }
}
</style>
