<template>
  <view class="nav">
    {{ t('login.tip1') }}
    <view class="nav-right" @click="goPage('/subPackages/register/register')">{{ t('login.tip2') }}</view>
  </view>

  <view class="container">
    <view class="wrap mx-[1.88rem]">
      <img class="bg" :src="bg" />
      <view class="title">{{ t('login.tip3') }}</view>
      <view class="mas">
        <view class="input_wrap">
          <view class="icon">
            <image src="/src/static/image/login/1.png" mode="widthFix"></image>
          </view>
          <input v-model="loginParams.account" :placeholder="t('login.account_placeholder')" />
        </view>
        <view class="input_wrap">
          <view class="icon">
            <image src="/src/static/image/login/2.png" mode="widthFix"></image>
          </view>
          <input v-model="loginParams.password" :placeholder="t('login.password_placeholder')" type="password" />
        </view>
      </view>
      <view class="button-wrap">
        <view class="button" @click="login">{{ t('login.button_text') }}</view>
        <view class="text" @click="goPage('/subPackages/register/register')">{{ t('login.register') }}</view>
      </view>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { useI18n } from 'vue-i18n'
import { loginApi } from '@/api/login/login'
import { switchTab, goPage, checkInput } from '@/common/common'
import { useCounterStore } from '@/store/store'
import { showSuccessToast } from 'vant'
import bg from '@/static/logo.png'
const { t } = useI18n()
const store = useCounterStore()

const loginParams = ref({
  account: '',
  password: ''
})

const checkArr = [
  { key: 'account', message: t('login.account_error') },
  { key: 'password', message: t('login.password_error') }
]

const login = async () => {
  if (!checkInput(checkArr, loginParams.value)) {
    return
  }
  uni.showLoading({ mask: true })
  const res = await loginApi(loginParams.value)
  uni.hideLoading()
  if (res.code === 1) {
    uni.setStorageSync('token', res.data.userinfo.token)
    uni.setStorageSync('userId', res.data.userinfo.id)
    uni.setStorageSync('userInfo', res.data.userinfo)
    store.token = res.data.userinfo.token
    store.userId = res.data.userinfo.userId
    store.getUserInfo()
    showSuccessToast(res.msg)
    setTimeout(() => {
      switchTab('/pages/index/index')
    }, 1000)
  }
}
</script>

<style lang="scss" scoped>
body {
  background-image: url('/src/static/image/login/bg1.png');
}
.nav {
  width: 100%;
  height: 3.125rem;
  color: #fff;
  // background: #fff;
  font-size: 1.0625rem;
  display: flex;
  justify-content: center;
  align-items: center;
  position: sticky;
  top: 0;
  left: 0;
  .nav-right {
    position: absolute;
    top: 0.8438rem;
    right: 1.7813rem;
    font-size: 1.0625rem;
    color: #fff;
  }
}
.container {
  text-align: center;
  overflow-x: hidden;
  margin: 0 auto;
  // background-color: #fff;
  // height: 100vh;
  .bg {
    width: 13.69rem;
    // height: 14.5833rem;
    margin: 3rem auto;
  }
  .title {
    color: #45d9fd;
    font-size: 1.125rem;
    font-weight: 600;
    margin-top: 0.6875rem;
  }
}
.logo {
  width: 7.5rem;
  height: 7.5rem;
  // border-radius: 50%;
  //  background-color: $uni-color-primary;
  margin: 14vh auto 0;
}
.name {
  margin: 2.3vh auto 9.54vh;
  font-size: 1.5rem;
  color: $uni-color-primary;
}
.button-wrap {
  width: 20rem;
  margin-top: 1.875rem;
  .text {
    color: #fff;
    font-size: 0.9375rem;
    line-height: 3.125rem;
  }
  .button {
    width: 100%;
  }
}
.mas {
  width: 20rem;
  height: 7.375rem;
  background: #00316b;
  border-radius: 1.25rem;
  margin-top: 1.8125rem;
}
.input_wrap {
  display: flex;
  align-items: center;
  height: 3.6875rem;
  border-radius: 1.25rem;
  // border: 0.03rem solid $uni-color-primary;
  padding: 0 0.88rem;
  max-width: 20rem;
  margin-left: auto;
  margin-right: auto;
  font-size: 1rem;
  .uni-input-input {
    color: #fff;
  }
  .icon {
    width: 1.1875rem;
    height: 1.1875rem;
    margin-right: 0.94rem;
    image {
      width: 100%;
      height: 100%;
      display: block;
    }
  }
  input {
    flex: 1;
    text-align: left;
    color: $uni-color-primary;
    .uni-input-placeholder {
      color: #b3b6c1;
    }
  }
}
.text_button {
  max-width: 20rem;
  font-size: $uni-font-size-1;
  color: $uni-color-primary;
  display: flex;
  justify-content: space-between;
  margin: 0.31rem auto;
  view {
    padding: 0.31rem;
  }
}
.button {
  // max-width: 20rem;
  width: 11.25rem;
  height: 3.125rem;
  background-color: #6a69e9;
  border-radius: 1.34rem;
  display: flex;
  justify-content: center;
  align-items: center;
  // margin: 0 auto;
  color: #fff;
  // margin-top: 4rem;
  // margin-bottom: 2rem;
}
</style>
