<!-- eslint-disable no-unused-vars -->
<script setup>
import { ElMessage } from 'element-plus'
import { ref } from 'vue'
import { useTokenStore } from '@/stores/token.js'
import router from '@/router'
import { useInfoStore } from '@/stores/info.js'
import { modifyPasswordService } from '@/api/common.js'

const resetPassword = ref({
  oldPassword: '',
  newPassword: '',
  reNewPassword: '',
  identity: '',
})
const tokenStore = useTokenStore()
const userInfoStore = useInfoStore()

//修改密码
const reset = async () => {
  try {
    let result = await modifyPasswordService(resetPassword.value)
    if (result.code !== 200) {
      ElMessage({
        message: result.message ? result.message : '更新用户信息失败，请重试',
        grouping: true,
        type: 'error',
      })
    } else {
      ElMessage({
        message: result.message ? result.message : '密码修改成功, 请重新登陆',
        grouping: true,
        type: 'success',
      })
      // 删删除持久化信息
      tokenStore.removeToken()
      userInfoStore.removeInfo()
      router.push('/login')
    }
  } catch (error) {
    ElMessage({
      message: '更新用户信息失败，请重试',
      grouping: true,
      type: 'error',
    })
  }
}

//更新密码规则
const resetPasswordRules = {
  oldPassword: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        const regex = /^.+$/
        if (!value) {
          callback(new Error('请输入密码'))
        } else if (!regex.test(value)) {
          callback(new Error('密码不能为空'))
        } else {
          callback()
        }
      },
      trigger: 'blur',
    },
  ],
  newPassword: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        const regex = /^.+$/
        if (!value) {
          callback(new Error('请输入密码'))
        } else if (!regex.test(value)) {
          callback(new Error('密码不能为空'))
        } else {
          callback()
        }
      },
      trigger: 'blur',
    },
  ],
  reNewPassword: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        const regex = /^.+$/
        if (!value) {
          callback(new Error('请输入密码'))
        } else if (!regex.test(value)) {
          callback(new Error('密码不能为空'))
        } else if (value !== resetPassword.value.newPassword) {
          // 检查reNewPassword与newPassword是否一致
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur',
    },
  ],
  identity: [{ require: true, message: '请输入身份证', trigger: 'blur' }],
}
</script>

<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>修改密码</span>
      </div>
    </template>
    <el-row>
      <el-col :span="12">
        <el-form
          :model="resetPassword"
          :rules="resetPasswordRules"
          label-width="140px"
          size="large"
        >
          <el-form-item label="旧密码" prop="oldPassword">
            <el-input
              v-model="resetPassword.oldPassword"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="newPassword">
            <el-input
              v-model="resetPassword.newPassword"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item label="再次输入新密码" prop="reNewPassword">
            <el-input
              v-model="resetPassword.reNewPassword"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item label="身份证" prop="identity">
            <el-input v-model="resetPassword.identity" show-password></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="reset" class="modify-button"
              >修改密码</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </el-card>
</template>

<style scoped lang="scss">
/* From Uiverse.io by mrhyddenn */
.page-container {
  .modify-button {
    position: relative;
    padding: 10px 20px;
    border-radius: 7px;
    border: 1px solid rgb(61, 106, 255);
    font-size: 14px;
    text-transform: uppercase;
    font-weight: 600;
    letter-spacing: 2px;
    background: transparent;
    color: rgb(40, 40, 96);
    overflow: hidden;
    box-shadow: 0 0 0 0 transparent;
    -webkit-transition: all 0.2s ease-in;
    -moz-transition: all 0.2s ease-in;
    transition: all 0.2s ease-in;
  }

  .modify-button:hover {
    background: rgb(61, 106, 255);
    box-shadow: 0 0 30px 5px rgba(0, 142, 236, 0.815);
    -webkit-transition: all 0.2s ease-out;
    -moz-transition: all 0.2s ease-out;
    transition: all 0.2s ease-out;
  }

  .modify-button:hover::before {
    -webkit-animation: sh02 0.5s 0s linear;
    -moz-animation: sh02 0.5s 0s linear;
    animation: sh02 0.5s 0s linear;
  }

  .modify-button::before {
    content: '';
    display: block;
    width: 0px;
    height: 86%;
    position: absolute;
    top: 7%;
    left: 0%;
    opacity: 0;
    background: #fff;
    box-shadow: 0 0 50px 30px #fff;
    -webkit-transform: skewX(-20deg);
    -moz-transform: skewX(-20deg);
    -ms-transform: skewX(-20deg);
    -o-transform: skewX(-20deg);
    transform: skewX(-20deg);
  }

  @keyframes sh02 {
    from {
      opacity: 0;
      left: 0%;
    }

    50% {
      opacity: 1;
    }

    to {
      opacity: 0;
      left: 100%;
    }
  }

  .modify-button:active {
    box-shadow: 0 0 0 0 transparent;
    -webkit-transition: box-shadow 0.2s ease-in;
    -moz-transition: box-shadow 0.2s ease-in;
    transition: box-shadow 0.2s ease-in;
  }
}
</style>
