<!--  -->
<template>
  <v-card class="pa-5 d-flex flex-column align-center" :class="{'card-box': !props.hideWidth}" :elevation="props.elevation">
    <div class="resume-header mt-15 mb-8" style="width: 700px;">
      <div class="resume-title">修改登录密码</div>
    </div>
    <CtForm ref="CtFormRef" :items="formItems" style="width: 700px;"></CtForm>
    <div class="mb-15">
      <v-btn class="buttons mt-5" color="primary" :loading="loading" @click.stop="handleCommit">保存</v-btn>
      <v-btn v-if="!props.hideGoBack" class="mt-3" color="primary" variant="text" @click="router.go(-1)">返回</v-btn>
    </div>
  </v-card>

  <Loading :visible="overlay"></Loading>
</template>

<script setup>
import { ref } from 'vue'
import Snackbar from '@/plugins/snackbar'
// import { useI18n } from '@/hooks/web/useI18n'
import { useRouter } from 'vue-router'; const router = useRouter()
import { entUpdatePassword } from '@/api/enterprise'
const props = defineProps({
  entChangePassword: {
    type: Boolean,
    default: false
  },
  hideGoBack: {
    type: Boolean,
    default: false
  },
  hideWidth: {
    type: Boolean,
    default: false
  },
  elevation: {
    type: String,
    default: '2'
  }
})

defineOptions({name: 'staff-changePassword'})

// const { t } = useI18n() // 强制修改密码不能使用

const CtFormRef = ref()
const formItems = ref({
  options: [
    {
      type: 'text',
      key: 'password',
      value: '',
      label: '请输入新密码 *',
      rules: [
        value => {
          if (value) return true
          return '请输入新密码'
        },
        value => {
          if (!(/^[\s]+$/.test(value))) return true
          return '请输入新密码'
        }
      ]
    },
    {
      type: 'text',
      key: 'passwordConfirm',
      value: '',
      label: '请再次输入新密码 *',
      rules: [
        value => {
          if (value) return true
          return '请再次输入新密码'
        },
        value => {
          if (!(/^[\s]+$/.test(value))) return true
          return '请再次输入新密码'
        }
      ]
    },
  ]
})

const loading = ref(false)
const handleCommit = async () => {
  const { valid } = await CtFormRef.value.formRef.validate()
  if (!valid) return
  
  const params = {}
  const baseInfo = JSON.parse(localStorage.getItem('entBaseInfo') || "null") || null
  if (baseInfo?.id) params.id = baseInfo.id

  formItems.value.options.forEach(e => { params[e.key] = e.value })
  // 邮箱登录密码校验
  if (params.password !== params.passwordConfirm) return Snackbar.warning('两次输入的密码不一致，请确认')

  loading.value = true
  await entUpdatePassword(params)
  Snackbar.success('修改成功')
  if (props.entChangePassword) {
    localStorage.setItem('entUpdatePassword', 'doNotNeedChange')
    setTimeout(() => {
      loading.value = false
      window.location.href = '/recruit/enterprise'
    }, 1500)
  } else {
    loading.value = false
    router.go(-1)
  }
}

</script>
<style lang="scss" scoped>
</style>
