<template>
  <Flex class="user-login-page" fd="co" ai="c" jc="c" style="height: 100%">
    <div class="user-login-title">中印边境自卫作战联合反击后装保障效能全业务全链路仿真系统</div>
    <Grid style="width: 917px; height: 445px" :columns="['524px', '329px', '0px']" gap="32px">
      <div class="oh" style="grid-area: 1 / 1 / 2 / 4">
        <img class="user-login-bg" :src="bgLogin" alt="" />
      </div>
      <Flex class="pr zi1" fd="co" jc="c" style="grid-area: 1 / 2 / 2 / 3">
        <div class="user-login-form-title">用户登录</div>
        <a-form class="user-login-form-body" :form="form" @submit="handleSubmit">
          <a-form-item>
            <a-input
              size="large"
              type="text"
              placeholder="请输入用户名"
              v-decorator="[
                'username',
                {
                  rules: [
                    { required: true, message: $t('user.userName.required') },
                    { validator: handleUsernameOrEmail },
                  ],
                  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-password
              size="large"
              placeholder="请输入密码"
              v-decorator="[
                'password',
                { rules: [{ required: true, message: $t('user.password.required') }], validateTrigger: 'blur' },
              ]"
            >
              <a-icon slot="prefix" type="lock" :style="{ color: 'rgba(0,0,0,.25)' }" />
            </a-input-password>
          </a-form-item>

          <a-form-item>
            <div class="pr">
              <a-input
                size="large"
                type="text"
                placeholder="验证码"
                v-decorator="[
                  'code',
                  {
                    rules: [{ required: true, message: '请输入验证码' }],
                    validateTrigger: 'change',
                  },
                ]"
              >
              </a-input>
              <img class="code pa" :src="codeImg" @click="codeClick" ref="code" alt="更新验证码" />
            </div>
          </a-form-item>

          <a-form-item style="margin-top: 16px">
            <a-button
              size="large"
              type="primary"
              htmlType="submit"
              class="login-button"
              :loading="state.loginBtn"
              :disabled="state.loginBtn"
            >
              登录
            </a-button>
          </a-form-item>
        </a-form>
      </Flex>
    </Grid>
  </Flex>
</template>

<script>
// import md5 from 'md5'
import { mapActions } from 'vuex'
import { timeFix } from '@/utils/util'

export default {
  data() {
    return {
      bgLogin: require('@/assets/images/user/bg-login.png'),
      form: this.$form.createForm(this),
      state: {
        loginBtn: false,
        // login type: 0 email, 1 username, 2 telephone
        loginType: 0,
      },
      clientCode: '',
      codeImg: '',
    }
  },
  created() {
    this.codeClick()
  },
  methods: {
    ...mapActions(['Login']),
    codeClick() {
      this.clientCode = 'c' + Math.random() * 100000000
      this.codeImg = '/api/validateCode?t=' + new Date().getTime() + '&clientCode=' + this.clientCode
    },
    // handler
    handleUsernameOrEmail(rule, value, callback) {
      const { state } = this
      const regex = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/
      if (regex.test(value)) {
        state.loginType = 0
      } else {
        state.loginType = 1
      }
      callback()
    },
    handleSubmit(e) {
      e.preventDefault()
      const {
        form: { validateFields },
        state,
        Login,
      } = this

      state.loginBtn = true

      const validateFieldsKey = ['username', 'password', 'code']

      validateFields(validateFieldsKey, { force: true }, (err, values) => {
        if (!err) {
          const loginParams = { ...values, clientCode: this.clientCode }
          delete loginParams.username
          loginParams[!state.loginType ? 'email' : 'userName'] = values.username
          loginParams.password = values.password
          Login(loginParams)
            .then((res) => {
              this.loginSuccess(res)
            })
            .catch((err) => {
              this.requestFailed(err)
            })
            .finally(() => {
              state.loginBtn = false
            })
        } else {
          setTimeout(() => {
            state.loginBtn = false
          }, 600)
        }
      })
    },
    loginSuccess(res) {
      console.log(res)
      this.$router.push({ path: '/' })
      // 延迟 1 秒显示欢迎信息
      setTimeout(() => {
        this.$notification.success({
          message: '欢迎',
          description: `${timeFix()}，欢迎回来`,
        })
      }, 1000)
    },
    requestFailed(err) {
      console.log(err)
      this.$notification['error']({
        message: '错误',
        description: ((err.response || {}).data || {}).message || '请求出现错误，请稍后再试',
        duration: 4,
      })
    },
  },
}
</script>

<style lang="less" scoped>
.user-login-title {
  height: 36px;
  font-size: 36px;
  line-height: 1;
  font-weight: bolder;
  letter-spacing: 7px;
  color: #ffffff;
  margin-bottom: 75px;
}
.user-login-bg {
  width: 937px;
  height: 465px;
  transform: translate(-10px, -8px);
}
.user-login-form-title {
  font-size: 24px;
  line-height: 1;
  font-weight: bolder;
  font-stretch: normal;
  letter-spacing: 5px;
  color: #ffffff;
  margin-bottom: 27px;
}
.user-login-form-body {
  button.login-button {
    padding: 0 15px;
    font-size: 16px;
    height: 40px;
    width: 100%;
    background-image: url(../../assets/images/user/btn-login.png);
    border: none;
    background-position: -2px 0px;
  }
  .code {
    position: absolute;
    top: 0;
    right: 0;
    width: 50%;
    height: 52px;
    object-fit: cover;
  }
}
::v-deep {
  .ant-input {
    height: 52px;
    background-color: #0b293a;
    border: solid 1px #1d5777;
    color: #ffffff;
  }
  .ant-input:-webkit-autofill {
    -webkit-text-fill-color: #fff !important;
    transition: background-color 5000s ease-in-out 0s;
    caret-color: #acfff2;
  }
  .ant-input-prefix .anticon {
    color: #ffffff !important;
  }
  .ant-input-suffix {
    color: #ffffff;
  }
  .ant-input-suffix .anticon:hover {
    color: #ffffffcc;
  }
  .has-error .ant-input:hover {
    background-color: #0b293a;
  }
  .has-error .ant-input-affix-wrapper .ant-input,
  .has-error .ant-input-affix-wrapper .ant-input:hover {
    background-color: #0b293a;
  }
}
</style>
