<template>
  <div class="login">
    <!-- 公司左上角logo -->
    <div class="left-logo">
      <svg-icon icon-class="icon-hm-login1" style="font-size:130px;" />
    </div>
    <!-- 中间背景 -->
    <div class="login-bgm" />
    <!-- <img src="../../../assets/img/login_bgm.png" alt="" class="login-bgm"> -->
    <div class="login-content">
      <div class="login-content-title">
        <div class="hospital_logo">
          <img width="500" src="@/assets/img/xianganlogo.png">
        </div>
        <p class="main-title">眼科工作平台</p>
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="密码登录" name="first">
            <el-form
              ref="dataForm"
              :model="dataForm"
              :rules="dataRule"
              status-icon
            >
              <!-- 用户名 -->
              <el-form-item prop="account">
                <el-input v-model="dataForm.account" placeholder="请输入工号" @keyup.enter.native="submitNext" />
              </el-form-item>
              <!-- 密码 -->
              <el-form-item prop="password">
                <el-input ref="passWord" v-model="dataForm.password" type="password" placeholder="请输入密码" @keyup.enter.native="submitNext2" />
              </el-form-item>
              <el-form-item prop="captcha">
                <el-row :gutter="10">
                  <el-col :span="14">
                    <el-input ref="captcha" v-model="dataForm.captcha" :placeholder="$t('login.captcha')" @keyup.enter.native="dataFormSubmitHandle()" />
                  </el-col>
                  <el-col :span="10" class="login-captcha">
                    <img :src="captchaPath" @click="getCaptcha()">
                  </el-col>
                </el-row>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" class="w-percent-100" @click="dataFormSubmitHandle()">{{ $t('login.title') }}
                </el-button>
              </el-form-item>
            </el-form>
            <p class="tips">没有账号? 请联系管理员</p>
          </el-tab-pane>
          <el-tab-pane label="CA登录" name="second" />
        </el-tabs>
      </div>
    </div>
    <div class="login-footer">
      <p class="copyright">南京慧目信息技术有限公司 版权所有</p>
    </div>
    <el-dialog
      class="caLogin"
      :visible.sync="visible"
      width="40%"
      title="CA登录"
      @close="visible === false"
    >
      <iframe name="ifrmName" frameborder="0" width="100%" height="100%" :src="caUrl" />
    </el-dialog>
  </div>
</template>
<script>
import debounce from 'lodash/debounce'
import { messages } from '@/i18n'
import { getUUID } from '@/utils'
export default {
  data() {
    return {
      i18nMessages: messages,
      activeName: 'first',
      captchaPath: '',
      visible: false,
      caUrl: 'http://192.168.4.174:8080/ca-sign-xiangan/',
      dataForm: {
        account: '',
        password: '',
        uuid: '',
        captcha: ''
      }
    }
  },
  computed: {
    dataRule() {
      return {
        account: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        password: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        captcha: [
          { required: false, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getCaptcha()
  },
  methods: {
    handleClick() {
      if (this.activeName === 'second') {
        this.visible = true
        this.$nextTick(() => {
          document.getElementsByTagName('iframe')[0].src = this.caUrl
        })
      }
    },
    submitNext() {
      this.$refs.passWord.focus()
    },
    submitNext2() {
      this.$refs.captcha.focus()
    },
    // 获取验证码
    async getCaptcha() {
      this.dataForm.uuid = getUUID()
      const { data: res } = await this.$http.get(`/captcha?uuid=${this.dataForm.uuid}`)
      this.captchaPath = res.data
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function() {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        const params = {
          ...this.dataForm
        }
        params.password = this.$md5(this.dataForm.password)
        this.$http.post('/login', params).then(({ data: res }) => {
          if (res.code !== 0) {
            this.getCaptcha()
            return this.$message.error(res.msg)
          }
          window.sessionStorage.setItem('xa-token', res.data.token)
          window.sessionStorage.setItem('qg-userData', JSON.stringify(res.data.currentUser))
          this.$router.push({ name: 'outpatientManagement-call' })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
<style lang="scss" scoped>
.login {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  display: flex;
  justify-content: center;
  position: relative;
  .hospital_logo{
    position: fixed;
    top: -100px;
    left: -80px;
  }
  .left-logo {
    position: fixed;
    left: 30px;
    top: -30px;
    .title {
      font-size: 18px;
      font-family: PingFang SC;
      font-weight: 700;
      display: inline-block;
      padding-left: 10px;
      vertical-align: middle;
    }
  }
  .login-bgm {
    background: url(../../../assets/img/login_bgm.png) no-repeat top center;
    width: 900px;
    height: 276px;
    margin: 0 auto;
    margin-top: 175px;
  }
  .login-content {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    //width: 100%;
    .login-content-title {
      text-align: center;
      color: #262626;
      margin-bottom: 32px;
      .main-title {
        font-size: 38px;
        margin-bottom: 20px;
        color:#1E79FF;
        font-weight: 700;
      }
      .subtitle-title {
        font-size: 24px;
        color: #11305C;
      }
    }
    .tips {
      text-align: center;
      color: #909399;
    }
    .login-captcha {
      img {
        width: 135px;
        height: 48px;
      }
    }
  }
  .login-footer {
    position: fixed;
    bottom: 0;
    left: 0;
    z-index: -1;
    background: url(../../../assets/img/login_filter.png) no-repeat top center;
    width: 100%;
    height: 166px;
    .copyright {
      position: fixed;
      bottom: 16px;
      left: 50%;
      transform: translateX(-50%);
      color: #fff;
    }
  }
}
</style>
<style lang="scss">
.login {
  .el-input__inner {
    background-color: #11305c;
    color: #fff;
    height: 48px;
  }
  .el-tabs__content {
    height: 300px;
  }
  .el-dialog__body {
    height: 350px;
  }
}
</style>
