<route lang="json5" type="page">
{
  style: {
    navigationBarTitleText: '登录',
    navigationStyle: 'custom',
  },
  notNeedLogin: false,
}
</route>
<template>
  <view class="login-container relative">
    <NavBar />
    <view
      class="mt-44 text-28 pl-32 bg-[linear-gradient(180deg,#04A9F9,#23C0CF)] text-transparent"
      style="-webkit-background-clip: text"
    >
      登录
    </view>
    <view class="pt-45">
      <view class="mx-15 px-15 shadow-[0_0_10px_rgba(10,68,245,0.1)] rounded-10">
        <view class="flex items-center py-15">
          <image class="w-24 h-24" src="@/static/images/login/phone.png" mode="aspectFit" />
          <input
            type="text"
            v-model="formData.member_username"
            placeholder-class="text-#889BC6"
            class="text-right flex-1 text-14"
            placeholder="请输入用户名"
          />
        </view>
        <view class="flex items-center py-15">
          <image class="w-24 h-24" src="@/static/images/login/password.png" mode="aspectFit" />
          <input
            placeholder-class="text-#889BC6"
            type="password"
            v-model="formData.member_password"
            class="text-right flex-1 text-14"
            placeholder="请输入登录密码"
          />
        </view>
      </view>
      <!-- <view class="ml-auto mr-0 w-fit text-#04A9F9 text-14 p-15" @click="handleEditPassword">
        忘记密码
      </view> -->
      <view
        class="mt-30 text-center py-12 rounded-full mx-15 text-15 text-white btn-gradient"
        @click="handleLogin"
      >
        登录
      </view>
      <view class="mt-30 text-center text-14 text-#999">
        没有账号？
        <text class="text-#04A9F9" @click="handleRegister">立即注册</text>
      </view>
    </view>
    <view class="absolute bottom-15 left-50% -translate-x-50% text-#999 text-14">V1.0.0</view>
  </view>
</template>

<script lang="ts" setup>
import NavBar from '@/components/NavBar.vue'
import { isLogged } from '@/interceptors/route'
import { loginAPI } from '@/service'
import { useUserStore } from '@/store/user'

const userStore = useUserStore()
const formData = ref({
  member_username: '',
  member_password: '',
})

let redirect = '/'
onLoad((options) => {
  redirect = options.redirect ?? '/'
})

const handleLogin = async () => {
  if (!formData.value.member_username || !formData.value.member_password) {
    return uni.showToast({
      title: '请输入用户名和密码',
      icon: 'none',
    })
  }
  const {
    data: { token },
  } = await loginAPI(formData.value)
  userStore.setToken(token)
  uni.reLaunch({
    url: redirect,
  })
}

const handleRegister = () => {
  uni.navigateTo({
    url: '/pages/register/index',
  })
}

const handleEditPassword = () => {
  uni.navigateTo({
    url: '/pages/edit-password/index',
  })
}

onLoad(() => {
  if (isLogged()) {
    uni.reLaunch({
      url: redirect,
    })
  }
})
</script>

<style lang="scss">
.login-container {
  box-sizing: border-box;
  min-height: 100vh;
  padding-bottom: 200rpx;
  background-image: url('@/static/images/login-bg.png');
  background-repeat: no-repeat;
  background-size: cover;
}
</style>
