<template>
  <div class="login-container">
    <!-- <div hidden >
      <iframe src="https://10.194.188.237/gz-aiceps-manage/login.jsp" />
    </div> -->
    <div>
      <div class="logo_content">
        <div class="sysName">内蒙古自治区市场监督管理局</div>
        <img src="../../assets/images/login_bg.png" alt="" srcset="">
        <div class="copyRight">
          © {{ new Date().getFullYear() }} 内蒙古自治区市场监督管理局 | 技术支持：广东众望通科技股份有限公司
        </div>
      </div>
      <div class="form">
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          auto-complete="on"
          label-position="left"
        >
          <div class="title-container">
            <div class="title">欢迎登录！</div>
            <div class="desc">{{ systemTitleLogin }}</div>
          </div>
          <el-form-item prop="username">
            <span class="svg-container">
              <svg-icon icon-class="user" />
            </span>
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="用户名"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="on"
            />
          </el-form-item>

<!--          <el-form-item prop="captcha">-->
<!--            <el-col span="12">-->
<!--              <span class="svg-container">-->
<!--                <svg-icon icon-class="example" />-->
<!--              </span>-->
<!--              <el-input-->
<!--                ref="password"-->
<!--                v-model="loginForm.captcha"-->
<!--                placeholder="短信图形验证码"-->
<!--                name="password"-->
<!--                tabindex="2"-->
<!--                auto-complete="on"-->
<!--              />-->
<!--            </el-col>-->
<!--            <el-col span="7">-->
<!--              <el-image style="position: relative; top: 10px;" :src="captchaSrc" fit="cover"></el-image>-->
<!--            </el-col>-->
<!--            <el-col span="5">-->
<!--              <el-button style="margin-top: 8px" type="primary" plain icon="el-icon-refresh" @click="refreshCaptcha">刷新</el-button>-->
<!--            </el-col>-->
<!--          </el-form-item>-->

          <el-form-item prop="password">
            <el-col span="17">
              <span class="svg-container">
                <svg-icon icon-class="password" />
              </span>
              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                placeholder="密码"
                name="password"
                tabindex="2"
                auto-complete="on"
                @keyup.enter.native="handleLogin"
              />
            </el-col>
<!--            <el-col span="7">-->
<!--              <el-button-->
<!--                style="margin-top: 5px"-->
<!--                :loading="codeloading"-->
<!--                type="primary"-->
<!--                plain-->
<!--                @click="getCode"-->
<!--              >密码</el-button>-->
<!--            </el-col>-->
          </el-form-item>
          <el-form-item prop="captchadl">
            <el-col span="12">
              <span class="svg-container">
                <svg-icon icon-class="example" />
              </span>
              <el-input
                ref="password"
                v-model="loginForm.captchadl"
                placeholder="登录图形验证码"
                name="password"
                tabindex="2"
                auto-complete="on"
              />
            </el-col>
            <el-col span="7">
              <el-image style="position: relative; top: 10px;" :src="captchaSrcdl" fit="cover"></el-image>
            </el-col>
            <el-col span="5">
              <el-button style="margin-top: 8px" type="primary" plain icon="el-icon-refresh" @click="refreshCaptchadl">刷新</el-button>
            </el-col>
          </el-form-item>
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width:100%;margin:10px 0; background:#3296FA;height:64px;font-size:20px"
            @click.native.prevent="handleLogin"
          >登录</el-button>
          <el-button
            size="medium"
            type="default"
            style="width:100%;margin:0;height:50px;font-size:18px;border:1px solid #3296FA;color:#3296FA"
            @click.native.prevent="handleUnifiedLogin"
          >统一身份认证</el-button>
        </el-form>
      </div>

    </div>
    <iframe :src="`http://localhost:8093/manage/`" style="display: none" />
  </div>

</template>

<script>
import Cookie from 'js-cookie'
import { logManage, getCode, isHaveUser, loginByPwd } from '@/api/user.js'
export default {
  name: 'Login',
  data() {
    const { username, password } = Cookie.get('loginInfo') != null ? JSON.parse(Cookie.get('loginInfo')) : {}
    return {
      systemTitleLogin: process.env.VUE_APP_SYSTEM_TITLE_LOGIN,
      loginForm: {
        username,
        password,
        captcha: '',
        captchadl: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '用户名不能为空' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '短信验证码不能为空' }
        ],
        captcha: [
          { required: true, trigger: 'blur', message: '图形验证码不能为空' }
        ],
        captchadl: [
          { required: true, trigger: 'blur', message: '登录图形验证码不能为空' }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      reload: false,
      auditUserName: '',
      auditPassWordHid: '',
      codeloading: false,
      captchaSrc: '',
      captchaSrcdl: ''
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  mounted() {
    this.reload = this.$route.reload || false
    if (this.reload) {
      window.location.reload()
    }
    var ipno = this.$route.query.iponeNumber
    // eslint-disable-next-line eqeqeq
    if (ipno != '' && ipno != null && ipno != 'undefined') {
      // var jiam = btoa(ipno)
      // var jiem = atob(jiam)
      var jiem = atob(ipno) // 解密

      isHaveUser(jiem)
        .then((response) => {
          const { code, msg } = response
          if (code === 0) {
            this.loginForm.username = jiem
            this.getCode()
          } else {
            this.$message.warning(msg)
          }
          this.loading = false
        })
        .catch(() => {
          this.loading = false
        })
    }
    this.refreshCaptcha()
    this.refreshCaptchadl()
  },
  methods: {
    refreshCaptcha() {
      this.captchaSrc = '/aiccs-api/user/captcha' + '?' + new Date().getTime()
    },
    refreshCaptchadl() {
      this.captchaSrcdl = '/aiccs-api/user/captchadl' + '?' + new Date().getTime()
    },
    isEmpty(str) {
      return !str || str.length === 0 || str === null || str === 'null'
    },
    getCode() {
      if (this.isEmpty(this.loginForm.username)) {
        this.codeloading = false
        return this.$message.warning('账号不能为空')
      }
      if (this.isEmpty(this.loginForm.captcha)) {
        this.codeloading = false
        return this.$message.warning('图形验证码不能为空')
      }
      this.codeloading = true
      getCode(this.loginForm.username, this.loginForm.captcha).then((response) => {
        const { code, msg } = response
        if (code === 0) {
          this.$message.success('验证码已发送')
        } else {
          this.$message.warning(msg)
        }
        this.codeloading = false
      }).finally(() => {
        this.refreshCaptcha()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          loginByPwd(this.loginForm.username, this.loginForm.password, this.loginForm.captchadl)
            .then((response) => {
              const { code, msg } = response
              if (code === 0) {
                // this.loginManage()
                // this.loginManage()
                Cookie.set('loginInfo', this.loginForm, { expires: 30 })
                if (this.loginForm.password === '123456') {
                  this.$router.push({ path: '/redirect', query: { queryOlderP: '1' }})
                } else {
                  this.$router.push({ path: '/redirect' })
                }
              } else {
                this.$message.warning(msg)
                this.refreshCaptcha()
                this.refreshCaptchadl()
              }
              this.loading = false
            })
            .catch(() => {
              this.loading = false
              this.refreshCaptcha()
              this.refreshCaptchadl()
            })
        } else {
          console.log('error submit!!')
          this.refreshCaptcha()
          this.refreshCaptchadl()
          return false
        }
      })
    },
    loginManage() {
      const params = {
        auditUserName: this.loginForm.username
      }
      logManage(params).then(res => {
        console.log(res)
      })
    },
    handleUnifiedLogin() {
      window.location.href = process.env.VUE_APP_BASE_API + '/aiccs-api/oauth2/unified/login'
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #000;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 70%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      padding: 12px 5px 12px 15px;
      color: #000;
      height: 47px;
      caret-color: $cursor;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #fff inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }
  .el-form-item {
    border: 1px solid #8CC3FB;
    border-radius:5px;
  }
}
</style>

<style lang="scss" scoped>
$bg: #fff;
$dark_gray: #889aa4;
$light_gray: #43425D;
.login-container {
  position: relative;
  height: 100%;
  width: 100%;
  .logo_content{
      position: absolute;
      bottom:0;
      top:0;
      left: 0;
      right: 0;
      img {
        width: 100%;
        height: 100%;
      }
      .sysName{
        position: absolute;
        bottom: 0;
        top: 50px;
        left: 50px;
        color: #fff;
        font-size: 32px;
        font-family: "Times New Roman", Times, serif;
      }
  }
  .form {
    position: absolute;
    left: 55%;
    top: 50%;
    margin-top: -330px;
    width: 620px;
    height: 680px;
    padding: 86px 60px;
    background: #fff;
    border-radius: 12px;
    box-shadow: 0px 4px 20px 0px rgba(7,80,212,0.15);
    .title-container {
      .title {
        font-size: 42px;
        font-weight: 600;
        margin: 16px 0!important;
        padding: 0;
      }
      .desc {
        font-size: 40px;
        margin-bottom: 64px;
      }
    }
  }
  .copyRight {
    position: absolute;
    bottom:30px;
    left: 15%;
    color: #fff;
    font-size: 14px;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
