<template>
  <my-modal
    v-model:open="visible"
    :title="IS_EDIT ? '编辑' : '新建' + '考试'"
    @ok="handleOk"
  >
    <qm-low-form
      :params="params"
      :fields="fields"
      :label-width="80"
      :rules="rules"
      ref="form"
    >
      <template #mode> CET </template>
    </qm-low-form>
  </my-modal>
</template>
<script name="AddExam" lang="ts" setup>
import { ref, computed } from "vue";
import { setValueFromObj } from "@/utils/tool";
import { addOrEditExam } from "@/ap/baseDataConfig";
import { useUserStore } from "@/store";

const userStore = useUserStore();
const form = ref();
const IS_EDIT = computed(() => !!props.curRow?.id);
const visible = defineModel();
const props = defineProps<{ curRow: any }>();

const rules = {
  name: [{ required: true, message: "请输入考试名称" }],
};
const params = ref({
  name: "",
});
params.value = setValueFromObj(params.value, props.curRow);
const fields = ref([
  { prop: "name", label: "名称", colSpan: 24 },
  { colSpan: 24, cell: "mode", label: "扫描模式" },
]);
const handleOk = () => {
  form.value.formRef.validate().then(() => {
    addOrEditExam({ name: params.value.name, id: props.curRow?.id }).then(
      (res: any) => {
        window.$message.success("操作成功");
        visible.value = false;
        if (props.curRow?.id) {
          const exam = userStore.curExam;
          if (exam) {
            let obj = { ...exam };
            obj.name = params.value.name;
            userStore.setCurExam(obj);
          }
        }
      }
    );
  });
};
</script>
<style lang="less" scoped></style>
