<template>
  <div class="auth-page login-page">
    <div class="auth-split">
      <div class="auth-brand" aria-hidden="true">
        <div class="auth-brand__accent" />
        <div class="auth-brand__inner">
          <img class="auth-brand__logo" src="/brand-logo.png" alt="安澜应急指挥" />
          <h1 class="auth-brand__title">安澜应急指挥</h1>
          <p class="auth-brand__tagline">事件处置 · 调度 · 资源协同</p>
          <ul class="auth-brand__bullets">
            <li>统一事件与调度协同</li>
            <li>预案、资源与值守一体</li>
            <li>态势展示可扩展接入</li>
          </ul>
        </div>
      </div>
      <div class="auth-form-side">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
          <div class="form-head">
            <h2 class="form-title">{{ title }}</h2>
            <p class="form-sub">登录以进入工作台</p>
          </div>
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              type="text"
              auto-complete="off"
              placeholder="账号"
            >
              <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              auto-complete="off"
              placeholder="密码"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="code" v-if="captchaEnabled">
            <el-input
              v-model="loginForm.code"
              auto-complete="off"
              placeholder="验证码"
              style="width: 63%"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
            </el-input>
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" class="login-code-img">
            </div>
          </el-form-item>
          <el-checkbox v-model="loginForm.rememberMe" class="remember">记住密码</el-checkbox>
          <el-form-item class="submit-row">
            <el-button
              :loading="loading"
              size="medium"
              type="primary"
              class="submit-btn"
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
            <div v-if="register" class="extra-link">
              <router-link class="link-type" :to="'/register'">立即注册</router-link>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <footer class="auth-footer">
      <span>安澜应急指挥平台</span>
    </footer>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login"
import Cookies from "js-cookie"
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      title: process.env.VUE_APP_TITLE,
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      captchaEnabled: true,
      register: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
    this.getCookie()
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img
          this.loginForm.uuid = res.uuid
        }
      })
    },
    getCookie() {
      const username = Cookies.get("username")
      const password = Cookies.get("password")
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 })
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
          } else {
            Cookies.remove("username")
            Cookies.remove("password")
            Cookies.remove('rememberMe')
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{})
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$brand: #242424;
$indigo: #4f46e5;

.auth-page {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  background: var(--color-background, #fcfcfc);
}

.auth-split {
  flex: 1;
  display: flex;
  min-height: 100vh;
  width: 100%;
}

.auth-brand {
  position: relative;
  flex: 1 1 42%;
  min-width: 280px;
  background: $brand;
  color: #fafafa;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 48px 40px;
  overflow: hidden;
}

.auth-brand__accent {
  position: absolute;
  top: -20%;
  right: -10%;
  width: 60%;
  height: 70%;
  background: radial-gradient(ellipse at center, rgba($indigo, 0.35) 0%, transparent 70%);
  pointer-events: none;
}

.auth-brand__inner {
  position: relative;
  z-index: 1;
  max-width: 360px;
}

.auth-brand__logo {
  width: 56px;
  height: 56px;
  object-fit: contain;
  border-radius: 12px;
  margin-bottom: 16px;
  display: block;
}

.auth-brand__title {
  margin: 0 0 8px 0;
  font-size: 28px;
  font-weight: 700;
  letter-spacing: 0.12em;
  color: #fafafa;
}

.auth-brand__tagline {
  margin: 0 0 28px 0;
  font-size: 14px;
  color: #a1a1aa;
  font-weight: 500;
}

.auth-brand__bullets {
  list-style: none;
  margin: 0;
  padding: 0;
  li {
    position: relative;
    padding-left: 20px;
    margin-bottom: 12px;
    font-size: 14px;
    line-height: 1.5;
    color: #d4d4d8;
    &:before {
      content: "";
      position: absolute;
      left: 0;
      top: 0.55em;
      width: 6px;
      height: 6px;
      border-radius: 50%;
      background: $indigo;
      box-shadow: 0 0 0 3px rgba($indigo, 0.25);
    }
  }
}

.auth-form-side {
  flex: 1 1 58%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 32px 24px 48px;
  background: linear-gradient(180deg, #fafafa 0%, #f4f4f5 100%);
}

.login-form {
  width: 100%;
  max-width: 400px;
  border-radius: 16px;
  background: #fff;
  padding: 32px 28px 24px;
  box-shadow: 0 20px 50px rgba(15, 23, 42, 0.08);
  border: 1px solid var(--color-border, #e4e4e7);
  .el-input {
    height: 40px;
    input {
      height: 40px;
    }
  }
  .input-icon {
    height: 40px;
    width: 14px;
    margin-left: 2px;
  }
}

.form-head {
  margin-bottom: 20px;
}

.form-title {
  margin: 0 0 6px 0;
  font-size: 22px;
  font-weight: 700;
  color: #0f172a;
  letter-spacing: 0.06em;
}

.form-sub {
  margin: 0;
  font-size: 13px;
  color: #71717a;
}

.remember {
  margin: 0 0 20px 0;
}

.submit-row {
  width: 100%;
  margin-bottom: 0;
}

.submit-btn {
  width: 100%;
}

.extra-link {
  text-align: right;
  margin-top: 12px;
}

.login-code {
  width: 33%;
  height: 40px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.login-code-img {
  height: 40px;
}

.auth-footer {
  flex-shrink: 0;
  text-align: center;
  padding: 12px 16px 20px;
  font-size: 12px;
  color: #71717a;
  letter-spacing: 0.06em;
}

@media (max-width: 900px) {
  .auth-split {
    flex-direction: column;
    min-height: auto;
  }
  .auth-brand {
    flex: none;
    min-height: 220px;
    padding: 32px 24px;
  }
  .auth-form-side {
    flex: none;
    padding: 24px 16px 32px;
  }
}
</style>
