import React, { Component } from 'react';
import { Container, Row, Col } from "react-bootstrap";
import { makeStyles, createMuiTheme, MuiThemeProvider } from '@material-ui/core/styles';
import { Typography } from '@material-ui/core';
import Images from '../../../assets/Images';
import UploadFile from "../../../library/Upload";
import MUIDataTable from "mui-datatables";
import { render } from '@testing-library/react';
import { ExcelRenderer } from 'react-excel-renderer';
import CreatePerusahaan from "./CreatePerusahaan";
import EditPerusahaan from "./EditPerusahaan"
import VisualPerusahaan from "./VisualPerusahaan";
import api from "../../../api";
import ReactTooltip from 'react-tooltip';
import MuiAlert from '@material-ui/lab/Alert';
import { TextField, InputBase, Snackbar, withStyles } from "@material-ui/core";
// import DeletePerusahaan from "./DeletePerusahaan";
import PopUpFailedSave from '../../../library/PopUpFailedSave';
import PopUpDelete from "../../../library/PopUpDelete";
import Constant from '../../../library/Constant';
import { css } from "@emotion/core";
import PropagateLoader from "react-spinners/PropagateLoader"
var ct = require("../../../library/CustomTable");
const getMuiTheme = () => createMuiTheme(ct.customTable());
const options = ct.customOptions();
const options2 = ct.customOptions2();
const Alert = withStyles({
})((props) => );
export default class Perusahaan extends Component {
constructor(props) {
super(props)
this.state = {
visibleCreate: false,
visibleEdit: false,
visibleVisual: false,
visibleDelete: false,
dataTable: [],
listData: [],
data: [],
search: "",
visiblePerusahaan: true,
cols: null,
rows: null,
dataLoaded: false,
popupError: false,
alert: false,
tipeAlert: '',
messageAlert: '',
create: false,
edit: false,
delete: false,
load: false,
judul: '',
rowData: [],
companyName: '',
loading: false
}
this.fileHandler = this.fileHandler.bind(this);
}
fileHandler = (event) => {
let fileObj = event
ExcelRenderer(fileObj, (err, resp) => {
if (err) {
console.log(err);
}
else {
let isi = resp.rows.slice(3)
let payload = []
isi.map((item, index) => {
if (item.length > 0) {
payload.push({
"id": index + 1,
"company_name": item[0] === undefined ? "" : item[0],
"company_parent": item[1] === undefined ? "" : item[1],
"unit_bisnis": item[2] === undefined ? "" : item[2],
"start_date": item[3] === undefined ? "" : item[3],
"end_date": item[4] === undefined ? "" :item[4],
})
}
})
let body = {
company: payload
}
console.log(resp.rows[1])
this.setState({ payload: body, buttonError: false, judul: resp.rows[1][0] })
}
});
}
checkUpload() {
api.create().checkUploadPerusahaan(this.state.payload).then(response => {
console.log(response);
let dataRow = []
if (response.data) {
if (response.ok) {
if (response.data.status === "success") {
dataRow = response.data.data.map((item, index) => {
return [
item.company_name,
item.company_parent,
item.unit_bisnis,
item.start_date,
item.end_date,
item.error
]
})
let columns = [
{
name: "Company Name",
options: {
customBodyRender: (val, tableMeta) => {
let check = null
if (tableMeta.rowData[5] != null) {
check = tableMeta.rowData[5].findIndex((val) => val.field.includes('company_name'))
if (check > -1) {
this.setState({ buttonError: true })
}
}
return (
);
}
}
},
{
name: "Parent Company",
options: {
customBodyRender: (val, tableMeta) => {
let check = null
if (tableMeta.rowData[5] != null) {
check = tableMeta.rowData[5].findIndex((val) => val.field.includes('company_parent'))
if (check > -1) {
this.setState({ buttonError: true })
}
}
return (
);
}
}
},
{
name: "Business Unit",
options: {
customBodyRender: (val, tableMeta) => {
let check = null
if (tableMeta.rowData[5] != null) {
check = tableMeta.rowData[5].findIndex((val) => val.field.includes('unit_bisnis'))
if (check > -1) {
this.setState({ buttonError: true })
}
}
return (
);
}
}
},
{
name: "Valid From",
options: {
customBodyRender: (val, tableMeta) => {
let check = null
if (tableMeta.rowData[5] != null) {
check = tableMeta.rowData[5].findIndex((val) => val.field.includes('start_date'))
if (check > -1) {
this.setState({ buttonError: true })
}
}
return (
);
}
}
},
{
name: "Valid To",
options: {
customBodyRender: (val, tableMeta) => {
let check = null
if (tableMeta.rowData[5] != null) {
check = tableMeta.rowData[5].findIndex((val) => val.field.includes('end_date'))
if (check > -1) {
this.setState({ buttonError: true })
}
}
return (
);
}
}
},
{
name: "",
options: {
display: false
}
}
]
console.log(dataRow);
this.setState({
dataLoaded: true,
cols: columns,
rows: dataRow, visibleUpload: false,
visiblePerusahaan: false
});
} else {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
if (response.data.message.includes("Someone Logged In")) {
setTimeout(() => {
localStorage.removeItem(Constant.TOKEN)
window.location.reload();
}, 1000);
}
})
}
} else {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error' })
}
} else {
this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
}
})
}
componentDidMount() {
this.getData()
this.getPermission()
}
getPermission() {
let payload = {
menu: "company"
}
api.create().getPermission(payload).then(response => {
console.log(response)
if (response.data) {
if (response.data.status === "success") {
this.setState({
create: response.data.data.create,
edit: response.data.data.edit,
delete: response.data.data.delete,
load: true
})
} else {
this.setState({
load: true
})
}
}
})
}
getData() {
this.setState({loading: true})
api.create().getPerusahaan().then((response) => {
// console.log(response)
if (response.data) {
if (response.ok) {
if (response.data.status == 'success') {
let data = response.data.data
let listData = data.sort((a, b) => a.company_id - b.company_id).map((item, index) => {
return [index, item.company_id, item.company_name, item.parent_name, item.business_unit_name, item.status]
})
this.setState({ dataTable: listData, listData: response.data.data }, () => {
setTimeout(() => {
this.setState({loading: false})
}, 2000);
})
} else {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning', loading: false }, () => {
if (response.data.message.includes("Someone Logged In")) {
setTimeout(() => {
localStorage.removeItem(Constant.TOKEN)
window.location.reload();
}, 1000);
}
})
}
} else {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error', loading: false })
}
} else {
this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error', loading: false })
}
})
}
openPopUp(rowData, type) {
if (type === 'edit') {
this.setState({
rowData: rowData,
visibleEdit: true
})
} else if (type === 'delete') {
this.setState({
rowData: rowData,
companyName: String(rowData[2]),
visibleDelete: true
})
} else {
this.setState({
rowData: rowData,
visibleCreate: true
})
}
}
handleInputChange(e) {
this.setState({ search: e })
let body = {
"keyword": e
}
api.create().searchPerusahaan(body).then(response => {
if (response.data) {
if (response.ok) {
if (response.data.status == 'success') {
let data = response.data.data
let listData = data.map((item, index) => {
return [index, item.company_id, item.company_name, item.parent_name, item.business_unit_name, item.status]
})
this.setState({ dataTable: listData, listData: response.data.data })
} else {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
if (response.data.message.includes("Someone Logged In")) {
setTimeout(() => {
localStorage.removeItem(Constant.TOKEN)
window.location.reload();
}, 1000);
}
})
}
} else {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error' })
}
} else {
this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
}
})
}
updatePerusahaan = (payload) => {
this.setState({ visibleEdit: false })
api.create().updatePerusahaan(payload).then(response => {
if (response.data) {
if (response.ok) {
if (response.data.status == 'success') {
this.getData()
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'success' })
} else {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
if (response.data.message.includes("Someone Logged In")) {
setTimeout(() => {
localStorage.removeItem(Constant.TOKEN)
window.location.reload();
}, 1000);
}
})
}
} else {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error' })
}
} else {
this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
}
})
}
createPerusahaan = (payload) => {
this.setState({ visibleCreate: false })
api.create().createPerusahaan(payload).then(response => {
if (response.data) {
if (response.ok) {
if (response.data.status == 'success') {
this.getData()
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'success' })
} else {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
if (response.data.message.includes("Someone Logged In")) {
setTimeout(() => {
localStorage.removeItem(Constant.TOKEN)
window.location.reload();
}, 1000);
}
})
}
} else {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error' })
}
} else {
this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
}
})
}
updateHierarchy = (payload) => {
this.setState({ visibleVisual: false, visiblePerusahaan: true })
api.create().saveVisualisasiPerusahaan(payload).then((response) => {
if (response.data) {
if (response.ok) {
if (response.data.status == 'success') {
this.getData()
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'success' })
} else {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
if (response.data.message.includes("Someone Logged In")) {
setTimeout(() => {
localStorage.removeItem(Constant.TOKEN)
window.location.reload();
}, 1000);
}
})
}
} else {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error' })
}
} else {
this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
}
})
}
downloadFile = async () => {
let res = await fetch(
`${process.env.REACT_APP_URL_MAIN_BE}/public/attachment/download_file?fileName=CompanyTemplate&&fileType=xlsx`
)
res = await res.blob()
console.log(res)
if (res.size > 0) {
let url = window.URL.createObjectURL(res);
let a = document.createElement('a');
a.href = url;
a.download = 'Template Company.xlsx';
a.click();
}
}
downloadDataTable = async () => {
let res = await fetch(
`${process.env.REACT_APP_URL_MAIN_BE}/public/company/export_company`
)
res = await res.blob()
console.log(res)
if (res.size > 0) {
let url = window.URL.createObjectURL(res);
let a = document.createElement('a');
a.href = url;
a.download = 'Company.xlsx';
a.click();
}
}
uploadPerusahaan() {
api.create().uploadPerusahaan(this.state.payload).then(response => {
console.log(response)
if (response.data) {
if (response.ok) {
if (response.data.status == "success") {
this.getData()
this.setState({ visiblePerusahaan: true, alert: true, messageAlert: response.data.message, tipeAlert: 'success' })
} else {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
if (response.data.message.includes("Someone Logged In")) {
setTimeout(() => {
localStorage.removeItem(Constant.TOKEN)
window.location.reload();
}, 1000);
}
})
}
} else {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error' })
}
} else {
this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
}
})
}
deleteCompany = (payload) => {
let id = String(payload[1])
api.create().deletePerusahaan(id).then(response => {
if (response.data) {
if (response.ok) {
if (response.data.status == 'success') {
this.getData()
this.setState({ visibleDelete: false, alert: true, messageAlert: response.data.message, tipeAlert: 'success' })
} else {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
if (response.data.message.includes("Someone Logged In")) {
setTimeout(() => {
localStorage.removeItem(Constant.TOKEN)
window.location.reload();
}, 1000);
}
})
}
} else {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error' })
}
} else {
this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
}
})
}
closeAlert() {
this.setState({ alert: false })
}
render() {
const columns = [{
name: "Action",
options: {
filter: false,
sort: false,
customBodyRender: (val, tableMeta) => {
return (
);
}
}
}, {
name: "ID",
options: {
customBodyRender: (val, tableMeta) => {
return (
{val}
);
}
}
}, {
name: "Company Name",
options: {
customBodyRender: (val, tableMeta) => {
return (
{val}
);
}
}
}, {
name: "Parent Company",
options: {
customBodyRender: (val, tableMeta) => {
return (
{val}
);
}
}
}, {
name: "Business Unit",
options: {
customBodyRender: (val, tableMeta) => {
return (
{val}
);
}
}
}, {
name: "Status",
options: {
customBodyRender: (val, tableMeta) => {
return (
{val}
);
}
}
}]
const data = [
["", "1", "Triputra Agro Persada Group", "Triputra Investindo Arya", "Agrobisnis", "5", "Aktif"],
["", "2", "Gawi Bahandep Sawit Mekar", "Triputra Agro Persada Group", "Agrobisnis", "2", "Aktif"],
["", "3", "Puninar Group", "Triputra Investindo Arya", "Service", "5", "Aktif"],
["", "4", "Puninar Infinite Raya", "Puninar Group", "Service", "5", "Non Aktif"],
["", "-", "-", "-", "-", "-"],
]
const loadingComponent = (
);
return (
{/*
*/}
this.closeAlert()}>
this.closeAlert()} severity={this.state.tipeAlert}>
{this.state.messageAlert}
{this.state.loading && loadingComponent}
{this.state.visiblePerusahaan === true ?
this.state.load && (
{/*
this.handleInputChange(e.target.value)}
inputProps={{ 'aria-label': 'naked' }}
/>
*/}
)
:
this.state.visibleVisual === true ?
this.setState({ visibleVisual: false, visiblePerusahaan: true })}
height={this.props.height}
updateHierarchy={this.updateHierarchy.bind(this)}
handleLoading={() => this.setState({loading: true}, () => console.log('aksdkas'))}
/>
:
{this.state.dataLoaded && (
)}
}
{this.state.visibleCreate && (
this.setState({ visibleCreate: false })}
type={"create"}
createPerusahaan={this.createPerusahaan.bind(this)}
/>
)}
{this.state.visibleEdit && (
this.setState({ visibleEdit: false })}
data={this.state.rowData}
updatePerusahaan={this.updatePerusahaan.bind(this)}
/>
)}
{/* {this.state.visibleEdit && (
this.setState({ visibleEdit: false })}
data={this.state.listData[this.state.selectIndex]}
updatePerusahaan={this.updatePerusahaan.bind(this)}
/>
)} */}
{/* {this.state.visibleDelete && (
this.setState({ visibleDelete: false })}
data={this.state.rowData}
deleteCompany={this.deleteCompany.bind(this)}
/>
)} */}
{this.state.popupError && (
this.setState({ popupError: false })} />
)}
{this.state.visibleUpload && (
{
this.fileHandler(dt)
this.setState({ uploadStatus: 'idle', percentage: '0' })
}}
onUpload={() => {
String(this.state.judul).includes("MASTER") && String(this.state.judul).includes("DATA") && String(this.state.judul).includes("COMPANY") ?
this.checkUpload() :
this.setState({ alert: true, messageAlert: "Invalid Template", tipeAlert: 'warning'})
}}
/>
)}
{this.state.visibleDelete && (
this.setState({ visibleDelete: false })}
onClickDelete={this.deleteCompany.bind(this)}
/>
)}
);
}
}