<template>
  <div class="login" :style="'background-image:url('+ Background +');'">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" label-position="left" label-width="0px" class="login-form">
      <h2 class="title" :style="isSuccess ? { color: '#67C23A' }:{}">
        <i v-show="isSuccess" class="el-icon-success" />企业账号申请<span v-show="isSuccess">完成</span>
      </h2>
      <h4 class="title" :style="isSuccess ? { color: '#67C23A' }:{}">
        <span v-show="!isSuccess">平度应急局安全风险监测预警管理平台</span>
        <span v-show="isSuccess">请等待应急局审核，通过后使用企业名称登录</span>
      </h4>
      <el-form-item prop="username">
        <el-input v-model="loginForm.username" type="text" auto-complete="off" :disabled="isSuccess" placeholder="企业名称（账号）" maxlength="100" show-word-limit>
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 63%" :disabled="isSuccess" @keyup.enter.native="handleApply">
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode">
        </div>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button :loading="loading" size="medium" type="primary" style="width:100%;" :disabled="isSuccess" @click.native.prevent="handleApply">
          <span v-if="!loading">提 交 申 请</span>
          <span v-else>提 交 中 ...</span>
        </el-button>
      </el-form-item>
      <el-form-item style="width:100%;">
        <router-link :to="{path: '/login'}">
          <el-button size="medium" style="width:100%;">
            <span>返 回</span>
          </el-button>
        </router-link>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div v-if="$store.state.settings.showFooter" id="el-login-footer">
      <span v-html="$store.state.settings.footerTxt" />
      <span v-if="$store.state.settings.caseNumber"> ⋅ </span>
      <a href="https://beian.miit.gov.cn/#/Integrated/index" target="_blank">{{ $store.state.settings.caseNumber }}</a>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from '@/api/login'
import enterpriseApi from '@/api/kd/enterprise'
import qs from 'qs'
import Background from '@/assets/images/background.webp'
export default {
  name: 'Login',
  data() {
    return {
      isSuccess: false,
      Background: Background,
      codeUrl: '',
      loginForm: {
        username: '',
        code: '',
        uuid: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '企业名称不能为空' }],
        code: [{ required: true, trigger: 'change', message: '验证码不能为空' }]
      },
      loading: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const data = route.query
        if (data && data.redirect) {
          this.redirect = data.redirect
          delete data.redirect
          if (JSON.stringify(data) !== '{}') {
            this.redirect = this.redirect + '&' + qs.stringify(data, { indices: false })
          }
        }
      },
      immediate: true
    }
  },
  created() {
    // 获取验证码
    this.getCode()
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.codeUrl = res.img
        this.loginForm.uuid = res.uuid
      })
    },
    handleApply() {
      this.$refs.loginForm.validate(valid => {
        const enterprise = {
          name: this.loginForm.username,
          code: this.loginForm.code,
          uuid: this.loginForm.uuid
        }
        if (valid) {
          this.loading = true
          enterpriseApi.apply(enterprise).then(() => {
            this.loading = false
            this.isSuccess = true
          }).catch(() => {
            this.loading = false
            this.getCode()
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
  .login {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    background-size: cover;
  }
  .title {
    margin: 0 auto 30px auto;
    text-align: center;
    color: #707070;
  }

  .login-form {
    border-radius: 6px;
    background: #ffffff;
    width: 500px; /* 385px */
    padding: 25px 25px 5px 25px;
    .el-input {
      height: 38px;
      input {
        height: 38px;
      }
    }
    .input-icon{
      height: 39px;width: 14px;margin-left: 2px;
    }
  }
  .login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }
  .login-code {
    width: 33%;
    display: inline-block;
    height: 38px;
    float: right;
    img{
      cursor: pointer;
      vertical-align:middle
    }
  }
</style>
