<template>
  <el-dialog v-model="dialogData.dialogShow" title="重置密码" width="480px">
    <el-form :model="editPassWordData" :rules="editPassWordDataRules" ref="editPassWordForm" label-width="120px">
      <el-form-item :label="dialogData.label+ '：'" prop="name" >
        <el-input :value="dialogData.name" disabled/>
      </el-form-item>
      <el-form-item label="重置方式：" v-if="dialogData.type == '2'">
        <el-radio-group v-model="editPassWordData.useDefault">
          <el-radio :value="0">使用新密码</el-radio>
          <el-radio :value="1">重置为默认密码</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="新密码：" v-if="editPassWordData.useDefault==0" prop="randomPassword">
        <el-input v-model="editPassWordData.randomPassword" type="password" placeholder="请输入密码" />
      </el-form-item>
      <el-form-item label="默认密码：" v-if="dialogData.type == '2' && editPassWordData.useDefault==1">
        <div class="default_pass">
          <el-input value="******" disabled />
          <p class="default_pass">默认密码：{{ dialogData.remark }}</p>
        </div>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button class="button_border_gray cancel" @click="dialogData.dialogShow = false">取 消</el-button>
      <el-button class="button_background" :loading="submitLoading" @click="confirmPassWord(editPassWordForm)">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
export default {
  name: 'ResetPassword',
}
</script>
<script lang="ts" setup>
import { ref, reactive, onMounted, inject } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { resetPassWord } from '@/api/manager'
import { resetSchoolPassWord } from '@/api/school'
interface DialogData {
  dialogShow: boolean,
  type: string
  name: string,
  label: string,
  id: string
  remark: string
}
const dialogData = inject<DialogData>('passwordDialog', {
  dialogShow: false,
  type: '',
  name: '',
  label: '',
  id: '',
  remark: ''
})
interface EditPassWordData {
  useDefault: number
  randomPassword: string
}
const editPassWordData = reactive<EditPassWordData>({
  useDefault: 0,
  randomPassword: '',
})
const submitLoading = ref(false)
const editPassWordForm = ref<FormInstance>()
const editPassWordDataRules = reactive<FormRules<EditPassWordData>>({
  randomPassword: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' },
  ],
})
onMounted(() => {
  
})
const confirmPassWord = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async(valid) => {
    if (valid) {
      submitLoading.value = true
      try {
        let res
        if(dialogData.type == '1') {
          res = await resetPassWord(dialogData.id, { 
            randomPassword: editPassWordData.useDefault == 1 ? '' : editPassWordData.randomPassword 
          })
        }else if(dialogData.type == '2')  {
          res = await resetSchoolPassWord({
            schoolInfoId: dialogData.id, 
            useDefault: editPassWordData.useDefault,
            password: editPassWordData.useDefault == 1 ? '' : editPassWordData.randomPassword 
          })
        }
        if (res.code == 200) {
          ElMessage.success(res.msg)
          if(editPassWordForm.value) {
            editPassWordForm.value.resetFields()
          }
          dialogData.dialogShow = false
        } else {
          ElMessage.error(res.msg)
        }
      }catch {}finally {
        submitLoading.value = false
      }
    }
  })
}
</script>  
<style lang="scss" scoped>
.el-form :deep {
  .el-form-item__label {
    font-size: 16px;
  }
  .el-radio__label {
    font-size: 16px;
  }
  .el-input {
    width: 240px;
  }
}
.default_pass {
  font-size: 12px;
}
</style>