<template>
  <my-modal
    v-model:open="visible"
    title="新增用户"
    :width="400"
    @ok="submitHandle"
  >
    <qm-low-form
      :params="params"
      :fields="fields"
      :label-width="80"
      :rules="rules"
      ref="form"
    >
    </qm-low-form>
  </my-modal>
</template>
<script name="AddUserDialog" lang="ts" setup>
import { ref, reactive } from "vue";
import { ROLES, enum2Options } from "@/constants/enums";
import { addUser } from "@/ap/baseDataConfig";

const form = ref();
const visible = defineModel();
const emit = defineEmits(["success"]);

const params = reactive({
  role: "SCHOOL_ADMIN",
  loginName: "",
  password: "",
});
const fields = ref([
  {
    prop: "role",
    label: "角色",
    colSpan: 24,
    type: "radio",
    attrs: {
      options: enum2Options(ROLES),
    },
  },
  {
    prop: "loginName",
    label: "账号",
    colSpan: 24,
  },
  {
    prop: "password",
    label: "初始密码",
    colSpan: 24,
    type: "password",
  },
]);
const rules = {
  loginName: [{ required: true, message: "请输入账号" }],
  password: [{ required: true, message: "请输入初始密码" }],
};
const submitHandle = () => {
  form.value.formRef.validate().then(() => {
    addUser(params).then((res: any) => {
      window.$message.success("操作成功");
      visible.value = false;
      emit("success");
    });
  });
};
</script>
<style lang="less" scoped></style>
