<template>
  <div class="login-container">
    <div class="login-form">
      <div class="header">
        <img
          :src="ImageLogo"
        >
        <div class="description">
          <div class="zw">
            企业购工作台
          </div>
        </div>
      </div>
      <ElForm
        ref="loginForm"
        :model="loginForm"
        size="medium"
        class="form-container"
        auto-complete="on"
        label-position="left"
      >
        <div class="title">
          欢迎登陆
        </div>
        <ElFormItem prop="username">
          <ElInput
            v-model="loginForm.username"
            name="username"
            type="text"
            auto-complete="on"
            placeholder="请输入登录账号"
          >
            <svg-icon
              slot="prefix"
              icon-class="user"
            />
          </ElInput>
        </ElFormItem>

        <ElFormItem prop="password">
          <ElInput
            v-model="loginForm.password"
            :type="passwordType"
            name="password"
            auto-complete="on"
            placeholder="请输入密码"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon
              slot="prefix"
              icon-class="password"
            />
          </ElInput>
        </ElFormItem>
        <!--<el-link-->
        <!--class="link"-->
        <!--@click="$emit('edit')"-->
        <!--&gt;-->
        <!--忘记密码-->
        <!--</el-link>-->
        <ElButton
          :loading="loading"
          size="medium"
          type="primary"
          class="login-button"
          @click.native.prevent="handleLogin"
        >
          登录
        </ElButton>
      </ElForm>
    </div>
    <div
      v-show="visible"
      class="verification-container"
    >
      <ImageVerification
        :visible="visible"
        :bg-image="bgImage"
        :slider-image="sliderImage"
        :y-height="yHeight"
        @getMoveDistance="handleSuccess"
        @refresh="refresh"
        @close="visible = false"
      />
    </div>
  </div>
</template>
<script>
import { verifyCode } from '@/api/auth'
import ImageVerification from '../components/ImageVerification'
import ImageLogo from '@/assets/logo.png'

export default {
  components: {
    ImageVerification
  },
  data() {
    return {
      ImageLogo,
      loginForm: {
        username: null,
        password: null
      },
      loading: false,
      passwordType: 'password',
      bgImage: '',
      sliderImage: '',
      yHeight: 0,
      visible: false
    }
  },
  methods: {
    async handleSuccess(code) {
      const params = {
        p: { ...this.loginForm, checkMoveId: this.checkMoveId, code: code }
      }
      try {
        await this.$store.dispatch('user/login', params)
        this.$message.success('登录成功')
        this.$router.push({
          name: 'order-list'
        })
      } catch (e) {
        this.handleLogin()
      }
    },
    async refresh() {
      await this.handleLogin()
    },
    async handleLogin() {
      this.loading = true
      try {
        const valid = await this.$refs.loginForm.validate()
        const data = (await verifyCode()).data
        this.bgImage = data.bigImage
        this.sliderImage = data.smallImage
        this.yHeight = data.yHeight
        this.checkMoveId = data.checkMoveId
        if (valid) {
          this.visible = true
        }
      } finally {
        this.loading = false
      }
    }
  }
}
</script>
<style scoped lang="scss">
$login-header-height: 80px;
.login-container {
  background-color: #ffffff;
  width: 700px;
  max-width: calc(100% - 40px);
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .header {
    height: $login-header-height;
    background-color: #344354;
    display: flex;
    align-items: center;
    padding: 0 50px;
    color: #ffffff;

    img {
      width: 130px;
      margin-top: 40px;
    }

    .description {
      display: flex;
      flex-direction: column;
      margin-left: 50px;
      letter-spacing: 10px;
      font-weight: bold;

      .zw {
        font-size: 28px;
      }

      .yw {
        font-size: 12px;
        margin: 10px 0 0 40px;
      }
    }
  }

  .form-container {
    padding: 50px 24%;

    .title {
      margin: 0 0 10px 0;
      font-size: 14px;
    }

    .login-button {
      width: 100%;
      margin: 20px 0 30px 0;
      background-color: #004E9D;
    }
  }

  .verification-container {
    position: absolute;
    left: 50%;
    top: $login-header-height;
    transform: translateX(-50%);
    background-color: #ffffff;
    width: 100%;
    height: calc(100% - #{ $login-header-height });
    display: flex;
    align-items: center;
    justify-content: center;
  }
}

@media screen and (max-width: 768px) {
  .form-container {
    padding: 50px 10% !important;
  }

  .header {
    padding: 0 10px !important;

    img {
      width: 50px !important;
      margin: 0 !important;
    }

    .description {
      letter-spacing: 2px !important;
      margin-left: 20px !important;

      .yw {
        margin: 0 !important;
      }
    }
  }
}

</style>
