<template>
  <app-statusbar class="login">
    <app-navback class="login-navback" />
    <div class="login-logo">
      <slot name="logo">
        <img :src="logoSrc" />
      </slot>
    </div>
    <Form class="login-form" @submit="formSubmit">
      <CellGroup inset>
        <Field v-model="formData.userName" name="account" :label="$t('user.login.username')" label-align="top" size="large" :placeholder="$t('user.login.Pleaseenterausername')"
          :rules="[{ required: true, message: $t('user.login.Pleaseenterausername') }]" />
        <Field v-model="formData.password" name="password" type="password" :label="$t('user.login.password')" label-align="top" size="large"
          :placeholder="$t('user.login.Pleaseenterthepassword')" :rules="[{ required: true, message: $t('user.login.Pleaseenterthepassword') }]" autocomplete="off" />
        <Field>
          <template #input>
            <SliderVerify @statu="slide" style="max-width: 100%;margin: auto;" v-if="showSliderVerify" />
          </template>
        </Field>
      </CellGroup>
      <div class="button-submit">
        <Button type="primary" native-type="submit" round block>{{ $t('user.login.login') }}</Button>
      </div>
    </Form>
    <div class="login-link">
      <span @click="navigationTo('user-register')" v-if="globalStore.hasSystemModule('register')">{{ $t('user.login.register') }}</span>
      <span @click="navigationTo('user-forget')">{{ $t('user.login.forgetpassword') }}</span>
    </div>
    <div class="login-footer">
      <div class="login-footer__trem">
        <Checkbox shape="square" icon-size="16px" v-model="checked">{{ $t('user.login.checked') }}</Checkbox>
        <span @click="routerTo('rules-zcxy')">{{ $t('user.login.ruleszcxy') }}</span>
        <span>、</span>
        <span @click="routerTo('rules-yhkhfxgzs')" v-if="showYhkhfxgzs">{{ $t('user.login.rulesyhkhfxgzs') }}</span>
        <span v-if="showYhkhfxgzs">、</span>
        <span @click="routerTo('rules-yszc')">{{ $t('user.login.rulesyszc') }}</span>
      </div>
      <div class="login-footer__version">
        <span>v1.0.{{ appVersion }}</span>
      </div>
    </div>
  </app-statusbar>
</template>

<script lang="ts" setup>
import { shallowRef } from 'vue'
import { Button, Field, CellGroup, Form, Checkbox, showFailToast, showToast } from 'vant'
import { fullloading, dialog } from '@/utils/vant'
import { useLogin } from '@/business/login'
import { useNavigation } from '@mobile/router/navigation'
import { useGlobalStore } from '@/stores'
import service from '@/services'
import SliderVerify from '@/components/base/slider-verify/index.vue' // 临时调用，待优化
import logoImage from '../../../assets/images/login-logo.png'

defineProps({
  logoSrc: {
    type: String,
    default: logoImage
  },
  showYhkhfxgzs: {
    type: Boolean,
    default: true
  }
})

const globalStore = useGlobalStore()
const { setGlobalUrlParams, routerTo } = useNavigation()
const { formData, userLogin } = useLogin()
const checked = shallowRef(false) // 是否同意协议管理
const showSliderVerify = shallowRef(true) // 验证滑块组件重载
const sliderVerifyStatus = shallowRef(false) // 滑块验证状态

const meta = document.getElementsByTagName('meta')
const appVersion = meta.namedItem('revised')?.content ?? '0'

// 导航跳转
const navigationTo = (name: string) => {
  fullloading((hideLoading) => {
    service.onReady().then(() => {
      hideLoading()
      routerTo(name)
    }).catch(() => {
      showFailToast('初始化失败')
    })
  }, '加载中...')
}

// 滑块验证 
const slide = (vfcStatu: { statu: string }) => {
  if (vfcStatu.statu === 'success') {
    sliderVerifyStatus.value = true
  } else {
    sliderVerifyStatus.value = false
  }
}

const formSubmit = () => {
  if (sliderVerifyStatus.value) {
    if (checked.value) {
      fullloading((hideLoading) => {
        userLogin().then((forcedPasswordChange) => {
          hideLoading()
          if (forcedPasswordChange) {
            dialog('为了您的账户安全，请修改密码！').then(() => {
              setGlobalUrlParams({ forcedPasswordChange })
              routerTo('user-password', true)
            })
          } else {
            routerTo('report', true)
          }
        }).catch((err) => {
          showSliderVerify.value = false
          sliderVerifyStatus.value = false
          formData.password = ''
          hideLoading(err, 'fail')

          setTimeout(() => {
            showSliderVerify.value = true
          }, 0)
        })
      }, '登录中...')
    } else {
      showToast('请先同意使用条款')
    }
  }
}
</script>

<style lang="less">
@import './index.less';
</style>