<script setup lang="ts">
import {ref} from 'vue';
import {useRouter} from "vue-router";
import myAxios from "../API/myAxios.ts";
import {ElMessage} from "element-plus";
import {userStore} from "../stores/userStore.ts";
const router = useRouter();
const userAccount = ref('');
const userPassword= ref('');
const store = userStore();

const doLogin = async() =>{
  console.log(userAccount.value);
  console.log(userPassword.value);
  const res:any = await myAxios.post("/user/login",{
    userAccount: userAccount.value,
    userPassword: userPassword.value
  })
  ElMessage({
    showClose:true,
    type: res.code===0?'success':"error",
    message:res.code===0?'登录成功':`登录失败:${res.message}`,
  })
  console.log(res.data);
  const resone:any = await myAxios.get('/user/current', { withCredentials: true });
  console.log(resone);
  console.log(res.code);
  if(res.code === 0 && res.data) {
    await store.getLoginUser()

    await router.replace({path: '/'})
  }
};

</script>

<template>
  <div id="login">
    <header class="login-header">
      <div class="container m-top-20">
        <h1 class="logo">
          <RouterLink to="/">校快送</RouterLink>
        </h1>
      </div>
    </header>
    <section class="login-section">
      <div class="wrapper">
        <nav>
          <RouterLink to="/">登录</RouterLink>
        </nav>
        <div class="account-box">
          <div class="form">
            <el-form ref="formRef" label-position="right" label-width="60px"
                     status-icon>
              <el-form-item prop="userAccount" placeholder="请输入用户名" label="账户">
                <el-input v-model="userAccount"/>
              </el-form-item>
              <el-form-item prop="userPassword" placeholder="请输入密码" label="密码">
                <el-input v-model="userPassword"/>
              </el-form-item>
              <el-button size="large" class="subBtn" @click="doLogin">点击登录</el-button>
            </el-form>
          </div>
        </div>
      </div>
    </section>

    <footer class="login-footer">
      <div class="container">
        <p>CopyRight &copy; 校快送</p>
      </div>
    </footer>
  </div>
</template>

<style scoped lang='scss'>

.login-header {
  background: #fff;
  border-bottom: 1px solid #e4e4e4;

  .container {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
  }

  .logo {
    width: 200px;

    a {
      display: block;
      height: 90px;
      width: 100%;
      text-indent: -9999px;
      background: url("../images/logo.png") no-repeat center 18px / contain;

    }
  }

  .sub {
    flex: 1;
    font-size: 24px;
    font-weight: normal;
    margin-bottom: 38px;
    margin-left: 20px;
    color: #666;
  }

  .entry {
    width: 120px;
    margin-bottom: 38px;
    font-size: 16px;

    i {
      font-size: 14px;
      color: blue;
      letter-spacing: -5px;
    }
  }
}

.login-section {
  background: url('../images/bgi.jpg') no-repeat center / cover;
  height: 488px;
  position: relative;

  .wrapper {
    width: 380px;
    background: #fff;
    position: absolute;
    left: 50%;
    top: 54px;
    transform: translate3d(100px, 0, 0);
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.15);

    nav {
      font-size: 14px;
      height: 55px;
      margin-bottom: 20px;
      border-bottom: 1px solid #f5f5f5;
      display: flex;
      padding: 0 40px;
      text-align: right;
      align-items: center;

      a {
        flex: 1;
        line-height: 1;
        display: inline-block;
        font-size: 18px;
        position: relative;
        text-align: center;
      }
    }
  }
}

.login-footer {
  padding: 30px 0 50px;
  background: #fff;

  p {
    text-align: center;
    color: #999;
    padding-top: 20px;

    a {
      line-height: 1;
      padding: 0 10px;
      color: #999;
      display: inline-block;

      ~a {
        border-left: 1px solid #ccc;
      }
    }
  }
}

.account-box {
  .toggle {
    padding: 15px 40px;
    text-align: right;

    a {
      color: blue;

      i {
        font-size: 14px;
      }
    }
  }

  .form {
    padding: 0 20px 20px 20px;

    &-item {
      margin-bottom: 28px;

      .input {
        position: relative;
        height: 36px;

        >i {
          width: 34px;
          height: 34px;
          background: #cfcdcd;
          color: #fff;
          position: absolute;
          left: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 18px;
        }

        input {
          padding-left: 44px;
          border: 1px solid #cfcdcd;
          height: 36px;
          line-height: 36px;
          width: 100%;

          &.error {
            border-color: blue;
          }

          &.active,
          &:focus {
            border-color: blue;
          }
        }

        .code {
          position: absolute;
          right: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 14px;
          background: #f5f5f5;
          color: #666;
          width: 90px;
          height: 34px;
          cursor: pointer;
        }
      }

      >.error {
        position: absolute;
        font-size: 12px;
        line-height: 28px;
        color: blue;

        i {
          font-size: 14px;
          margin-right: 2px;
        }
      }
    }

    .agree {
      a {
        color: #069;
      }
    }

    .btn {
      display: block;
      width: 100%;
      height: 40px;
      color: #fff;
      text-align: center;
      line-height: 40px;
      background: blue;

      &.disabled {
        background: #cfcdcd;
      }
    }
  }

  .action {
    padding: 20px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .url {
      a {
        color: #999;
        margin-left: 10px;
      }
    }
  }
}

.subBtn {
  background: blue;
  border-radius: 15px;
  width: 100%;
  color: #fff;
}
</style>