import React, { useEffect, useState } from 'react';
import { useNavigate, useParams, useLocation } from 'react-router-dom';
import { Typography, Card, Row, Col, Tag, Button, Space, Descriptions, Divider, Steps, Collapse, Switch, Spin, Modal, Empty } from 'antd';
import { ArrowLeftOutlined, FileTextOutlined, FilePdfOutlined, EyeOutlined } from '@ant-design/icons';
import { useBreadcrumb } from '../../../layout/LayoutBreadcrumb';
import { NotifOk, NotifAlert } from '../../../components/Global/ToastNotif';
import { getBrandById } from '../../../api/master-brand';
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 [activeErrorKeys, setActiveErrorKeys] = useState([]);
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);
} 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 handleFileView = (fileName, fileType) => {
localStorage.setItem(`brand_device_${id}_last_phase`, currentStep.toString());
let actualFileName = fileName;
if (fileName && fileName.includes('/')) {
const parts = fileName.split('/');
actualFileName = parts[parts.length - 1];
}
const encodedFileName = encodeURIComponent(actualFileName);
const fileTypeParam = fileType === 'image' ? 'image' : 'pdf';
const navigationPath = `/master/brand-device/view/${id}/files/${fileTypeParam}/${encodedFileName}`;
navigate(navigationPath);
};
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 errorCodes = brandData?.error_code || [];
return (
Error Codes ({errorCodes.length})
{errorCodes.length > 0 ? (
{errorCodes.map((errorCode, index) => (
{errorCode.error_code}
- {errorCode.error_code_name}
{errorCode.is_active ? 'Active' : 'Inactive'}
{errorCode.solution?.length || 0} solution(s)
}
>
Description:
{errorCode.error_code_description || 'No description'}
Solutions:
{errorCode.solution && errorCode.solution.length > 0 ? (
{errorCode.solution.map((solution) => (
{solution.type_solution === 'pdf' ? (
) : solution.type_solution === 'image' ? (
) : (
)}
{solution.solution_name}
{solution.type_solution ? solution.type_solution.toUpperCase() : 'TEXT'}
{solution.type_solution === 'text' ? (
{solution.text_solution}
) : (
File: {solution.path_document || solution.path_solution || 'Document'}
{(solution.path_document || solution.path_solution) && (
)}
)}
))}
) : (
No solutions available
)}
))}
) : (
!loading && (
No error codes available
}
/>
)
)}
);
}
return null;
};
return (
View Brand Device
}
onClick={() => navigate('/master/brand-device')}
>
Kembali
{loading && (
)}
{renderStepContent()}
{currentStep > 0 && (
)}
{currentStep < 1 && (
)}
);
};
export default ViewBrandDevice;