<template>
  <div>
    <el-dialog title="修改密码" v-model="dialogVisible">
      <div>
        <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          size="large"
          label-width="120px"
        >
          <el-form-item
            label="昵称"
            :rules="[
              {
                required: true,
                message: '请输入昵称',
                trigger: 'blur'
              }
            ]"
            prop="nickname"
          >
            <el-input
              clearable
              v-model="ruleForm.nickname"
              placeholder="请输入昵称"
              maxlength="10"
              show-word-limit
            />
          </el-form-item>

          <el-form-item
            label="原密码"
            prop="oldPassword"
            :rules="[
              {
                required: true,
                message: '请输入原密码',
                trigger: 'blur'
              }
            ]"
          >
            <el-input
              clearable
              show-password
              v-model="ruleForm.oldPassword"
              placeholder="请输入新密码"
            />
          </el-form-item>

          <el-form-item
            prop="password"
            label="新密码"
            :rules="[
              {
                required: true,
                message: '请输入新密码',
                trigger: 'blur'
              }
            ]"
          >
            <el-input
              clearable
              show-password
              v-model="ruleForm.password"
              placeholder="密码"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit(ruleFormRef)"
              >确定</el-button
            >
            <el-button @click="dialogVisible = false">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div></div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { ref, computed, defineProps, defineEmits, watch } from "vue";
import type { FormInstance } from "element-plus";
import { ElMessage } from "element-plus";

import { request } from "@/utils";
import { useNav } from "@/layout/hooks/useNav";
const { username } = useNav();
const ruleForm = ref({
  nickname: username,
  password: "",
  oldPassword: ""
});

const ruleFormRef = ref<FormInstance>();
const roleList = ref([]);
const props = defineProps({
  show: {
    type: Boolean,
    default: false
  },
  rowData: {
    type: Object,
    default: () => {}
  }
});
const emits = defineEmits(["update:show", "success"]);
const dialogVisible = computed({
  get() {
    return props.show;
  },
  set(n) {
    emits("update:show", n);
  }
});

watch(
  () => dialogVisible.value,
  async n => {
    if (n) {
      await init();
      if (props?.rowData?.id) {
        for (let key in ruleForm.value) {
          ruleForm.value[key] = props.rowData[key];
        }
      }
    } else {
      onReset();
    }
  }
);
const getRoleList = () => {
  return request.get("/mechanism/role/list").then(resp => {
    roleList.value = resp.data.list;
  });
};

const onSubmit = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (!valid) return;
    let params = ruleForm.value;
    request.post("/mechanism/profile/update", params).then(resp => {
      ElMessage.success(resp.message);
      dialogVisible.value = false;
      emits("success");
    });
  });
};
const onReset = () => {
  if (!ruleFormRef.value) return;
  ruleFormRef.value.resetFields();
};
const init = () => {
  return getRoleList();
};
</script>
