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 PopUpFailedSave from '../../../library/PopUpFailedSave'; import Constant from '../../../library/Constant'; 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, dataTable: [], listData: [], data: [], search: "", visiblePerusahaan: true, cols: null, rows: null, dataLoaded: false, popupError: false, alert: false, tipeAlert: '', messageAlert: '', create: false, edit: false, load: false, judul: '' } 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], "company_parent": item[1] === undefined ? null : item[1], "unit_bisnis": item[2], "startDate": item[3], "endDate": 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); if (response.data) { if (response.ok) { if (response.data.status === "success") { let dataRow = response.data.data.map((item, index) => { return [ index + 1, item.company_name, item.company_parent, item.unit_bisnis, item.start_date, item.end_date, item.error ] }) let columns = [ "Data", { name: "Company Name", options: { customBodyRender: (val, tableMeta) => { let check = null if (tableMeta.rowData[6] != null) { check = tableMeta.rowData[6].findIndex((val) => val.field.includes('company_name')) if (check > -1) { this.setState({ buttonError: true }) } } return (
{tableMeta.rowData[6] != null && check > -1 ? -1 ? "red" : 'black' }}>{val} : -1 ? "red" : 'black' }}>{val} }
); } } }, { name: "Parent Company", options: { customBodyRender: (val, tableMeta) => { let check = null if (tableMeta.rowData[6] != null) { check = tableMeta.rowData[6].findIndex((val) => val.field.includes('company_parent')) if (check > -1) { this.setState({ buttonError: true }) } } return (
{tableMeta.rowData[6] != null && check > -1 ? -1 ? "red" : 'black' }}>{val} : -1 ? "red" : 'black' }}>{val} }
); } } }, { name: "Business Unit", options: { customBodyRender: (val, tableMeta) => { let check = null if (tableMeta.rowData[6] != null) { check = tableMeta.rowData[6].findIndex((val) => val.field.includes('unit_bisnis')) if (check > -1) { this.setState({ buttonError: true }) } } return (
{tableMeta.rowData[6] != null && check > -1 ? -1 ? "red" : 'black' }}>{val} : -1 ? "red" : 'black' }}>{val} }
); } } }, { name: "Valid From", options: { customBodyRender: (val, tableMeta) => { let check = null if (tableMeta.rowData[6] != null) { check = tableMeta.rowData[6].findIndex((val) => val.field.includes('start_date')) if (check > -1) { this.setState({ buttonError: true }) } } return (
{tableMeta.rowData[6] != null && check > -1 ? -1 ? "red" : 'black' }}>{val} : -1 ? "red" : 'black' }}>{val} }
); } } }, { name: "Valid To", options: { customBodyRender: (val, tableMeta) => { let check = null if (tableMeta.rowData[6] != null) { check = tableMeta.rowData[6].findIndex((val) => val.field.includes('end_date')) if (check > -1) { this.setState({ buttonError: true }) } } return (
{tableMeta.rowData[6] != null && check > -1 ? -1 ? "red" : 'black' }}>{val} : -1 ? "red" : 'black' }}>{val} }
); } } }, { 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("Token")) { 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, load: true }) } else { this.setState({ load: true }) } } }) } getData() { 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 }) } else { this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => { if (response.data.message.includes("Token")) { 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' }) } }) } openPopUp(rowData, type) { if (type === 'edit') { this.setState({ rowData: rowData, visibleEdit: 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("Token")) { 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("Token")) { 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("Token")) { 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( "https://tia.eksad.com/tia-reporting-dev/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( "https://tia.eksad.com/tia-reporting-dev/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("Token")) { 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: { customBodyRender: (val, tableMeta) => { return (
{this.state.edit && ( )}
); } } }, { 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"], ["", "-", "-", "-", "-", "-"], ] return (
{/* */}
this.closeAlert()}> this.closeAlert()} severity={this.state.tipeAlert}> {this.state.messageAlert} {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} /> :
{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.popupError && ( this.setState({ popupError: false })} /> )} {this.state.visibleUpload && (
Upload File
{ this.fileHandler(dt) this.setState({ uploadStatus: 'idle', percentage: '0' }) }} onUpload={() => { this.state.judul === "MASTER DATA - COMPANY" ? this.checkUpload() : this.setState({ alert: true, messageAlert: "Invalid Template", tipeAlert: 'warning'}) }} />
)}
); } }