import React, { memo, useEffect, useState } from 'react';
import { useNavigate, useParams, useLocation } from 'react-router-dom';
import { Typography, Card, Row, Col, Tag, Button, Space, Descriptions, Divider, Table, Steps, Collapse, Switch, Skeleton, Spin, Modal } from 'antd';
import { ArrowLeftOutlined, EditOutlined, DeleteOutlined, FileTextOutlined, FilePdfOutlined, EyeOutlined } from '@ant-design/icons';
import { useBreadcrumb } from '../../../layout/LayoutBreadcrumb';
import { NotifConfirmDialog, NotifOk, NotifAlert } from '../../../components/Global/ToastNotif';
import { getBrandById, deleteBrand } from '../../../api/master-brand';
import TableList from '../../../components/Global/TableList';
const { Title, Text } = Typography;
const { Step } = Steps;
const { Panel } = Collapse;
const ViewBrandDevice = () => {
const navigate = useNavigate();
const { id } = useParams();
const location = useLocation();
const { setBreadcrumbItems } = useBreadcrumb();
const [brandData, setBrandData] = useState(null);
const [loading, setLoading] = useState(true);
const [currentStep, setCurrentStep] = useState(0);
const [errorCodesTriger, setErrorCodesTriger] = useState(0);
useEffect(() => {
const fetchBrandData = async () => {
const token = localStorage.getItem('token');
if (token) {
const savedPhase = location.state?.phase || localStorage.getItem(`brand_device_${id}_last_phase`);
if (savedPhase) {
setCurrentStep(parseInt(savedPhase));
localStorage.removeItem(`brand_device_${id}_last_phase`);
}
setBreadcrumbItems([
{ title: • Master },
{
title: navigate('/master/brand-device')}>Brand Device
},
{ title: View Brand Device }
]);
try {
setLoading(true);
const response = await getBrandById(id);
if (response && response.statusCode === 200) {
setBrandData(response.data);
setErrorCodesTriger(prev => prev + 1);
} else {
NotifAlert({
icon: 'error',
title: 'Error',
message: response?.message || 'Failed to fetch brand device data',
});
}
} catch (error) {
console.error('Fetch Brand Device Error:', error);
NotifAlert({
icon: 'error',
title: 'Error',
message: error.message || 'Failed to fetch brand device data',
});
} finally {
setLoading(false);
}
} else {
navigate('/signin');
}
};
fetchBrandData();
}, [id, setBreadcrumbItems, navigate, location.state]);
// const handleEdit = () => {
// navigate(`/master/brand-device/edit/${id}`);
// };
// const handleDelete = () => {
// NotifConfirmDialog({
// icon: 'question',
// title: 'Konfirmasi Hapus',
// message: `Brand Device "${brandData?.brand_name}" akan dihapus?`,
// onConfirm: async () => {
// try {
// const response = await deleteBrand(id);
// if (response && response.statusCode === 200) {
// NotifOk({
// icon: 'success',
// title: 'Berhasil',
// message: response.message || 'Brand Device berhasil dihapus.',
// });
// navigate('/master/brand-device');
// } else {
// NotifAlert({
// icon: 'error',
// title: 'Gagal',
// message: response?.message || 'Gagal menghapus Brand Device',
// });
// }
// } catch (error) {
// console.error('Delete Brand Device Error:', error);
// NotifAlert({
// icon: 'error',
// title: 'Error',
// message: error.message || 'Gagal menghapus Brand Device',
// });
// }
// },
// onCancel: () => {},
// });
// };
// Fungsi untuk membuka file viewer di halaman baru
const handleFileView = (fileName, fileType) => {
console.log('handleFileView called with:', { fileName, fileType });
// Save current phase before navigating to file viewer
localStorage.setItem(`brand_device_${id}_last_phase`, currentStep.toString());
// Extract only the filename without folder prefix
let actualFileName = fileName;
if (fileName && fileName.includes('/')) {
const parts = fileName.split('/');
actualFileName = parts[parts.length - 1]; // Get the last part (actual filename)
}
console.log('Processed filename:', { original: fileName, actual: actualFileName });
const encodedFileName = encodeURIComponent(actualFileName);
const fileTypeParam = fileType === 'image' ? 'image' : 'pdf';
const navigationPath = `/master/brand-device/view/${id}/files/${fileTypeParam}/${encodedFileName}`;
console.log('Navigating to:', navigationPath);
navigate(navigationPath);
};
// if (loading) {
// return (
//
//
//
// );
// }
if (!brandData && !loading) {
return Brand Device not found
;
}
// Error code table columns configuration
const errorCodeColumns = [
{
title: 'No',
key: 'no',
width: '5%',
align: 'center',
render: (_, __, index) => index + 1,
},
{
title: 'Error Code',
dataIndex: 'error_code',
key: 'error_code',
width: '15%',
render: (text) => text || '-',
},
{
title: 'Error Code Name',
dataIndex: 'error_code_name',
key: 'error_code_name',
width: '20%',
render: (text) => text || '-',
},
{
title: 'Description',
dataIndex: 'error_code_description',
key: 'error_code_description',
width: '25%',
render: (text) => text || '-',
},
{
title: 'Solutions',
dataIndex: 'solution',
key: 'solution',
width: '20%',
render: (solutions) => (
{solutions && solutions.length > 0 ? (
{solutions.length} solution(s)
{solutions.slice(0, 2).map((sol, index) => (
{sol.type_solution === 'text' ? (
• {sol.solution_name}
) : (
• {sol.solution_name} ({sol.type_solution})
)}
))}
{solutions.length > 2 && (
...and {solutions.length - 2} more
)}
) : (
No solutions
)}
)
},
{
title: 'Status',
dataIndex: 'is_active',
key: 'is_active',
width: '10%',
align: 'center',
render: (_, { is_active }) => (
{is_active ? 'Active' : 'Inactive'}
),
},
{
title: 'Action',
key: 'action',
align: 'center',
width: '5%',
render: (_, record) => (
}
onClick={() => {
// Show detailed view for this error code
Modal.info({
title: 'Error Code Details',
width: 800,
content: (
{record.error_code}
{record.error_code_name}
{record.error_code_description}
{record.is_active ? 'Active' : 'Inactive'}
Solutions ({record.solution?.length || 0})
{record.solution && record.solution.length > 0 ? (
{record.solution.map((solution) => (
{solution.type_solution === 'pdf' ? (
) : (
)}
{solution.solution_name}
{solution.type_solution.toUpperCase()}
{solution.type_solution === 'text' ? (
{solution.text_solution}
) : (
File: {solution.path_document || solution.path_solution}
{solution.path_document && (
)}
)}
))}
) : (
No solutions available
)}
),
});
}}
style={{ color: '#1890ff' }}
/>
),
},
];
// Mock data function for error codes
const getErrorCodesData = async () => {
const errorCodes = brandData?.error_code || [];
return {
data: errorCodes,
paging: {
current_page: 1,
current_limit: 10,
total_limit: errorCodes.length,
total_page: 1,
}
};
};
const renderStepContent = () => {
if (currentStep === 0) {
return (
Status
{(brandData || {}).is_active ? 'Running' : 'Offline'}
Brand Code
{brandData?.brand_code || (loading ? 'Loading...' : '-')}
Brand Name
{brandData?.brand_name || (loading ? 'Loading...' : '-')}
Manufacture
{brandData?.brand_manufacture || (loading ? 'Loading...' : '-')}
Brand Type
{brandData?.brand_type || (loading ? 'Loading...' : '-')}
Model
{brandData?.brand_model || (loading ? 'Loading...' : '-')}
);
}
if (currentStep === 1) {
const errorCodesCount = loading ? 3 : (brandData?.error_code?.length || 0);
return (
Error Codes ({errorCodesCount})
{errorCodesCount > 0 ? (
) : (
!loading &&
No error codes available
)}
);
}
return null;
};
return (
View Brand Device
}
onClick={() => navigate('/master/brand-device')}
>
Kembali
{/* Content area with blur overlay during loading */}
{/* Overlay with blur effect during loading - only on content area */}
{loading && (
)}
{renderStepContent()}
{currentStep > 0 && (
)}
{currentStep < 1 && (
)}
);
};
export default ViewBrandDevice;