liuyu
21 小时以前 93c133349a5ccd7a328371fa113dce69d5611f21
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
<script lang="ts" setup>
import type { ActionItem, BasicColumn } from '@jnpf/ui/vxeTable';
 
import type { SignModeItem } from './types';
 
import { onMounted, ref } from 'vue';
 
import { useMessage } from '@jnpf/hooks';
import { useModal } from '@jnpf/ui/modal';
import { BasicVxeTable, TableAction, useVxeTable } from '@jnpf/ui/vxeTable';
 
import { deleteSignMode, getSignModeList, setSignModeEnabled } from '#/api/x/tms/signMode';
import { useBaseStore } from '#/store';
import {
  TMS_DIC,
  TMS_DIC_FIELD_NAMES,
  labelOfDic,
  loadTmsDic,
  type TmsDicOpt,
} from '#/views/x/tms/shared/dic';
 
import Form from './Form.vue';
 
defineOptions({ name: 'TmsSignMode' });
 
const { createMessage } = useMessage();
const baseStore = useBaseStore();
const [registerForm, { openModal: openFormModal }] = useModal();
 
const yesNoOpts = ref<TmsDicOpt[]>([]);
const enableOpts = ref<TmsDicOpt[]>([]);
 
const columns: BasicColumn[] = [
  { title: '方式编码', dataIndex: 'modeCode', width: 140 },
  { title: '方式名称', dataIndex: 'modeName', width: 140 },
  {
    title: '要点阅读秒数',
    dataIndex: 'tipSeconds',
    width: 130,
    align: 'center',
    customRender: ({ record }) => `${(record as SignModeItem).tipSeconds ?? 0} 秒`,
  },
  {
    title: '须匹配名单',
    dataIndex: 'matchRoster',
    width: 120,
    align: 'center',
    customRender: ({ record }) => labelOfDic(yesNoOpts.value, (record as SignModeItem).matchRoster),
  },
  {
    title: '状态',
    dataIndex: 'enabled',
    width: 90,
    align: 'center',
    slots: { default: 'enabled' },
  },
  { title: '备注', dataIndex: 'remark', minWidth: 220 },
];
 
const [registerTable, { reload, getForm }] = useVxeTable({
  api: fetchList,
  columns,
  immediate: false,
  rowKey: 'id',
  useSearchForm: true,
  formConfig: {
    baseColProps: { span: 6 },
    compact: true,
    schemas: [
      {
        field: 'keyword',
        label: '关键词',
        component: 'Input',
        componentProps: { placeholder: '编码/名称', submitOnPressEnter: true },
      },
      {
        field: 'enabled',
        label: '状态',
        component: 'Select',
        componentProps: {
          allowClear: true,
          placeholder: '请选择',
          options: enableOpts.value,
          fieldNames: TMS_DIC_FIELD_NAMES,
        },
      },
    ],
  },
  actionColumn: {
    width: 180,
    title: '操作',
    dataIndex: 'action',
    fixed: 'right',
  },
});
 
onMounted(async () => {
  yesNoOpts.value = await loadTmsDic(baseStore, TMS_DIC.yesNo);
  enableOpts.value = await loadTmsDic(baseStore, TMS_DIC.enableStatus);
  getForm()?.updateSchema?.({
    field: 'enabled',
    componentProps: {
      allowClear: true,
      placeholder: '请选择',
      options: enableOpts.value,
      fieldNames: TMS_DIC_FIELD_NAMES,
    },
  });
  reload();
});
 
async function fetchList(params: Record<string, any>) {
  return { data: await getSignModeList(params) };
}
 
function handleAdd() {
  openFormModal(true, {});
}
 
function handleEdit(record: SignModeItem) {
  openFormModal(true, { id: record.id });
}
 
async function handleToggleEnabled(record: SignModeItem) {
  const next = record.enabled === '1' ? '0' : '1';
  const action = next === '1' ? '启用' : '停用';
  try {
    await setSignModeEnabled(record.id, next);
    createMessage.success(`已${action}`);
    reload();
  } catch (e: any) {
    createMessage.error(e?.message || `${action}失败`);
  }
}
 
async function handleDelete(record: SignModeItem) {
  if (record.enabled !== '0') {
    createMessage.warning('请先停用后再删除');
    return;
  }
  try {
    await deleteSignMode(record.id);
    createMessage.success('删除成功');
    reload();
  } catch (e: any) {
    createMessage.error(e?.message || '删除失败');
  }
}
 
function getTableActions(record: SignModeItem): ActionItem[] {
  const enableLabel = record.enabled === '1' ? '停用' : '启用';
  const actions: ActionItem[] = [
    { label: '编辑', onClick: handleEdit.bind(null, record) },
    {
      label: enableLabel,
      modelConfirm: {
        content: `确定${enableLabel}签到方式「${record.modeName}」吗?`,
        onOk: handleToggleEnabled.bind(null, record),
      },
    },
  ];
  if (record.enabled === '0') {
    actions.push({
      label: '删除',
      color: 'error',
      modelConfirm: {
        content: `确定删除签到方式「${record.modeName}」吗?`,
        onOk: handleDelete.bind(null, record),
      },
    });
  }
  return actions;
}
</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'">
              {{ labelOfDic(enableOpts, record.enabled) }}
            </a-tag>
          </template>
          <template #action="{ record }">
            <TableAction :actions="getTableActions(record)" />
          </template>
        </BasicVxeTable>
      </div>
    </div>
    <Form @register="registerForm" @reload="reload" />
  </div>
</template>