import React, { memo, useEffect, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { useBreadcrumb } from '../../../layout/LayoutBreadcrumb';
import { Typography, Table, Card, Select, DatePicker, Button, Row, Col } from 'antd';
import { FileTextOutlined } from '@ant-design/icons';
import { decryptData } from '../../../components/Global/Formatter';
import dayjs from 'dayjs';
const { Text } = Typography;
// New data structure for tag history
const tagHistoryData = [
{
tag: 'TEMP_SENSOR_1',
color: '#FF6B4A',
history: [
{ timestamp: '2025-10-09 08:00', value: 75 },
{ timestamp: '2025-10-09 08:05', value: 76 },
{ timestamp: '2025-10-09 08:10', value: 75 },
],
},
{
tag: 'GAS_LEAK_SENSOR_1',
color: '#4ECDC4',
history: [
{ timestamp: '2025-10-09 08:00', value: 10 },
{ timestamp: '2025-10-09 08:05', value: 150 },
{ timestamp: '2025-10-09 08:10', value: 12 },
],
},
{
tag: 'PRESSURE_SENSOR_1',
color: '#FFE66D',
history: [
{ timestamp: '2025-10-09 08:00', value: 1.2 },
{ timestamp: '2025-10-09 08:05', value: 1.3 },
{ timestamp: '2025-10-09 08:10', value: 1.2 },
],
},
];
const IndexReport = memo(function IndexReport() {
const navigate = useNavigate();
const { setBreadcrumbItems } = useBreadcrumb();
const [plantSubSection, setPlantSubSection] = useState('Semua Plant');
const [startDate, setStartDate] = useState(dayjs('2025-09-30'));
const [endDate, setEndDate] = useState(dayjs('2025-10-09'));
const [periode, setPeriode] = useState('30 Menit');
const [userRole, setUserRole] = useState(null);
const [roleLevel, setRoleLevel] = useState(null);
useEffect(() => {
const token = localStorage.getItem('token');
if (token) {
// Get user data and role
let userData = null;
const sessionData = localStorage.getItem('session');
if (sessionData) {
userData = decryptData(sessionData);
} else {
const userRaw = localStorage.getItem('user');
if (userRaw) {
try {
userData = { user: JSON.parse(userRaw) };
} catch (e) {
console.error('Error parsing user data:', e);
}
}
}
if (userData?.user) {
setUserRole(userData.user.role_name);
setRoleLevel(userData.user.role_level);
}
setBreadcrumbItems([
{
title: (
• History
),
},
{
title: (
Report
),
},
]);
} else {
navigate('/signin');
}
}, []);
const handleReset = () => {
setPlantSubSection('Semua Plant');
setStartDate(dayjs('2025-09-30'));
setEndDate(dayjs('2025-10-09'));
setPeriode('30 Menit');
};
// Check if user has permission to view data (all except guest)
const canViewData = userRole && userRole !== 'guest';
// Convert tag history data to table format
const convertToTableData = () => {
const timestamps = {}; // Use an object to collect data per timestamp
tagHistoryData.forEach((tagData) => {
tagData.history.forEach((point) => {
if (!timestamps[point.timestamp]) {
timestamps[point.timestamp] = {
key: point.timestamp,
'Date and Time': point.timestamp,
};
}
timestamps[point.timestamp][tagData.tag] = point.value;
});
});
// Convert the object to an array
return Object.values(timestamps);
};
const tableData = convertToTableData();
// Create dynamic columns based on tags
const tags = tagHistoryData.map((tagData) => tagData.tag);
const columns = [
{
title: 'Date and Time',
dataIndex: 'Date and Time',
key: 'Date and Time',
fixed: 'left',
width: 180,
render: (text) => {text},
},
...tags.map((tag) => ({
title: tag,
dataIndex: tag,
key: tag,
align: 'center',
width: 150,
render: (value) => {value !== undefined ? value : '-'},
})),
];
return (
{/* Filter Section */}
☰ Filter Data
Plant Sub Section
Tanggal Mulai
Tanggal Akhir
Periode
}
disabled={!canViewData}
>
Tampilkan
{/* Table Section */}
{/* {!canViewData ? (
Anda tidak memiliki akses untuk melihat data report.
Silakan hubungi administrator untuk mendapatkan akses.
) : ( */}