<template>
  <my-modal
    v-model:open="visible"
    title="编辑科目名称"
    :width="400"
    @ok="submitHandle"
  >
    <qm-low-form
      :params="params"
      :fields="fields"
      :label-width="110"
      :rules="rules"
      ref="form"
    >
      <template #subjectCode>
        {{ params.subjectCode }}
      </template>
    </qm-low-form>
  </my-modal>
</template>
<script name="EditSubjectDialog" lang="ts" setup>
import { ref, reactive, computed, onMounted } from "vue";
import { editSubjectName } from "@/ap/baseDataConfig";
import { useUserStore } from "@/store";

const props = defineProps<{ curRow: any }>();

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

const params = reactive({
  subjectCode: "",
  subjectName: "",
});
onMounted(() => {
  params.subjectCode = props.curRow?.subjectCode || "";
  params.subjectName = props.curRow?.subjectName || "";
});
const fields = computed(() => {
  return [
    {
      prop: "subjectCode",
      cell: "subjectCode",
      label: "科目代码",
      colSpan: 24,
    },
    {
      prop: "subjectName",
      label: "科目名称",
      colSpan: 24,
    },
  ];
});

const rules = {
  subjectName: [{ required: true, message: "请输入科目名称" }],
};

const submitHandle = () => {
  form.value.formRef.validate().then(() => {
    editSubjectName({ examId: userStore.curExam?.id, ...params }).then(() => {
      emit("success");
      window.$message.success("修改成功");
      visible.value = false;
    });
  });
};
</script>
<style lang="less" scoped></style>
