<template>
  <div class="login">
    <main class="login-container">
      <div class="title">登录</div>
      <el-form ref="loginForm" :model="form" :rules="rules" label-position="right">
        <el-form-item prop="server_address" label="服务器地址">
          <el-input v-model="form.server_address" placeholder="请输入服务器地址" @change="updateServerAddress" />
        </el-form-item>
        <el-form-item prop="user_name" label="用户名">
          <el-input v-model="form.user_name" />
        </el-form-item>
        <el-form-item prop="password" label="密码">
          <el-input v-model="form.password" type="password" show-password />
        </el-form-item>
        <el-form-item prop="user_type" label="用户类型">
          <el-radio-group v-model="form.user_type">
            <el-radio label="USER">机构用户</el-radio>
            <el-radio label="ORG_MANAGER">机构管理员</el-radio>
            <el-radio label="ADMIN">系统管理员</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button class="submit" type="primary" @click="signIn">登录</el-button>
        </el-form-item>
        <el-form-item>
          <div class="protocol">
            <span>
              <el-checkbox v-model="isAgree" /> 我已阅读并同意<span
                style="color: #4d78ff; cursor: pointer"
                @click="viewUserAgreement"
                >《用户协议》</span
              >
            </span>
            <span style="color: #4d78ff; cursor: pointer">忘记密码？| <a @click="goRegister">注册</a></span>
          </div>
        </el-form-item>
      </el-form>
    </main>
    <el-dialog
      :visible.sync="showUseragreement"
      width="80%"
      append-to-body
      :show-close="false"
      :close-on-click-modal="false"
      class="login-userAgree"
    >
      <userAgreement class="userAgree-login" :change-agreement="changeAgreement" />
    </el-dialog>
  </div>
</template>

<script>
import md5 from 'md5';
import { GetLogo } from '@/api/app';
import { setConfig, getLocalStore, setLocalStore } from '@/utils/auth';
import { getUserTypeToJump } from '@/router/guard';
import { updateBaseURL } from '@/utils/http';

import UserAgreement from './userAgreement.vue';

export default {
  name: 'LoginPage',
  components: { UserAgreement },
  data() {
    return {
      isAgree: false, // 是否同意用户协议
      form: {
        user_type: 'USER',
        user_name: '',
        password: '',
        is_password_md5: 'true',
        server_address: getLocalStore('server_address') || '', // 服务器地址
      },
      rules: {
        user_name: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur',
          },
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur',
          },
        ],
      },
      image_content_base64: '',
      showUseragreement: false,
    };
  },
  created() {
    this.getLogo();
  },
  methods: {
    getLogo() {
      GetLogo().then((res) => {
        setConfig(res);
      });
    },

    signIn() {
      this.$refs.loginForm.validate((valid) => {
        if (!valid) return false;
        if (!this.isAgree) {
          // "阅读并同意用户协议"
          this.$message.warning('阅读并同意用户协议');
          return;
        }
        let _form = { ...this.form, password: md5(this.form.password).toUpperCase() };
        this.$store
          .dispatch('user/login', _form)
          .then((user_type) => {
            this.$router.push({ path: getUserTypeToJump(user_type) });
          })
          .catch(() => {});
      });
    },

    // 注册
    goRegister() {
      this.$router.push({ path: '/register' });
    },
    viewUserAgreement() {
      this.showUseragreement = true;
    },
    changeAgreement(flag) {
      this.isAgree = flag;
      this.showUseragreement = false;
    },
    updateServerAddress() {
      setLocalStore('server_address', this.form.server_address);
      updateBaseURL();
    },
  },
};
</script>

<style lang="scss" scoped>
.login {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #2148c0 url('~@/assets/login/login-bg.png') no-repeat center center / cover;

  &-container {
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: -350px;
    margin-left: -200px;

    .title {
      margin-bottom: 28px;
      font-size: 24px;
      color: #fff;
      text-align: center;
    }

    :deep .el-form {
      max-width: 400px;
      padding: 24px 32px;
      margin: 0 auto;
      background-color: #fff;
      border-radius: 16px;

      &-item {
        &__label {
          margin-bottom: 4px;
          font-size: 18px;
          font-weight: bold;
          color: #000;

          &::before {
            display: none;
          }
        }
      }

      .el-input {
        &__inner {
          height: 40px;
          line-height: 40px;
          background-color: #fff;
        }
      }

      .verification-code {
        display: flex;
        flex-direction: column;

        label {
          text-align: left;
        }

        .el-input {
          width: calc(100% - 112px);
          margin-right: 10px;
        }

        .el-image {
          height: 38px;
          vertical-align: bottom;
          cursor: pointer;
        }
      }

      .submit {
        width: 100%;
      }

      .protocol {
        display: flex;
        justify-content: space-between;
        font-size: 12px;
        color: #000;
      }

      .el-form-item--small.el-form-item:last-child {
        margin-bottom: 0;
      }
    }

    .not-tips {
      margin-top: 20px;
      font-size: 12px;
      color: #fff;
      text-align: center;
      cursor: pointer;
    }
  }
}
</style>
<style lang="scss">
.login-userAgree {
  .el-dialog {
    height: 70%;
    border-radius: 8px;
  }

  .el-dialog__header {
    padding: 0;
  }

  .el-dialog__body {
    height: 100%;
    padding: 0;
    border-radius: 8px;
    box-shadow: 0 4px 4px rgba(0, 0, 0, 25%);
  }
}
</style>
