<script setup lang="tsx">
import { reactive, ref, watch, onMounted, unref } from 'vue'
import { Form, FormSchema } from '../../../components/Form'
import { useI18n } from '../../../hooks/web/useI18n'
import { ElCheckbox, ElLink, ElMessage } from 'element-plus'
import { useForm } from '../../../hooks/web/useForm'
import { myLogin, getMenuList, getAuthBtn } from '../../../api/login'
import { useAppStore } from '../../../store/modules/app'
import { usePermissionStore } from '../../../store/modules/permission'
import { useRouter } from 'vue-router'
import type { RouteLocationNormalizedLoaded, RouteRecordRaw } from 'vue-router'
import { UserType } from '../../../api/login/types'
import { useValidator } from '../../../hooks/web/useValidator'
// import { Icon } from '@common/src/components/Icon'
import { useUserStore } from '../../../store/modules/user'
import { BaseButton } from '../../../components/Button'
// import { getMenuChildren } from '@common/src/utils'
// import { routes } from 'vue-router/vue-router-auto-routes'
import { filterRouters, getMenuChildren, getBtnAuth } from '@common/utils'

const SYS_CODE = import.meta.env.VITE_API_SYS_CODE

const { required } = useValidator()

const emit = defineEmits(['to-register'])

const appStore = useAppStore()

const userStore = useUserStore()

const permissionStore = usePermissionStore()

const { currentRoute, addRoute, push } = useRouter()

const { t } = useI18n()

const rules = {
  username: [required()],
  password: [required()]
}

const schema = reactive<FormSchema[]>([
  {
    field: 'title',
    colProps: {
      span: 24
    },
    formItemProps: {
      slots: {
        default: () => {
          return <h2 class="text-2xl font-bold text-center w-[100%]">{t('login.login')}</h2>
        }
      }
    }
  },
  {
    field: 'username',
    label: t('login.username'),
    // value: 'admin',
    component: 'Input',
    colProps: {
      span: 24
    },
    componentProps: {
      placeholder: 'admin or test'
    }
  },
  {
    field: 'password',
    label: t('login.password'),
    // value: 'admin',
    component: 'InputPassword',
    colProps: {
      span: 24
    },
    componentProps: {
      style: {
        width: '100%'
      },
      placeholder: 'admin or test'
    }
  },
  {
    field: 'tool',
    colProps: {
      span: 24
    },
    formItemProps: {
      slots: {
        default: () => {
          return (
            <>
              <div class="flex justify-between items-center w-[100%]">
                <ElCheckbox v-model={remember.value} label={t('login.remember')} size="small" />
                {/* <ElLink type="primary" underline={false}>
                  {t('login.forgetPassword')}
                </ElLink> */}
              </div>
            </>
          )
        }
      }
    }
  },
  {
    field: 'login',
    colProps: {
      span: 24
    },
    formItemProps: {
      slots: {
        default: () => {
          return (
            <>
              <div class="w-[100%]">
                <BaseButton
                  loading={loading.value}
                  type="primary"
                  class="w-[100%]"
                  onClick={signIn}
                >
                  {t('login.login')}
                </BaseButton>
              </div>
              {/* <div class="w-[100%] mt-15px">
                <BaseButton class="w-[100%]" onClick={toRegister}>
                  {t('login.register')}
                </BaseButton>
              </div> */}
            </>
          )
        }
      }
    }
  }
  // {
  //   field: 'other',
  //   component: 'Divider',
  //   label: t('login.otherLogin'),
  //   componentProps: {
  //     contentPosition: 'center'
  //   }
  // },
  // {
  //   field: 'otherIcon',
  //   colProps: {
  //     span: 24
  //   },
  //   formItemProps: {
  //     slots: {
  //       default: () => {
  //         return (
  //           <>
  //             <div class="flex justify-between w-[100%]">
  //               <Icon
  //                 icon="vi-ant-design:github-filled"
  //                 size={iconSize}
  //                 class="cursor-pointer ant-icon"
  //                 color={iconColor}
  //                 hoverColor={hoverColor}
  //               />
  //               <Icon
  //                 icon="vi-ant-design:wechat-filled"
  //                 size={iconSize}
  //                 class="cursor-pointer ant-icon"
  //                 color={iconColor}
  //                 hoverColor={hoverColor}
  //               />
  //               <Icon
  //                 icon="vi-ant-design:alipay-circle-filled"
  //                 size={iconSize}
  //                 color={iconColor}
  //                 hoverColor={hoverColor}
  //                 class="cursor-pointer ant-icon"
  //               />
  //               <Icon
  //                 icon="vi-ant-design:weibo-circle-filled"
  //                 size={iconSize}
  //                 color={iconColor}
  //                 hoverColor={hoverColor}
  //                 class="cursor-pointer ant-icon"
  //               />
  //             </div>
  //           </>
  //         )
  //       }
  //     }
  //   }
  // }
])

// const iconSize = 30

const remember = ref(userStore.getRememberMe)

const initLoginInfo = () => {
  const loginInfo = userStore.getLoginInfo
  console.log(loginInfo)
  if (loginInfo) {
    const { username, password } = loginInfo
    setValues({ username, password })
  }
}
initLoginInfo()

const { formRegister, formMethods } = useForm()
const { getFormData, getElFormExpose, setValues } = formMethods

const loading = ref(false)

// const iconColor = '#999'

// const hoverColor = 'var(--el-color-primary)'

const redirect = ref<string>('')

watch(
  () => currentRoute.value,
  (route: RouteLocationNormalizedLoaded) => {
    redirect.value = route?.query?.redirect as string
  },
  {
    immediate: true
  }
)

// 登录
const signIn = async () => {
  push({ path: redirect.value })
  return

  // appStore.setMenuTheme('#fff')
  const formRef = await getElFormExpose()
  await formRef?.validate(async (isValid) => {
    if (isValid) {
      loading.value = true
      const formData = await getFormData<UserType>()
      console.log(formData)
      const pramsData = {
        loginType: 1,
        userNo: formData.username,
        password: formData.password,
        phone: ''
      }
      try {
        const res = await myLogin(pramsData)
        console.log(res)
        if (res) {
          // 是否记住我
          if (unref(remember)) {
            userStore.setLoginInfo({
              username: formData.username,
              password: formData.password
            })
          } else {
            userStore.setLoginInfo(undefined)
          }
          userStore.setRememberMe(unref(remember))
          userStore.setUserInfo(res.data)
          userStore.setToken(res.data.accessToken)
          // 是否使用动态路由
          console.log(appStore.getDynamicRouter)
          if (appStore.getDynamicRouter) {
            getRole()
          } else {
            await permissionStore.generateRoutes('static').catch(() => {})
            permissionStore.getAddRouters.forEach((route) => {
              addRoute(route as RouteRecordRaw) // 动态添加可访问路由表
            })
            permissionStore.setIsAddRouters(true)
            push({ path: redirect.value || permissionStore.addRouters[0].path })
          }
        }
      } finally {
        loading.value = false
      }
    }
  })
}

// 获取角色信息
const getRole = async () => {
  const res = await getMenuList({})
  const btn = await getAuthBtn({})
  if (res) {
    console.log(res.data)
    console.log(res.data[1]?.children)
    // const routers = filterRouters(res.data[1]?.children) || []
    const routers = filterRouters(getMenuChildren(res.data, SYS_CODE)) || []
    const btnAuth = getBtnAuth(btn.data) || []
    if (!routers.length) {
      ElMessage.error('暂无菜单权限')
      return
    }
    // const routers = res.data || []
    userStore.setRoleRouters(routers)
    userStore.setBtnAuth(btnAuth)
    appStore.getDynamicRouter && appStore.getServerDynamicRouter
      ? await permissionStore.generateRoutes('server', routers).catch(() => {})
      : await permissionStore.generateRoutes('frontEnd', routers).catch(() => {})

    permissionStore.getAddRouters.forEach((route) => {
      addRoute(route as RouteRecordRaw) // 动态添加可访问路由表
    })
    permissionStore.setIsAddRouters(true)
    push({ path: redirect.value || permissionStore.addRouters[0].path })
  }
}

// const filterRouters = (routes) => {
//   routes?.forEach((route) => {
//     if (!route.component) {
//       route = null
//       return
//     }
//     route.meta = { ...route.meta, ...route?.meta?.extend }
//     if (route?.children) {
//       filterRouters(route?.children)
//     }
//   })
//   return routes
// }

// const btnAuth: any[] = []

// const getBtnAuth = (data) => {
//   data?.forEach((item) => {
//     if (item?.children) {
//       getBtnAuth(item?.children)
//     } else {
//       if (item?.meta?.buttons) {
//         const btn: any[] = []
//         item?.meta?.buttons?.forEach((button: any) => {
//           if (button?.extend?.commonName) {
//             btn.push(button?.extend?.commonName)
//           }
//         })
//         btnAuth.push({
//           path: item?.path,
//           btns: btn
//         })
//         console.log(btn)
//         console.log(btnAuth)
//       } else {
//         return
//       }
//     }
//   })
//   return btnAuth
// }

// const getBtnAuth = (data) => {
//   return data?.map((item) => {
//     return {
//       path: item.path,
//       btns: item.buttons
//     }
//   })
// }

// 去注册页面
// const toRegister = () => {
//   emit('to-register')
// }
</script>

<template>
  <Form
    :schema="schema"
    :rules="rules"
    label-position="top"
    hide-required-asterisk
    size="large"
    class="dark:(border-1 border-[var(--el-border-color)] border-solid)"
    @register="formRegister"
  />
</template>
