<script lang="ts" setup>
  import { useUserStore } from '@/store/modules/user'
  import { useSettingsStore } from '@/store/modules/settings'
  import { translate } from '@/i18n'
  import { toLoginRoute } from '@/utils/routes'
  import { getUnlock } from './api/index'
  import { ElMessage } from 'element-plus'
  import { VabRoute } from '/#/router'
  const vFocus: any = {
    mounted(el: HTMLElement) {
      el.querySelector('input')?.focus()
    },
  }

  const userStore = useUserStore()
  const { avatar, id, username } = storeToRefs(userStore)
  const { logout } = userStore
  const settingsStore = useSettingsStore()
  const { theme, lock, title } = storeToRefs(settingsStore)
  const { handleLock: _handleLock, handleUnLock: _handleUnLock } = settingsStore

  const route: VabRoute = useRoute()
  const router = useRouter()
  const background = ref(
    'https://fastly.jsdelivr.net/gh/' +
      'chuzh' +
      'ixin/image/vab-im' +
      'age-lock/' +
      `${Math.round(Math.random() * 31)}.jpg`
  )
  const randomBackground = () => {
    background.value =
      'https://fastly.jsdelivr.net/gh/' +
      'chuzh' +
      'ixin/image/vab-im' +
      'age-lock/' +
      `${Math.round(Math.random() * 31)}.jpg`
  }

  const validatePass = (rule: any, value: string, callback: any) => {
    if (value === '' || value !== '123456') {
      callback(new Error('请输入正确的密码'))
    } else {
      callback()
    }
  }

  const formRef = ref()
  const lockpwdStatus = ref()
  const form = ref({
    password: '',
  })
  const rules = {
    password: [{ required: true, trigger: 'blur', message: '请输入密码' }],
  }

  let lockIcon = true
  const handleUnLock = () => {
    formRef.value.validate(async (valid: boolean) => {
      if (valid) {
        lockIcon = false
        setTimeout(async () => {
          await unLockApi()
          if (!lockpwdStatus.value) {
            ElMessage.error('密码错误，请重新输入！')
            return
          }
          await _handleUnLock()
          lockIcon = true
          form.value.password = ''
          await randomBackground()
          const el = document.querySelector('.vab-side-bar') as HTMLElement
          if (el) el.removeAttribute('style')
        }, 500)
      }
    })
  }
  //返回登录页
  const backLogin = async () => {
    await _handleUnLock()
    await logout()
    localStorage.removeItem('dictsSetting')
    localStorage.removeItem('factory')
    localStorage.removeItem('factory_list')
    await router.push(toLoginRoute(route.fullPath))
  }
  //解锁接口
  const unLockApi = async () => {
    const data = {
      id: id.value,
      password: btoa(form.value.password),
    }
    await getUnlock(data).then((res) => {
      lockpwdStatus.value = res.data.status
    })
  }
  const handleLock = () => {
    _handleLock()
    const el = document.querySelector('.vab-side-bar') as HTMLElement
    if (el) el.style.display = 'none'
  }
</script>

<template>
  <vab-icon v-if="theme.showLock" icon="lock-line" @click="handleLock" />
  <transition v-if="theme.showLock" mode="out-in" name="fade-transform">
    <div v-if="lock" class="vab-screen-lock">
      <div
        class="vab-screen-lock-background"
        :style="{
          background: `fixed url(${background}) center`,
          backgroundSize: '100% 100%',
          filter: 'blur(10px)',
        }"
      ></div>

      <div class="vab-screen-lock-content">
        <div class="vab-screen-lock-content-title">
          <el-avatar :size="100" :src="avatar" />
          <div>{{ username }}</div>
          <vab-icon :icon="lockIcon ? 'lock-line' : 'lock-unlock-line'" />
          {{ title }} {{ translate('屏幕已锁定') }}
        </div>

        <div class="vab-screen-lock-content-form">
          <el-form ref="formRef" :model="form" :rules="rules" @submit.prevent>
            <el-form-item label="" :label-width="0" prop="password">
              <el-input
                v-model="form.password"
                v-focus
                autocomplete="off"
                placeholder="请输入登录密码"
                show-password
                style="width: 80%; padding-right: 10px"
                type="password"
              >
                <!-- <template #suffix>

                </template> -->
              </el-input>
              <el-button
                native-type="submit"
                style="height: 42px; line-height: 42px"
                type="primary"
                @click="handleUnLock"
              >
                <vab-icon :icon="lockIcon ? 'lock-line' : 'lock-unlock-line'" />
                {{ translate('解锁') }}
              </el-button>
            </el-form-item>
          </el-form>
        </div>
        <span @click="randomBackground">{{ translate('切换壁纸') }}</span>
        <span style="margin-left: 30px" @click="backLogin">返回登录页</span>
      </div>
    </div>
  </transition>
</template>

<style lang="scss" scoped>
  .vab-screen-lock {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: $base-z-index;
    // z-index: 9999999;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    background-color: rgba(255, 255, 255, 0.6);
    transition: $base-transition;
    backdrop-filter: blur(10px);

    &-background {
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      z-index: $base-z-index - 1;
    }

    &-content {
      z-index: $base-z-index;
      padding: 40px 95px 40px 95px;
      color: #252a30;
      text-align: center;
      background: rgba(255, 255, 255, 0.6);
      backdrop-filter: blur(10px);
      border-radius: 15px;

      > span {
        font-size: $base-font-size-small;
        cursor: pointer;
      }

      &-title {
        line-height: 50px;
        color: #252a30;
        text-align: center;

        .ri-lock-line,
        .ri-lock-unlock-line {
          display: block;
          margin: auto !important;
          font-size: 30px;
          color: #252a30 !important;
          transition: $base-transition;
        }
      }

      &-form {
        :deep() {
          .el-input__inner {
            width: 280px;
            height: 40px;
            line-height: 40px;
          }

          .el-input__wrapper {
            // padding-right: 0;
            .el-input__suffix {
              right: 0;

              .el-button {
                height: 40px;
                line-height: 40px;
                border-top-left-radius: 0;
                border-bottom-left-radius: 0;
                i {
                  margin-left: 0 !important;
                }
              }

              .el-input__validateIcon {
                display: none;
              }
            }
          }
        }
      }
    }

    @media (max-width: 576px) {
      .vab-screen-lock-content {
        width: auto !important;
        margin: 5vw;
      }
    }
  }
</style>
