<template>
  <div>
    <div class="login_top"></div>
    <div class="login_out">
<!--      <div class="login_logo"></div>-->
    </div>
    <div class="login_content">
      <div class="">您好~</div>
      <div style="margin-bottom: 20px">欢迎使用</div>
      <div class="form_item">
        <input class="form_input" v-model="accountName" type="text" placeholder="请输入登录名">
      </div>
      <div class="form_item">
        <input class="form_input" v-model="password"  type="password" placeholder="请输入密码">
      </div>
      <div class="form_button">
        <nut-button type="success" @click="submit" block>登录</nut-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login"
}
</script>
<script setup>
  import { ref } from 'vue';
  import { goLogin, getLoginUser } from "../../utils/api";
  import { useRouter } from "vue-router";
  import { showToast } from "@nutui/nutui";
  import smCrypto from "../../utils/smCrypto";
  import tool from "../../utils/tool";

  const router = useRouter();

  const accountName = ref('');
  const password = ref('');



  const submit = () => {
    let params = {
      account: accountName.value,
      password: smCrypto.doSm2Encrypt(password.value),
      validCode: '',
      validCodeReqNo: '',
      type: 1
    }
    goLogin(params).then(res => {
      if(res.code === 200) {
        showToast.success('登录成功, 即将跳转')
        afterLogin(res.data)
        // setTimeout(() => {
        //   router.replace('/');
        // }, 500)
      } else {
        showToast.fail(res.msg);
      }
    })
  }

  const afterLogin = async (loginToken) => {
    tool.data.set('snowy_token', loginToken)
    const loginUser = await getLoginUser()
    tool.data.set('snowy_user_info', loginUser.data)
    tool.data.set('snowy_org_id', loginUser.data.lastLoginOrgId)
    await router.replace('/')
  }
</script>

<style scoped>
  .login_top {
    width: 100%;
    height: 80px;
  }
  .login_out {
    width: 100%;
    height: 140px;
    position: relative;
  }
  .login_logo {
    width: 85px;
    height: 78px;
    /*background: url('https://dam.img.ifarmcloud.com:8030/images/tongwei/logo.png') no-repeat;*/
    /*background-size: 100% 100%;*/
    background: url("../../assets/logo.png") no-repeat;
    background-size: 100% 100%;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }

  .login_content {
    width: 100%;
    box-sizing: border-box;
    padding: 16px;
  }
  .form_item {
    width: 100%;
    height: 54px;
    background: #FFFFFF;
    box-shadow: 0px 8px 30px 0px rgba(0,0,0,0.06);
    border-radius: 15px;
    box-sizing: border-box;
    padding: 0 14px;
    position: relative;
    margin-bottom: 20px;
  }

  .form_input {
    width: 100%;
    height: 54px;
    line-height: 54px;
    font-size: 16px;
    border: none;
    background: none;
    margin: 0;
    padding: 0;
  }
  .form_input:focus {
    outline: none;
  }
  .form_button {
    width: 100%;
    box-sizing: border-box;
    padding: 0 30px;
  }
</style>
