<template>
  <div>
    <el-dialog v-model="dialogFormVisible" title="修改密码" width="500">
      <el-form ref="formRef">
        <el-form-item label="新密码" :label-width="formLabelWidth">
          <el-input v-model="password" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="submit"> 确认 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import { editPassword as merEditPassword } from "@/api/merchant";
import { editPassword as suEditPassword } from "@/api/supplier";

const dialogFormVisible = ref(false);
const formLabelWidth = "100px";

const password = ref("");
const form = reactive({
  id: "",
  mer_id: "", //商户id
  name: "", //账户名称
});
const submit = () => {
  if (!password.value) return ElMessage.error("请先输入密码");
  if (form.mer_id) // 商户重置密码
    merEditPassword({
      mer_id: form.mer_id,
      password: password.value,
    }).then((res) => {
      emit("onClose");
      close();
    });
  else // 供应链重置密码
    suEditPassword({
      id: form.id,
      password: password.value,
    }).then((res) => {
      emit("onClose");
      close();
    });
};

const emit = defineEmits(["onClose"]);

const open = (e: any) => {
  form.mer_id = e.mer_id;
  form.id = e.id;
  password.value = "";
  dialogFormVisible.value = true;
};
const close = () => {
  dialogFormVisible.value = false;
};

defineExpose({
  open,
  close,
});
</script>
