liuyu
2 小时以前 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<script lang="ts" setup>
import type { ActionItem, BasicColumn } from '@jnpf/ui/vxeTable';
 
import type { CourseItem } from './types';
 
import { useMessage } from '@jnpf/hooks';
import { useModal } from '@jnpf/ui/modal';
import { BasicVxeTable, TableAction, useVxeTable } from '@jnpf/ui/vxeTable';
 
import { deleteCourse, getCourseList, setCourseEnabled } from '#/api/x/tms/course';
 
import Form from './Form.vue';
import {
  CATEGORY_OPTIONS,
  ENABLE_OPTIONS,
  EVAL_MODE_OPTIONS,
  TRAIN_MODE_OPTIONS,
  labelOfCategory,
  labelOfEnabled,
  labelOfEvalMode,
  labelOfTrainMode,
} from './constants';
 
defineOptions({ name: 'TmsCourse' });
 
const { createMessage } = useMessage();
const [registerForm, { openModal: openFormModal }] = useModal();
 
const columns: BasicColumn[] = [
  { title: '课程编号', dataIndex: 'courseNo', width: 140 },
  { title: '课程名称', dataIndex: 'courseName', minWidth: 180 },
  {
    title: '课程分类',
    dataIndex: 'category',
    width: 100,
    customRender: ({ record }) => labelOfCategory((record as CourseItem).category),
  },
  {
    title: '培训方式',
    dataIndex: 'trainMode',
    width: 110,
    customRender: ({ record }) => labelOfTrainMode((record as CourseItem).trainMode),
  },
  {
    title: '考核方式',
    dataIndex: 'evalMode',
    width: 110,
    customRender: ({ record }) => labelOfEvalMode((record as CourseItem).evalMode),
  },
  {
    title: '关联考核试卷',
    dataIndex: 'paperName',
    minWidth: 180,
    customRender: ({ record }) => (record as CourseItem).paperName || '-',
  },
  {
    title: '学时',
    dataIndex: 'hours',
    width: 80,
    align: 'center',
    customRender: ({ record }) => {
      const h = (record as CourseItem).hours;
      return h === null || h === undefined || h === ('' as any) ? '-' : h;
    },
  },
  {
    title: '状态',
    dataIndex: 'enabled',
    width: 90,
    align: 'center',
    slots: { default: 'enabled' },
  },
  { title: '备注', dataIndex: 'remark', minWidth: 140 },
];
 
const [registerTable, { reload }] = useVxeTable({
  api: fetchList,
  columns,
  immediate: true,
  rowKey: 'id',
  useSearchForm: true,
  formConfig: {
    baseColProps: { span: 6 },
    compact: true,
    schemas: [
      {
        field: 'keyword',
        label: '关键词',
        component: 'Input',
        componentProps: { placeholder: '编号/名称/试卷', submitOnPressEnter: true },
      },
      {
        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,
        },
      },
      {
        field: 'enabled',
        label: '状态',
        component: 'Select',
        componentProps: {
          allowClear: true,
          placeholder: '请选择',
          options: ENABLE_OPTIONS,
        },
      },
    ],
  },
  actionColumn: {
    width: 180,
    title: '操作',
    dataIndex: 'action',
    fixed: 'right',
  },
});
 
async function fetchList(params: Record<string, any>) {
  return { data: await getCourseList(params) };
}
 
function handleAdd() {
  openFormModal(true, {});
}
 
function handleEdit(record: CourseItem) {
  openFormModal(true, { id: record.id });
}
 
async function handleToggleEnabled(record: CourseItem) {
  const next = record.enabled === '1' ? '0' : '1';
  const action = next === '1' ? '启用' : '停用';
  try {
    await setCourseEnabled(record.id, next);
    createMessage.success(`已${action}`);
    reload();
  } catch (e: any) {
    createMessage.error(e?.message || `${action}失败`);
  }
}
 
async function handleDelete(record: CourseItem) {
  try {
    await deleteCourse(record.id);
    createMessage.success('删除成功');
    reload();
  } catch (e: any) {
    createMessage.error(e?.message || '删除失败');
  }
}
 
function getTableActions(record: CourseItem): ActionItem[] {
  const enableLabel = record.enabled === '1' ? '停用' : '启用';
  return [
    { label: '编辑', onClick: handleEdit.bind(null, record) },
    {
      label: enableLabel,
      modelConfirm: {
        content: `确定${enableLabel}培训课程「${record.courseName}」吗?`,
        onOk: handleToggleEnabled.bind(null, record),
      },
    },
    {
      label: '删除',
      color: 'error',
      modelConfirm: {
        content: `确定删除培训课程「${record.courseName}」吗?`,
        onOk: handleDelete.bind(null, record),
      },
    },
  ];
}
</script>
 
<template>
  <div class="jnpf-content-wrapper">
    <div class="jnpf-content-wrapper-center">
      <div class="jnpf-content-wrapper-content">
        <BasicVxeTable @register="registerTable">
          <template #tableTitle>
            <a-button type="primary" pre-icon="icon-ym icon-ym-btn-add" @click="handleAdd">新增</a-button>
          </template>
          <template #enabled="{ record }">
            <a-tag :color="record.enabled === '1' ? 'success' : 'default'">
              {{ labelOfEnabled(record.enabled) }}
            </a-tag>
          </template>
          <template #action="{ record }">
            <TableAction :actions="getTableActions(record)" />
          </template>
        </BasicVxeTable>
      </div>
    </div>
    <Form @register="registerForm" @reload="reload" />
  </div>
</template>