<template>
     <Group>
        <el-form :model="formData" label-suffix=":" ref="formRef">
          <el-form-item :label="$t('oldPassword')"  prop="oldPassword" style="width: 360px;" :rules="[
            { required: true, message: $t('enterOldPassword'), trigger: 'blur' }
          ]">
            <el-input v-model="formData.oldPassword" type="password" show-password :placeholder="$t('oldPasswordPlaceholder')"></el-input>
          </el-form-item>
          <el-form-item :label="$t('newPassword')" prop="newPassword" style="width: 360px;" :rules="[
            { required: true, message: $t('enterNewPassword'), trigger: 'blur' },
            { min: 6, max: 10, message: $t('passwordLength'), trigger: 'blur' }
          ]">
            <el-input v-model="formData.newPassword" type="password" show-password :placeholder="$t('newPasswordPlaceholder')"></el-input>
          </el-form-item>
          <el-form-item :label="$t('verifyPassword')" prop="verifyPassword" style="width: 360px;" :rules="[
            { required: true, message: $t('confirmNewPassword'), trigger: 'blur' },
            { validator: validateVerifyPassword, trigger: 'blur' }
          ]">
            <el-input v-model="formData.verifyPassword" type="password" show-password :placeholder="$t('verifyPasswordPlaceholder')"></el-input>
          </el-form-item>
          <el-form-item label="">
            <el-button type="primary" size="samll" v-debounce:click="submit">{{$t('submit')}}</el-button>
          </el-form-item>
        </el-form>
     </Group>
</template>

<script lang='ts' setup>
import {ref ,onMounted, reactive} from 'vue'
import {md5Data} from '@/utils/index';
import request from '@/utils/request'
import { ElMessage } from 'element-plus';
import i18n from '@/utils/i18n'
const formData = reactive({
  oldPassword:'',
  newPassword:'',
  verifyPassword:''
})
const formRef = ref(null)
const validateVerifyPassword =(rule, value, callback) => {
      if (value === '') {
        callback(new Error(i18n.global.t('confirmNewPassword')));
      } else if (value !== formData.newPassword) {
        callback(new Error(i18n.global.t('twoNewPassword')));
      } else {
        callback();
      }
    }
const submit= async ()=>{
  formRef.value.validate(async (valid)=>{
    if(valid){
      const encryptedOldPassword = md5Data(formData.oldPassword)
      const encryptedNewPassword = md5Data(formData.newPassword) 
      const encryptedFormData = {
        oldPassword: encryptedOldPassword,
        password: encryptedNewPassword
      }
  const res = await request('post', '/api/live/video/user/resetPassword', encryptedFormData)
  if(res.code === 200){
    ElMessage.success(i18n.global.t('passwordChangeSuccess'))
  }else{
    ElMessage.error(i18n.global.t('passwordChangeFail'))
  }
  }
})
}
</script>

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