import React, { useState } from 'react';
import { FormControl, InputLabel, Select, MenuItem, Button, TextField } from '@material-ui/core';
import t from '../common/localization';
import { useSelector } from 'react-redux';
import moment from 'moment';
const ReportFilter = ({ children, handleSubmit }) => {
const devices = useSelector(state => Object.values(state.devices.items));
const [deviceId, setDeviceId] = useState();
const [period, setPeriod] = useState('today');
const [from, setFrom] = useState(moment().subtract(1, 'hour'));
const [to, setTo] = useState(moment());
const handleShow = () => {
let selectedFrom;
let selectedTo;
switch (period) {
case 'today':
selectedFrom = moment().startOf('day');
selectedTo = moment().endOf('day');
break;
case 'yesterday':
selectedFrom = moment().subtract(1, 'day').startOf('day');
selectedTo = moment().subtract(1, 'day').endOf('day');
break;
case 'thisWeek':
selectedFrom = moment().startOf('week');
selectedTo = moment().endOf('week');
break;
case 'previousWeek':
selectedFrom = moment().subtract(1, 'week').startOf('week');
selectedTo = moment().subtract(1, 'week').endOf('week');
break;
case 'thisMonth':
selectedFrom = moment().startOf('month');
selectedTo = moment().endOf('month');
break;
case 'previousMonth':
selectedFrom = moment().subtract(1, 'month').startOf('month');
selectedTo = moment().subtract(1, 'month').endOf('month');
break;
default:
selectedFrom = from;
selectedTo = to;
break;
}
handleSubmit(deviceId, selectedFrom.toISOString(), selectedTo.toISOString());
}
return (
<>
{t('reportDevice')}
{t('reportPeriod')}
{period === 'custom' && (
setFrom(moment(e.target.value, moment.HTML5_FMT.DATETIME_LOCAL))}
fullWidth />
)}
{period === 'custom' && (
setTo(moment(e.target.value, moment.HTML5_FMT.DATETIME_LOCAL))}
fullWidth />
)}
{children}
>
);
}
export default ReportFilter;