File size: 5,440 Bytes
8ede856
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * 指令操作方法 Composable
 */
import { reactive } from 'vue';
import axios from 'axios';
import type { CommandItem, RenameDialogState, DetailsDialogState, TypeInfo, StatusInfo } from '../types';

export function useCommandActions(
  toast: (message: string, color?: string) => void,
  fetchCommands: () => Promise<void>
) {
  // 重命名对话框状态
  const renameDialog = reactive<RenameDialogState>({
    show: false,
    command: null,
    newName: '',
    aliases: [],
    loading: false
  });

  // 详情对话框状态
  const detailsDialog = reactive<DetailsDialogState>({
    show: false,
    command: null
  });

  /**
   * 切换指令启用/禁用状态
   */
  const toggleCommand = async (
    cmd: CommandItem,
    successMessage: string,
    errorMessage: string
  ) => {
    try {
      const res = await axios.post('/api/commands/toggle', {
        handler_full_name: cmd.handler_full_name,
        enabled: !cmd.enabled
      });
      if (res.data.status === 'ok') {
        toast(successMessage, 'success');
        await fetchCommands();
      } else {
        toast(res.data.message || errorMessage, 'error');
      }
    } catch (err: any) {
      toast(err?.message || errorMessage, 'error');
    }
  };

  /**
   * 打开重命名对话框
   */
  const openRenameDialog = (cmd: CommandItem) => {
    renameDialog.command = cmd;
    renameDialog.newName = cmd.current_fragment || '';
    renameDialog.aliases = [...(cmd.aliases || [])];
    renameDialog.show = true;
  };

  /**
   * 确认重命名
   */
  const confirmRename = async (successMessage: string, errorMessage: string) => {
    if (!renameDialog.command || !renameDialog.newName.trim()) return;

    renameDialog.loading = true;
    try {
      const res = await axios.post('/api/commands/rename', {
        handler_full_name: renameDialog.command.handler_full_name,
        new_name: renameDialog.newName.trim(),
        aliases: renameDialog.aliases.filter(a => a.trim())
      });
      if (res.data.status === 'ok') {
        toast(successMessage, 'success');
        renameDialog.show = false;
        await fetchCommands();
      } else {
        toast(res.data.message || errorMessage, 'error');
      }
    } catch (err: any) {
      toast(err?.message || errorMessage, 'error');
    } finally {
      renameDialog.loading = false;
    }
  };

  /**
   * 打开详情对话框
   */
  const openDetailsDialog = (cmd: CommandItem) => {
    detailsDialog.command = cmd;
    detailsDialog.show = true;
  };

  /**
   * 获取类型显示信息
   */
  const getTypeInfo = (type: string, translations: { group: string; subCommand: string; command: string }): TypeInfo => {
    switch (type) {
      case 'group':
        return { text: translations.group, color: 'info', icon: 'mdi-folder-outline' };
      case 'sub_command':
        return { text: translations.subCommand, color: 'secondary', icon: 'mdi-subdirectory-arrow-right' };
      default:
        return { text: translations.command, color: 'primary', icon: 'mdi-console-line' };
    }
  };

  /**
   * 获取权限颜色
   */
  const getPermissionColor = (permission: string): string => {
    switch (permission) {
      case 'admin': return 'error';
      default: return 'success';
    }
  };

  /**
   * 获取权限标签
   */
  const getPermissionLabel = (permission: string, translations: { admin: string; everyone: string }): string => {
    switch (permission) {
      case 'admin': return translations.admin;
      default: return translations.everyone;
    }
  };

  /**
   * 获取状态显示信息
   */
  const getStatusInfo = (
    cmd: CommandItem,
    translations: { conflict: string; enabled: string; disabled: string }
  ): StatusInfo => {
    if (cmd.has_conflict) {
      return { text: translations.conflict, color: 'warning', variant: 'flat' };
    }
    if (cmd.enabled) {
      return { text: translations.enabled, color: 'success', variant: 'flat' };
    }
    return { text: translations.disabled, color: 'error', variant: 'outlined' };
  };

  /**
   * 获取表格行属性(用于冲突高亮和子指令样式)
   */
  const getRowProps = ({ item }: { item: CommandItem }) => {
    const classes: string[] = [];
    if (item.has_conflict) {
      classes.push('conflict-row');
    }
    if (item.type === 'sub_command') {
      classes.push('sub-command-row');
    }
    if (item.is_group) {
      classes.push('group-row');
    }
    return classes.length > 0 ? { class: classes.join(' ') } : {};
  };

  /**
   * 更新指令权限
   */
  const updatePermission = async (
    cmd: CommandItem,
    permission: 'admin' | 'member',
    successMessage: string,
    errorMessage: string
  ) => {
    try {
      const res = await axios.post('/api/commands/permission', {
        handler_full_name: cmd.handler_full_name,
        permission: permission
      });
      if (res.data.status === 'ok') {
        toast(successMessage, 'success');
        await fetchCommands();
      } else {
        toast(res.data.message || errorMessage, 'error');
      }
    } catch (err: any) {
      toast(err?.message || errorMessage, 'error');
    }
  };

  return {
    // 状态
    renameDialog,
    detailsDialog,

    // 方法
    toggleCommand,
    updatePermission,
    openRenameDialog,
    confirmRename,
    openDetailsDialog,
    getTypeInfo,
    getPermissionColor,
    getPermissionLabel,
    getStatusInfo,
    getRowProps
  };
}