<template>
  <div class="container">
    <div class="box">
      <h1>{{ systemTitle }}</h1>
      <el-form class="form">
        <el-input v-model="form.codeRs" maxlength="50" placeholder="工号" size="large" @keyup.enter="submit">
          <template #prepend>
            <i class="sfont system-xingmingyonghumingnicheng"></i>
          </template>
        </el-input>
        <el-input
          ref="password"
          v-model="form.password"
          :type="passwordType"
          codeRs="password"
          maxlength="50"
          placeholder="密码（默认密码为：123456）"
          size="large"
          @keyup.enter="submit"
        >
          <template #prepend>
            <i class="sfont system-mima"></i>
          </template>
          <template #append>
            <i :class="passwordType ? 'system-yanjing-guan' : 'system-yanjing'" class="sfont password-icon" @click="passwordTypeChange"></i>
          </template>
        </el-input>
        <el-button size="large" style="width: 100%" type="primary" @click="submit">登录</el-button>
      </el-form>
      <div class="hospital-name">长沙泰和医院<i>·</i><span>H43010500370</span></div>
    </div>
  </div>
</template>

<script>
import { systemTitle } from '@/config'
import { onActivated, reactive, ref } from 'vue'
import { useStore } from 'vuex'
import { useRoute, useRouter } from 'vue-router'
import { addRoutes } from '@/router'
import { ElMessage } from 'element-plus'
import { closeWebSocket } from '@/utils/websocket'

export default {
  name: 'Login',
  setup() {
    const store = useStore()
    const router = useRouter()
    const route = useRoute()
    const form = reactive({
      codeRs: '',
      password: '',
    })
    const passwordType = ref('password')
    const passwordTypeChange = () => {
      passwordType.value === '' ? (passwordType.value = 'password') : (passwordType.value = '')
    }
    const checkForm = () => {
      return new Promise((resolve, reject) => {
        if (form.codeRs === '') {
          ElMessage.warning({
            message: '用户名不能为空',
            type: 'warning',
          })
          return
        }
        if (form.password === '') {
          ElMessage.warning({
            message: '密码不能为空',
            type: 'warning',
          })
          return
        }
        resolve(true)
      })
    }
    const submit = () => {
      checkForm().then(() => {
        let params = {
          codeRs: form.codeRs,
          password: form.password,
        }
        store.dispatch('user/login', params).then(() => {
          addRoutes()
          router.push(route.query.redirect || '/')
        })
      })
    }

    onActivated(() => {
      closeWebSocket()
      localStorage.clear()
    })

    return {
      systemTitle,
      form,
      passwordType,
      passwordTypeChange,
      submit,
    }
  },
}
</script>

<style lang="scss" scoped>
.container {
  position: relative;
  width: 100vw;
  height: 100vh;
  background-color: #eef0f3;

  .box {
    width: 500px;
    position: absolute;
    left: 50%;
    top: 50%;
    background: white;
    border-radius: 8px;
    transform: translate(-50%, -50%);
    height: 440px;
    overflow: hidden;
    box-shadow: 0 6px 20px 5px rgba(152, 152, 152, 0.1), 0 16px 24px 2px rgba(117, 117, 117, 0.14);

    h1 {
      margin-top: 80px;
      text-align: center;
    }

    .form {
      width: 80%;
      margin: 50px auto 15px;

      .el-input {
        margin-bottom: 20px;
      }

      .password-icon {
        cursor: pointer;
        color: #409eff;
      }
    }

    .fixed-top-right {
      position: absolute;
      top: 10px;
      right: 10px;
    }

    .hospital-name {
      position: absolute;
      right: 10%;
      bottom: 20px;
      color: rgb(100, 100, 100);
      font-size: 13px;

      i {
        margin-left: 4px;
        font-weight: bold;
        color: gray;
      }

      span {
        color: rgb(112, 112, 112);
        margin-left: 4px;
        font-size: 10px;
      }
    }
  }
}

@media screen and (max-width: 750px) {
  .container .box {
    width: 100vw;
    height: 100vh;
    box-shadow: none;
    left: 0;
    top: 0;
    transform: none;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;

    h1 {
      margin-top: 0;
    }
  }
}
</style>
