import React, {Component} from 'react';
import api from "../../api";
import {format} from "date-fns";
import Constant from "../../library/Constant";
import {
createMuiTheme,
FormControlLabel,
Input,
MuiThemeProvider,
TableCell,
Typography,
Paper,
TextField,
Snackbar
} from "@material-ui/core";
import MUIDataTable from "mui-datatables";
import {PropagateLoader} from 'react-spinners';
import {Alert} from '@material-ui/lab';
import ReactTooltip from "react-tooltip";
import Autocomplete from '@material-ui/lab/Autocomplete';
import Images from '../../assets/Images';
var ct = require("../../library/CustomTable");
const getMuiTheme = () => createMuiTheme(ct.customTable4());
const options = ct.customOptionsFixedColumnMonitoring();
const style = {
position: "sticky",
left: 0,
zIndex: 101,
background: "white",
};
const style2 = {
position: "sticky",
background: "white",
zIndex: 100,
};
class RepotrCafrm extends Component {
constructor(props) {
super(props)
this.state = {
dataTable: [],
curMonth: 0,
curYear: 0,
data: false
}
console.log(this.state.periodeMB);
console.log(this.state.month);
}
async getMonth() {
this.setState({loading: true})
await api.create().getMonthTransaction().then(response => {
let dateNow = new Date()
dateNow.setMonth(dateNow.getMonth());
let month = format(dateNow, 'MMMM')
if (response.data) {
if (response.data.status === "success") {
let data = response.data.data
let monthData = data.map((item) => {
return {
month_id: item.id,
month_value: String(item.month_name).substr(0, 3)
}
})
let defaultProps = {
options: monthData,
getOptionLabel: (option) => option.month_value,
};
console.log("month ");
console.log(defaultProps);
console.log(monthData[0]);
let index = data.findIndex((val) => val.month_name == month)
this.setState({
listMonth: defaultProps,
month: index == -1 ? monthData[0] : monthData[index],
curMonth: monthData[index]['month_id']
}, async () => {
await this.getPeriode()
})
} else {
this.setState({alert: true, messageAlert: response.data.message, tipeAlert: 'warning'}, () => {
if (response.data.message.includes("Someone Logged In") || response.data.message.includes("Token Expired")) {
setTimeout(() => {
localStorage.removeItem(Constant.TOKEN)
window.location.reload();
}, 1000);
}
})
}
} else {
this.setState({alert: true, messageAlert: response.data.message, tipeAlert: 'error'})
}
})
}
async getPeriode() {
let currentYear = new Date().getFullYear()
let MB = []
for (var i = 2000; i <= currentYear; i++) {
MB.push({name: String(i), value: i})
if (i == currentYear) {
MB.push({name: String(i + 1), value: i + 1})
}
}
let defaultPropsMB = {
options: MB,
getOptionLabel: (option) => option.name,
};
await this.setState({
listPeriodeMB: defaultPropsMB,
periodeMB: MB[MB.length - 2],
curYear: currentYear,
}, async () => {
await this.getStatus()
})
}
async getStatus(){
let listStatusVal = [
{name: "All", value: 2},
{name: "Completed", value: 1},
{name: "Not-Yet", value: 0}
];
let listStatus = {
options: listStatusVal,
getOptionLabel: (option) => option.name,
};
await this.setState({
listStatus,
defaultStatus : listStatusVal[0],
curStatus: listStatusVal[0].value
}, async () => {
await this.getFromCallback()
})
}
async getData(payload) {
console.log("getData +1323")
console.log(payload);
await api.create().getMonitoringCafrm(payload).then((res) => {
console.log(res);
if (res.data) {
if (res.data.status === 'success') {
let data = res.data.data;
this.setState({
dataTable: data,
data: true,
loading: false
})
} else {
this.setState({
alert: true,
messageAlert: res.data.message,
tipeAlert: 'warning',
loading: false
}, () => {
if (res.data.message.includes("Someone Logged In") || res.data.message.includes("Token Expired")) {
setTimeout(() => {
localStorage.removeItem(Constant.TOKEN)
window.location.reload();
}, 1000);
}
})
}
} else {
this.setState({alert: true, messageAlert: res.data.message, tipeAlert: 'error', loading: false})
}
});
}
async getFromCallback() {
if (this.state.curYear >= 2000 && this.state.curMonth > 0) {
let month = 0;
if(this.state.curMonth >= 1 && this.state.curMonth <= 9 && this.state.curMonth.toString().length == 1){
month = '0'+this.state.curMonth;
}else{
month = this.state.curMonth;
}
let payload = {year: this.state.curYear, month: month, status: this.state.curStatus};
await this.getData(payload);
console.log(`month ${this.state.curMonth} year ${this.state.curYear}`)
this.setState({
curMonth: month,
loading: false
})
}
}
async downloadData() {
let url = `${process.env.REACT_APP_URL_MAIN_BE}/public/transaction/monitoring/export_cafrm_report?periode=${this.state.curYear}&months=${this.state.curMonth}&status=${this.state.curStatus}`
console.log(url);
let res = await fetch(
`${process.env.REACT_APP_URL_MAIN_BE}/public/transaction/monitoring/export_cafrm_report?periode=${this.state.curYear}&months=${this.state.curMonth}&status=${this.state.curStatus}`
)
res = await res.blob()
this.setState({ loading: false })
if (res.size > 0) {
let url = window.URL.createObjectURL(res);
let a = document.createElement('a');
a.href = url;
a.download = `Progress Report CARFM ${this.state.curYear}-${this.state.curMonth}-${this.state.defaultStatus.name}.xlsx`;
a.click();
}
}
async componentDidMount() {
await this.getMonth();
// this.setState({
// loading: false
// })
}
closeAlert() {
this.setState({ alert: false })
}
render() {
const columnMR = [
{
name: "company_name",
label: "Company",
options : {
customHeadRender: (columnMeta) => (