liuyu
4 小时以前 82a74ba0402ab546ba524d23ce62198c4d00d2f7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<script lang="ts" setup>
import { computed, reactive, toRefs, unref } from 'vue';
 
import { useMessage } from '@jnpf/hooks';
import { BasicForm, useForm } from '@jnpf/ui/form';
import { BasicModal, useModalInner } from '@jnpf/ui/modal';
 
import {
  createCourse,
  getCourseInfo,
  getCoursePaperOptions,
  updateCourse,
} from '#/api/x/tms/course';
 
import {
  CATEGORY_OPTIONS,
  ENABLE_OPTIONS,
  EVAL_MODE_OPTIONS,
  TRAIN_MODE_OPTIONS,
  evalModeNeedPaper,
} from './constants';
 
defineOptions({ name: 'TmsCourseForm' });
 
const emit = defineEmits(['register', 'reload']);
const { createMessage } = useMessage();
 
const state = reactive({ id: '' });
const { id } = toRefs(state);
const getTitle = computed(() => (unref(id) ? '编辑培训课程' : '新增培训课程'));
 
const [registerForm, { setFieldsValue, resetFields, validate, updateSchema, clearValidate }] =
  useForm({
    labelWidth: 120,
    schemas: [
      {
        field: 'courseNo',
        label: '课程编号',
        component: 'Input',
        componentProps: {
          placeholder: '保存后自动生成',
          disabled: true,
        },
      },
      {
        field: 'courseName',
        label: '课程名称',
        component: 'Input',
        componentProps: { placeholder: '请输入课程名称', maxlength: 200 },
        rules: [{ required: true, message: '必填', trigger: 'blur' }],
      },
      {
        field: 'category',
        label: '课程分类',
        component: 'Select',
        componentProps: {
          allowClear: true,
          placeholder: '请选择',
          options: CATEGORY_OPTIONS,
        },
      },
      {
        field: 'trainMode',
        label: '培训方式',
        component: 'Select',
        componentProps: {
          allowClear: true,
          placeholder: '请选择',
          options: TRAIN_MODE_OPTIONS,
        },
      },
      {
        field: 'evalMode',
        label: '考核方式',
        component: 'Select',
        componentProps: {
          allowClear: true,
          placeholder: '请选择',
          options: EVAL_MODE_OPTIONS,
          onChange: (val: string) => syncPaperRequired(val),
        },
      },
      {
        field: 'paperId',
        label: '关联考核试卷',
        component: 'Select',
        componentProps: {
          allowClear: true,
          placeholder: '请选择试卷',
          options: [],
          showSearch: true,
          optionFilterProp: 'fullName',
        },
      },
      {
        field: 'hours',
        label: '学时',
        component: 'InputNumber',
        componentProps: {
          min: 0,
          max: 9999,
          step: 0.5,
          precision: 1,
          style: { width: '100%' },
          placeholder: '请输入学时',
        },
      },
      {
        field: 'enabled',
        label: '启用状态',
        component: 'Select',
        defaultValue: '1',
        componentProps: { placeholder: '请选择', options: ENABLE_OPTIONS },
        rules: [{ required: true, message: '必填', trigger: 'change' }],
      },
      {
        field: 'remark',
        label: '备注',
        component: 'Textarea',
        componentProps: { placeholder: '请输入备注', rows: 3, maxlength: 500 },
      },
    ],
  });
 
const [registerModal, { closeModal, changeLoading, changeOkLoading }] = useModalInner(init);
 
function syncPaperRequired(evalMode?: string) {
  const need = evalModeNeedPaper(evalMode);
  updateSchema({
    field: 'paperId',
    rules: need ? [{ required: true, message: '在线考试须关联试卷', trigger: 'change' }] : [],
  });
  if (!need) {
    setFieldsValue({ paperId: undefined });
    clearValidate(['paperId']);
  }
}
 
async function loadPaperOptions() {
  const list = await getCoursePaperOptions();
  updateSchema({
    field: 'paperId',
    componentProps: {
      allowClear: true,
      placeholder: '请选择试卷',
      options: list,
      showSearch: true,
      optionFilterProp: 'fullName',
    },
  });
}
 
async function init(data: { id?: string }) {
  changeLoading(true);
  changeOkLoading(false);
  resetFields();
  state.id = data?.id || '';
  try {
    await loadPaperOptions();
    if (state.id) {
      const info = await getCourseInfo(state.id);
      setFieldsValue(info);
      syncPaperRequired(info.evalMode);
    } else {
      setFieldsValue({ courseNo: '', enabled: '1' });
      syncPaperRequired(undefined);
    }
  } finally {
    changeLoading(false);
  }
}
 
async function handleSubmit() {
  const values = await validate();
  if (!values) return;
  changeOkLoading(true);
  try {
    if (state.id) await updateCourse({ ...values, id: state.id });
    else await createCourse(values);
    createMessage.success('保存成功');
    closeModal();
    emit('reload');
  } catch (e: any) {
    createMessage.error(e?.message || '保存失败');
  } finally {
    changeOkLoading(false);
  }
}
</script>
 
<template>
  <BasicModal v-bind="$attrs" :title="getTitle" @register="registerModal" @ok="handleSubmit">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>