import TableActionPopconfirm from '@/components/common/table/TableActionPopconfirm';
import {
RefreshCw as ActionRefreshCw,
Plus as ActionPlus,
Upload as ActionUpload,
} from '@/components/common/icons/ActionIcons';
import React, { useState, useEffect, useCallback } from 'react';
import {
Card,
Table,
Button,
Modal,
Form,
Select,
Tag,
Avatar,
Typography,
Space,
Banner,
Spin,
Upload,
} from '@/components/ui';
import { Users } from 'lucide-react';
import { API, showError, showSuccess } from '../../helpers';
import { useTranslation } from 'react-i18next';
const { Text } = Typography;
const CustomerServiceSetting = () => {
const { t } = useTranslation();
const [agents, setAgents] = useState([]);
const [loading, setLoading] = useState(false);
const [modalVisible, setModalVisible] = useState(false);
const [editingAgent, setEditingAgent] = useState(null);
const [formApi, setFormApi] = useState(null);
const [uploadingQrcode, setUploadingQrcode] = useState(false);
const [uploadingAvatar, setUploadingAvatar] = useState(false);
// 上传图片
const handleUpload = async (fileInfo, field) => {
const setUploading =
field === 'qrcode_url' ? setUploadingQrcode : setUploadingAvatar;
setUploading(true);
try {
const formData = new FormData();
// Semi UI 的 customRequest 回调中,fileInfo.fileInstance 是原始 File 对象
const file = fileInfo.fileInstance || fileInfo.file;
formData.append('file', file);
const res = await API.post('/api/customer-service/upload', formData, {
headers: { 'Content-Type': 'multipart/form-data' },
});
if (res.data.success && res.data.data?.url) {
formApi?.setValue(field, res.data.data.url);
showSuccess(t('上传成功'));
fileInfo.onSuccess?.();
} else {
showError(res.data.message || t('上传失败'));
fileInfo.onError?.();
}
} catch (err) {
showError(t('上传失败'));
fileInfo.onError?.();
} finally {
setUploading(false);
}
};
// 工作日选项
const workDayOptions = [
{ label: t('周一'), value: '1' },
{ label: t('周二'), value: '2' },
{ label: t('周三'), value: '3' },
{ label: t('周四'), value: '4' },
{ label: t('周五'), value: '5' },
{ label: t('周六'), value: '6' },
{ label: t('周日'), value: '7' },
];
// 获取客服列表
const fetchAgents = useCallback(async () => {
setLoading(true);
try {
const res = await API.get('/api/customer-service/');
if (res.data.success) {
setAgents(res.data.data || []);
} else {
showError(res.data.message);
}
} catch (err) {
showError(t('获取客服列表失败'));
} finally {
setLoading(false);
}
}, [t]);
useEffect(() => {
fetchAgents();
}, [fetchAgents]);
// 打开新建/编辑弹窗
const openModal = (agent = null) => {
setEditingAgent(agent);
setModalVisible(true);
};
// 关闭弹窗
const closeModal = () => {
setModalVisible(false);
setEditingAgent(null);
if (formApi) {
formApi.reset();
}
};
// 提交表单
const handleSubmit = async () => {
if (!formApi) return;
try {
await formApi.validate();
const values = formApi.getValues();
// 处理工作日
const workDays = Array.isArray(values.work_days)
? values.work_days.join(',')
: values.work_days || '1,2,3,4,5';
// 处理工作时间(Form.Input 返回字符串,直接使用)
const workStartTime = values.work_start_time || '';
const workEndTime = values.work_end_time || '';
const payload = {
...values,
work_days: workDays,
work_start_time: workStartTime,
work_end_time: workEndTime,
weight: values.weight || 100,
max_sessions: values.max_sessions || 50,
priority: values.priority || 0,
status: values.status ?? 1,
};
if (editingAgent) {
payload.id = editingAgent.id;
const res = await API.put('/api/customer-service/', payload);
if (res.data.success) {
showSuccess(t('更新成功'));
closeModal();
fetchAgents();
} else {
showError(res.data.message);
}
} else {
const res = await API.post('/api/customer-service/', payload);
if (res.data.success) {
showSuccess(t('创建成功'));
closeModal();
fetchAgents();
} else {
showError(res.data.message);
}
}
} catch (err) {
// 表单验证失败或其他错误
if (err && err.message) {
showError(err.message);
}
}
};
// 删除客服
const handleDelete = async (id) => {
try {
const res = await API.delete(`/api/customer-service/${id}`);
if (res.data.success) {
showSuccess(t('删除成功'));
fetchAgents();
return true;
} else {
showError(res.data.message);
}
} catch (err) {
showError(t('删除失败'));
}
return false;
};
// 切换状态
const handleToggleStatus = async (agent) => {
const newStatus = agent.status === 1 ? 0 : 1;
try {
const res = await API.put(`/api/customer-service/${agent.id}/status`, {
status: newStatus,
});
if (res.data.success) {
showSuccess(newStatus === 1 ? t('已上线') : t('已下线'));
fetchAgents();
} else {
showError(res.data.message);
}
} catch (err) {
showError(t('操作失败'));
}
};
// 状态标签
const getStatusTag = (status) => {
const statusMap = {
1: { text: t('在线'), color: 'green' },
2: { text: t('忙碌'), color: 'orange' },
0: { text: t('离线'), color: 'grey' },
};
const s = statusMap[status] || statusMap[0];
return