<template>
  <div class="grid full change-pwd-view">
    <div class="change-pwd-modal">
      <div class="p-l-large change-pwd-modal-header">请修改密码</div>
      <div class="change-pwd-modal-content">
        <base-form ref="formRef" :model="userModel" :rules="rules" :items="items" :label-width="'90px'" size="large">
          <template #form-item-repassword>
            <el-input
              ref="rePwdRef"
              v-model="userModel.rePassword"
              type="password"
              clearable
              show-password
              @keydown="rePwdKeyDown"
            ></el-input>
          </template>
          <template #form-item-confirm>
            <confirm-button class="m-t-large" size="large" @confirm="onSubmit" @cancel="onCancel"></confirm-button>
          </template>
        </base-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts" name="ChangePassword">
import { reactive, watch, ref } from 'vue'
import { ElMessage, ElInput } from 'element-plus'
import useFetch from '@/hooks/useFetch'
import useVModel from '@/hooks/useVModel'
import useVW from '@/hooks/useVW'
import useForm from '@/hooks/useForm'
import BaseForm from '@/components/element/BaseForm.vue'
import ConfirmButton from '@/components/common/ConfirmButton.vue'
import { useRouter, useRoute } from 'vue-router'
import useMainStore from '@/store/main'

import type { EpFormItem, EpFormRules } from 'global-type'
const { push, replace } = useRouter()
const route = useRoute()
const { formRef, elFormRef } = useForm()
const mainStore = useMainStore()
const rePwdRef = ref(null)
const userModel = reactive<{ password: string; rePassword: string }>({
  password: '',
  rePassword: '',
})

const rules: EpFormRules = {
  password: [
    { required: true, message: '请填写登录密码' },
    { pattern: /[0-9a-zA-Z~!@#￥%&\*]/, message: '密码限制"数字、字母、~!@#￥%&\*"' },
  ],
  rePassword: [
    {
      required: true,
      validator(_, value, cb) {
        if (!value) {
          cb(new Error('请确认登录密码'))
        } else if (value !== userModel.password) {
          cb(new Error('两次输入密码不一致'))
        } else {
          cb()
        }
      },
      trigger: 'blur',
    },
  ],
}
const rePwdKeyDown = (e: any) => {
  if (e.key === 'Enter') {
    onSubmit()
  }
}
const items: EpFormItem[] = [
  {
    label: '新密码',
    prop: 'password',
    slotType: 'input',
    slot: {
      type: 'password',
      onKeydown(event: any) {
        if (event.keyCode == 13) {
          if (userModel.rePassword.length > 0) {
            onSubmit()
          } else {
            ;(rePwdRef.value as any).ref.focus()
          }
        }
      },
    },
  },
  // { label: '确认密码', prop: 'rePassword', slotType: 'input', slot: { type: 'password' } },
  { label: '确认密码', prop: 'rePassword', slotName: 'repassword' },
  { slotName: 'confirm' },
]
const onCancel = () => {
  push({ name: 'Login' })
}
const onSubmit = async () => {
  try {
    const valid = await elFormRef?.value?.validate()
    valid && (await useFetch('updateUserPwd').fetch({ password: userModel.password }))
    ElMessage.success('修改成功')
    let rPath: string = route.query.redirectPath?.toString() || ''
    if (rPath) {
      await mainStore.getMyUserInfo()
      replace({ path: decodeURIComponent(rPath) })
    }
  } catch (error) {
    console.error(error)
  }
}
</script>

<style scoped lang="scss">
.change-pwd-view {
  place-items: center;
  .change-pwd-modal {
    width: 480px;
    height: 416px;
    background: $LoginModalBg;
    border-radius: $LoginModalRadius;
    overflow: hidden;
    .change-pwd-modal-header {
      height: $LoginModalHeaderHeight;
      line-height: $LoginModalHeaderHeight;
      background: $LoginModalHeaderBg;
      font-size: $LoginModalHeaderFontSize;
      font-weight: normal;
      color: $LoginModalHeaderFontColor;
    }
    .change-pwd-modal-content {
      height: 336px;
      padding: 52px 60px;
    }
  }
}
</style>
