Perusahaan.js 47.9 KB
Newer Older
1 2 3 4 5
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';
faisalhamdi's avatar
faisalhamdi committed
6
import UploadFile from "../../../library/Upload";
7 8
import MUIDataTable from "mui-datatables";
import { render } from '@testing-library/react';
faisalhamdi's avatar
faisalhamdi committed
9
import { ExcelRenderer } from 'react-excel-renderer';
faisalhamdi's avatar
faisalhamdi committed
10
import CreatePerusahaan from "./CreatePerusahaan";
11
import EditPerusahaan from "./EditPerusahaan"
faisalhamdi's avatar
faisalhamdi committed
12
import VisualPerusahaan from "./VisualPerusahaan";
faisalhamdi's avatar
faisalhamdi committed
13
import api from "../../../api";
14
import ReactTooltip from 'react-tooltip';
faisalhamdi's avatar
faisalhamdi committed
15 16
import MuiAlert from '@material-ui/lab/Alert';
import { TextField, InputBase, Snackbar, withStyles } from "@material-ui/core";
faisalhamdi's avatar
faisalhamdi committed
17
// import DeletePerusahaan from "./DeletePerusahaan";
18
import PopUpFailedSave from '../../../library/PopUpFailedSave';
faisalhamdi's avatar
faisalhamdi committed
19
import PopUpDelete from "../../../library/PopUpDelete";
a.bairuha's avatar
a.bairuha committed
20
import Constant from '../../../library/Constant';
d.arizona's avatar
d.arizona committed
21 22
import { css } from "@emotion/core";
import PropagateLoader from "react-spinners/PropagateLoader"
23 24 25 26

var ct = require("../../../library/CustomTable");
const getMuiTheme = () => createMuiTheme(ct.customTable());
const options = ct.customOptions();
faisalhamdi's avatar
faisalhamdi committed
27
const options2 = ct.customOptions2();
28

faisalhamdi's avatar
faisalhamdi committed
29 30 31
const Alert = withStyles({
})((props) => <MuiAlert elevation={6} variant="filled" {...props} />);

32 33 34 35 36 37
export default class Perusahaan extends Component {
    constructor(props) {
        super(props)
        this.state = {
            visibleCreate: false,
            visibleEdit: false,
faisalhamdi's avatar
faisalhamdi committed
38
            visibleVisual: false,
faisalhamdi's avatar
faisalhamdi committed
39
            visibleDelete: false,
faisalhamdi's avatar
faisalhamdi committed
40 41 42 43 44 45 46
            dataTable: [],
            listData: [],
            data: [],
            search: "",
            visiblePerusahaan: true,
            cols: null,
            rows: null,
47
            dataLoaded: false,
faisalhamdi's avatar
faisalhamdi committed
48 49 50
            popupError: false,
            alert: false,
            tipeAlert: '',
Deni Rinaldi's avatar
Deni Rinaldi committed
51 52 53
            messageAlert: '',
            create: false,
            edit: false,
d.arizona's avatar
d.arizona committed
54
            delete: false,
faisalhamdi's avatar
faisalhamdi committed
55
            load: false,
faisalhamdi's avatar
faisalhamdi committed
56 57
            judul: '',
            rowData: [],
d.arizona's avatar
d.arizona committed
58 59
            companyName: '',
            loading: false
faisalhamdi's avatar
faisalhamdi committed
60

61
        }
faisalhamdi's avatar
faisalhamdi committed
62 63 64 65 66 67 68 69 70 71
        this.fileHandler = this.fileHandler.bind(this);
    }

    fileHandler = (event) => {
        let fileObj = event
        ExcelRenderer(fileObj, (err, resp) => {
            if (err) {
                console.log(err);
            }
            else {
72 73 74 75 76 77
                let isi = resp.rows.slice(3)
                let payload = []
                isi.map((item, index) => {
                    if (item.length > 0) {
                        payload.push({
                            "id": index + 1,
faisalhamdi's avatar
faisalhamdi committed
78 79 80
                            "company_name": item[0] === undefined ? "" : item[0],
                            "company_parent": item[1] === undefined ? "" : item[1],
                            "unit_bisnis": item[2] === undefined ? "" : item[2],
a.bairuha's avatar
a.bairuha committed
81 82
                            "start_date": item[3] === undefined ? "" : item[3],
                            "end_date": item[4] === undefined ? "" :item[4],
83 84 85 86 87 88
                        })
                    }
                })
                let body = {
                    company: payload
                }
faisalhamdi's avatar
faisalhamdi committed
89 90
                console.log(resp.rows[1])
                this.setState({ payload: body, buttonError: false, judul: resp.rows[1][0] })
faisalhamdi's avatar
faisalhamdi committed
91 92 93 94 95 96 97
            }
        });
    }

    checkUpload() {
        api.create().checkUploadPerusahaan(this.state.payload).then(response => {
            console.log(response);
faisalhamdi's avatar
faisalhamdi committed
98
            let dataRow = []
faisalhamdi's avatar
faisalhamdi committed
99 100 101
            if (response.data) {
                if (response.ok) {
                    if (response.data.status === "success") {
faisalhamdi's avatar
faisalhamdi committed
102
                        dataRow = response.data.data.map((item, index) => {
faisalhamdi's avatar
faisalhamdi committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
                            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
faisalhamdi's avatar
faisalhamdi committed
118 119
                                        if (tableMeta.rowData[5] != null) {
                                            check = tableMeta.rowData[5].findIndex((val) => val.field.includes('company_name'))
faisalhamdi's avatar
faisalhamdi committed
120 121
                                            if (check > -1) {
                                                this.setState({ buttonError: true })
122 123
                                            }
                                        }
faisalhamdi's avatar
faisalhamdi committed
124 125
                                        return (
                                            <div style={{ display: 'flex' }}>
faisalhamdi's avatar
faisalhamdi committed
126 127
                                                {tableMeta.rowData[5] != null && check > -1 ?
                                                    <a data-tip={tableMeta.rowData[5][check].message} data-for="company_name">
faisalhamdi's avatar
faisalhamdi committed
128
                                                        <span style={{ color: check != null && check > -1 ? "red" : 'black' }}>{val === "" ? "Empty" : val}</span>
faisalhamdi's avatar
faisalhamdi committed
129
                                                    </a> :
faisalhamdi's avatar
faisalhamdi committed
130
                                                    <span style={{ color: check != null && check > -1 ? "red" : 'black' }}>{val === "" ? "Empty" : val}</span>
131
                                                }
faisalhamdi's avatar
faisalhamdi committed
132 133 134 135 136 137 138 139 140 141 142
                                                <ReactTooltip border={true} id="company_name" place="bottom" type="light" effect="solid" />
                                            </div >
                                        );
                                    }
                                }
                            },
                            {
                                name: "Parent Company",
                                options: {
                                    customBodyRender: (val, tableMeta) => {
                                        let check = null
faisalhamdi's avatar
faisalhamdi committed
143 144
                                        if (tableMeta.rowData[5] != null) {
                                            check = tableMeta.rowData[5].findIndex((val) => val.field.includes('company_parent'))
faisalhamdi's avatar
faisalhamdi committed
145 146
                                            if (check > -1) {
                                                this.setState({ buttonError: true })
147 148
                                            }
                                        }
faisalhamdi's avatar
faisalhamdi committed
149 150
                                        return (
                                            <div style={{ display: 'flex' }}>
faisalhamdi's avatar
faisalhamdi committed
151 152
                                                {tableMeta.rowData[5] != null && check > -1 ?
                                                    <a data-tip={tableMeta.rowData[5][check].message} data-for="company_parent">
faisalhamdi's avatar
faisalhamdi committed
153
                                                        <span style={{ color: check != null && check > -1 ? "red" : 'black' }}>{val === "" ? "Empty" : val}</span>
faisalhamdi's avatar
faisalhamdi committed
154
                                                    </a> :
faisalhamdi's avatar
faisalhamdi committed
155
                                                    <span style={{ color: check != null && check > -1 ? "red" : 'black' }}>{val === "" ? "Empty" : val}</span>
156
                                                }
faisalhamdi's avatar
faisalhamdi committed
157 158 159 160 161 162 163 164 165 166 167
                                                <ReactTooltip border={true} id="company_parent" place="bottom" type="light" effect="solid" />
                                            </div >
                                        );
                                    }
                                }
                            },
                            {
                                name: "Business Unit",
                                options: {
                                    customBodyRender: (val, tableMeta) => {
                                        let check = null
faisalhamdi's avatar
faisalhamdi committed
168 169
                                        if (tableMeta.rowData[5] != null) {
                                            check = tableMeta.rowData[5].findIndex((val) => val.field.includes('unit_bisnis'))
faisalhamdi's avatar
faisalhamdi committed
170 171
                                            if (check > -1) {
                                                this.setState({ buttonError: true })
172 173
                                            }
                                        }
faisalhamdi's avatar
faisalhamdi committed
174 175
                                        return (
                                            <div style={{ display: 'flex' }}>
faisalhamdi's avatar
faisalhamdi committed
176 177
                                                {tableMeta.rowData[5] != null && check > -1 ?
                                                    <a data-tip={tableMeta.rowData[5][check].message} data-for="unit_bisnis">
faisalhamdi's avatar
faisalhamdi committed
178
                                                        <span style={{ color: check != null && check > -1 ? "red" : 'black' }}>{val === "" ? "Empty" : val}</span>
faisalhamdi's avatar
faisalhamdi committed
179
                                                    </a> :
faisalhamdi's avatar
faisalhamdi committed
180
                                                    <span style={{ color: check != null && check > -1 ? "red" : 'black' }}>{val === "" ? "Empty" : val}</span>
181
                                                }
faisalhamdi's avatar
faisalhamdi committed
182 183 184 185 186 187 188 189 190 191 192
                                                <ReactTooltip border={true} id="unit_bisnis" place="bottom" type="light" effect="solid" />
                                            </div >
                                        );
                                    }
                                }
                            },
                            {
                                name: "Valid From",
                                options: {
                                    customBodyRender: (val, tableMeta) => {
                                        let check = null
faisalhamdi's avatar
faisalhamdi committed
193 194
                                        if (tableMeta.rowData[5] != null) {
                                            check = tableMeta.rowData[5].findIndex((val) => val.field.includes('start_date'))
faisalhamdi's avatar
faisalhamdi committed
195 196
                                            if (check > -1) {
                                                this.setState({ buttonError: true })
197 198
                                            }
                                        }
faisalhamdi's avatar
faisalhamdi committed
199 200
                                        return (
                                            <div style={{ display: 'flex' }}>
faisalhamdi's avatar
faisalhamdi committed
201 202
                                                {tableMeta.rowData[5] != null && check > -1 ?
                                                    <a data-tip={tableMeta.rowData[5][check].message} data-for="startdate">
faisalhamdi's avatar
faisalhamdi committed
203
                                                        <span style={{ color: check != null && check > -1 ? "red" : 'black' }}>{val === "" ? "Empty" : val}</span>
faisalhamdi's avatar
faisalhamdi committed
204
                                                    </a> :
faisalhamdi's avatar
faisalhamdi committed
205
                                                    <span style={{ color: check != null && check > -1 ? "red" : 'black' }}>{val === "" ? "Empty" : val}</span>
206
                                                }
faisalhamdi's avatar
faisalhamdi committed
207 208 209 210 211 212 213 214 215 216 217
                                                <ReactTooltip border={true} id="startdate" place="bottom" type="light" effect="solid" />
                                            </div >
                                        );
                                    }
                                }
                            },
                            {
                                name: "Valid To",
                                options: {
                                    customBodyRender: (val, tableMeta) => {
                                        let check = null
faisalhamdi's avatar
faisalhamdi committed
218 219
                                        if (tableMeta.rowData[5] != null) {
                                            check = tableMeta.rowData[5].findIndex((val) => val.field.includes('end_date'))
faisalhamdi's avatar
faisalhamdi committed
220 221
                                            if (check > -1) {
                                                this.setState({ buttonError: true })
222 223
                                            }
                                        }
faisalhamdi's avatar
faisalhamdi committed
224 225
                                        return (
                                            <div style={{ display: 'flex' }}>
faisalhamdi's avatar
faisalhamdi committed
226 227
                                                {tableMeta.rowData[5] != null && check > -1 ?
                                                    <a data-tip={tableMeta.rowData[5][check].message} data-for="enddate">
faisalhamdi's avatar
faisalhamdi committed
228
                                                        <span style={{ color: check != null && check > -1 ? "red" : 'black' }}>{val === "" ? "Empty" : val}</span>
faisalhamdi's avatar
faisalhamdi committed
229
                                                    </a> :
faisalhamdi's avatar
faisalhamdi committed
230
                                                    <span style={{ color: check != null && check > -1 ? "red" : 'black' }}>{val === "" ? "Empty" : val}</span>
faisalhamdi's avatar
faisalhamdi committed
231 232 233 234
                                                }
                                                <ReactTooltip border={true} id="enddate" place="bottom" type="light" effect="solid" />
                                            </div >
                                        );
a.bairuha's avatar
a.bairuha committed
235
                                    }
faisalhamdi's avatar
faisalhamdi committed
236 237 238 239 240 241 242
                                }
                            },
                            {
                                name: "",
                                options: {
                                    display: false
                                }
243
                            }
faisalhamdi's avatar
faisalhamdi committed
244 245
                        ]

faisalhamdi's avatar
faisalhamdi committed
246
                        console.log(dataRow);
faisalhamdi's avatar
faisalhamdi committed
247 248 249 250 251 252
                        this.setState({
                            dataLoaded: true,
                            cols: columns,
                            rows: dataRow, visibleUpload: false, 
                            visiblePerusahaan: false
                        });
a.bairuha's avatar
a.bairuha committed
253
                    } else {
faisalhamdi's avatar
faisalhamdi committed
254
                        this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
a.bairuha's avatar
a.bairuha committed
255
                            if (response.data.message.includes("Someone Logged In")) {
faisalhamdi's avatar
faisalhamdi committed
256 257 258 259 260 261
                                setTimeout(() => {
                                    localStorage.removeItem(Constant.TOKEN)
                                    window.location.reload();
                                }, 1000);
                            }
                        })
262
                    }
faisalhamdi's avatar
faisalhamdi committed
263 264 265 266 267
                } else {
                    this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error' })
                }
            } else {
                this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
faisalhamdi's avatar
faisalhamdi committed
268
            }
faisalhamdi's avatar
faisalhamdi committed
269
        })
270 271 272
    }

    componentDidMount() {
faisalhamdi's avatar
faisalhamdi committed
273
        this.getData()
274 275 276 277 278 279 280 281 282 283 284 285 286 287
        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,
d.arizona's avatar
d.arizona committed
288
                        delete: response.data.data.delete,
289 290 291 292 293 294 295 296 297
                        load: true
                    })
                } else {
                    this.setState({
                        load: true
                    })
                }
            }
        })
faisalhamdi's avatar
faisalhamdi committed
298 299 300
    }

    getData() {
d.arizona's avatar
d.arizona committed
301
        this.setState({loading: true})
faisalhamdi's avatar
faisalhamdi committed
302
        api.create().getPerusahaan().then((response) => {
faisalhamdi's avatar
faisalhamdi committed
303
            // console.log(response)
faisalhamdi's avatar
faisalhamdi committed
304
            if (response.data) {
a.bairuha's avatar
a.bairuha committed
305 306 307 308 309 310
                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]
                        })
d.arizona's avatar
d.arizona committed
311 312 313 314 315
                        this.setState({ dataTable: listData, listData: response.data.data }, () => {
                            setTimeout(() => {
                              this.setState({loading: false})
                            }, 2000);
                          })
a.bairuha's avatar
a.bairuha committed
316
                    } else {
d.arizona's avatar
d.arizona committed
317
                        this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning', loading: false }, () => {
a.bairuha's avatar
a.bairuha committed
318
                            if (response.data.message.includes("Someone Logged In")) {
a.bairuha's avatar
a.bairuha committed
319 320 321 322 323 324 325
                                setTimeout(() => {
                                    localStorage.removeItem(Constant.TOKEN)
                                    window.location.reload();
                                }, 1000);
                            }
                        })
                    }
faisalhamdi's avatar
faisalhamdi committed
326
                } else {
d.arizona's avatar
d.arizona committed
327
                    this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error', loading: false })
faisalhamdi's avatar
faisalhamdi committed
328
                }
faisalhamdi's avatar
faisalhamdi committed
329
            } else {
d.arizona's avatar
d.arizona committed
330
                this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error', loading: false })
Deni Rinaldi's avatar
Deni Rinaldi committed
331
            }
faisalhamdi's avatar
faisalhamdi committed
332
        })
333 334
    }

faisalhamdi's avatar
faisalhamdi committed
335
    openPopUp(rowData, type) {
faisalhamdi's avatar
faisalhamdi committed
336 337
        if (type === 'edit') {
            this.setState({
faisalhamdi's avatar
faisalhamdi committed
338
                rowData: rowData,
faisalhamdi's avatar
faisalhamdi committed
339 340
                visibleEdit: true
            })
faisalhamdi's avatar
faisalhamdi committed
341 342 343 344 345 346
        } else if (type === 'delete') {
            this.setState({
                rowData: rowData,
                companyName: String(rowData[2]),
                visibleDelete: true
            })
faisalhamdi's avatar
faisalhamdi committed
347 348
        } else {
            this.setState({
faisalhamdi's avatar
faisalhamdi committed
349
                rowData: rowData,
faisalhamdi's avatar
faisalhamdi committed
350 351 352 353 354 355 356 357 358 359 360
                visibleCreate: true
            })
        }
    }

    handleInputChange(e) {
        this.setState({ search: e })
        let body = {
            "keyword": e
        }
        api.create().searchPerusahaan(body).then(response => {
faisalhamdi's avatar
faisalhamdi committed
361
            if (response.data) {
a.bairuha's avatar
a.bairuha committed
362 363 364 365 366 367 368 369 370
                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' }, () => {
a.bairuha's avatar
a.bairuha committed
371
                            if (response.data.message.includes("Someone Logged In")) {
a.bairuha's avatar
a.bairuha committed
372 373 374 375 376 377 378
                                setTimeout(() => {
                                    localStorage.removeItem(Constant.TOKEN)
                                    window.location.reload();
                                }, 1000);
                            }
                        })
                    }
faisalhamdi's avatar
faisalhamdi committed
379
                } else {
a.bairuha's avatar
a.bairuha committed
380
                    this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error' })
faisalhamdi's avatar
faisalhamdi committed
381
                }
faisalhamdi's avatar
faisalhamdi committed
382
            } else {
faisalhamdi's avatar
faisalhamdi committed
383
                this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
faisalhamdi's avatar
faisalhamdi committed
384 385 386 387 388 389 390
            }
        })
    }

    updatePerusahaan = (payload) => {
        this.setState({ visibleEdit: false })
        api.create().updatePerusahaan(payload).then(response => {
faisalhamdi's avatar
faisalhamdi committed
391
            if (response.data) {
a.bairuha's avatar
a.bairuha committed
392 393 394 395 396 397
                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' }, () => {
a.bairuha's avatar
a.bairuha committed
398
                            if (response.data.message.includes("Someone Logged In")) {
a.bairuha's avatar
a.bairuha committed
399 400 401 402 403 404 405
                                setTimeout(() => {
                                    localStorage.removeItem(Constant.TOKEN)
                                    window.location.reload();
                                }, 1000);
                            }
                        })
                    }
faisalhamdi's avatar
faisalhamdi committed
406
                } else {
a.bairuha's avatar
a.bairuha committed
407
                    this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error' })
faisalhamdi's avatar
faisalhamdi committed
408
                }
faisalhamdi's avatar
faisalhamdi committed
409
            } else {
faisalhamdi's avatar
faisalhamdi committed
410
                this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
faisalhamdi's avatar
faisalhamdi committed
411 412 413 414 415 416 417
            }
        })
    }

    createPerusahaan = (payload) => {
        this.setState({ visibleCreate: false })
        api.create().createPerusahaan(payload).then(response => {
faisalhamdi's avatar
faisalhamdi committed
418
            if (response.data) {
a.bairuha's avatar
a.bairuha committed
419 420 421 422 423 424
                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' }, () => {
a.bairuha's avatar
a.bairuha committed
425
                            if (response.data.message.includes("Someone Logged In")) {
a.bairuha's avatar
a.bairuha committed
426 427 428 429 430 431 432
                                setTimeout(() => {
                                    localStorage.removeItem(Constant.TOKEN)
                                    window.location.reload();
                                }, 1000);
                            }
                        })
                    }
faisalhamdi's avatar
faisalhamdi committed
433
                } else {
a.bairuha's avatar
a.bairuha committed
434
                    this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error' })
faisalhamdi's avatar
faisalhamdi committed
435
                }
faisalhamdi's avatar
faisalhamdi committed
436
            } else {
faisalhamdi's avatar
faisalhamdi committed
437
                this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
faisalhamdi's avatar
faisalhamdi committed
438 439 440 441
            }
        })
    }

faisalhamdi's avatar
faisalhamdi committed
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
    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' })
            }
        })
    }

faisalhamdi's avatar
faisalhamdi committed
469 470
    downloadFile = async () => {
        let res = await fetch(
faisalhamdi's avatar
faisalhamdi committed
471
            "https://tia.eksad.com/tia-reporting-dev/public/attachment/download_file?fileName=CompanyTemplate&&fileType=xlsx"
faisalhamdi's avatar
faisalhamdi committed
472 473 474 475 476 477 478
        )
        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;
faisalhamdi's avatar
faisalhamdi committed
479
            a.download = 'Template Company.xlsx';
faisalhamdi's avatar
faisalhamdi committed
480 481 482 483 484 485
            a.click();
        }
    }

    downloadDataTable = async () => {
        let res = await fetch(
faisalhamdi's avatar
faisalhamdi committed
486
            "https://tia.eksad.com/tia-reporting-dev/public/company/export_company"
faisalhamdi's avatar
faisalhamdi committed
487 488 489 490 491 492 493
        )
        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;
faisalhamdi's avatar
faisalhamdi committed
494
            a.download = 'Company.xlsx';
faisalhamdi's avatar
faisalhamdi committed
495 496 497 498
            a.click();
        }
    }

499 500 501
    uploadPerusahaan() {
        api.create().uploadPerusahaan(this.state.payload).then(response => {
            console.log(response)
faisalhamdi's avatar
faisalhamdi committed
502
            if (response.data) {
a.bairuha's avatar
a.bairuha committed
503 504 505 506 507 508
                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' }, () => {
a.bairuha's avatar
a.bairuha committed
509
                            if (response.data.message.includes("Someone Logged In")) {
a.bairuha's avatar
a.bairuha committed
510 511 512 513 514 515 516
                                setTimeout(() => {
                                    localStorage.removeItem(Constant.TOKEN)
                                    window.location.reload();
                                }, 1000);
                            }
                        })
                    }
faisalhamdi's avatar
faisalhamdi committed
517
                } else {
a.bairuha's avatar
a.bairuha committed
518
                    this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error' })
faisalhamdi's avatar
faisalhamdi committed
519 520 521 522
                }
            } else {
                this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
            }
523 524 525
        })
    }

faisalhamdi's avatar
faisalhamdi committed
526 527 528 529 530 531 532 533 534 535
    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' }, () => {
a.bairuha's avatar
a.bairuha committed
536
                            if (response.data.message.includes("Someone Logged In")) {
faisalhamdi's avatar
faisalhamdi committed
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
                                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' })
            }
        })
    }

faisalhamdi's avatar
faisalhamdi committed
553 554 555 556
    closeAlert() {
        this.setState({ alert: false })
    }

557 558 559 560
    render() {
        const columns = [{
            name: "Action",
            options: {
faisalhamdi's avatar
faisalhamdi committed
561 562
                filter: false,
                sort: false,
563 564
                customBodyRender: (val, tableMeta) => {
                    return (
d.arizona's avatar
d.arizona committed
565 566 567 568 569 570 571 572 573 574 575 576 577 578
                        <div style={{ display: 'flex' }}>
                            {this.state.edit && 
                                <span>
                                    <a data-tip={'Edit'} data-for="edit">
                                        <button
                                            style={{
                                                backgroundColor: 'transparent',
                                                cursor: 'pointer',
                                                borderColor: 'transparent'
                                            }}
                                            // onClick={() => this.setState({ visibleEdit: true, data: tableMeta.rowData })}
                                            onClick={() => this.openPopUp(tableMeta.rowData, 'edit')}
                                        >
                                            <div style={{ display: 'flex' }}>
faisalhamdi's avatar
faisalhamdi committed
579
                                                {!tableMeta.rowData[2].includes("PT TRIPUTRA INVESTINDO ARYA") ?
d.arizona's avatar
d.arizona committed
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
                                                <img src={Images.editCopy} /> :
                                                null
                                                }
                                            </div >
                                        </button>
                                    </a>
                                    <ReactTooltip border={true} id="edit" place="bottom" type="light" effect="solid" />
                                </span>
                            }
                            {this.state.delete && 
                                <span>
                                    <a data-tip={'Delete'} data-for="delete">
                                        <button
                                            style={{
                                                backgroundColor: 'transparent',
                                                cursor: 'pointer',
                                                borderColor: 'transparent',
                                                marginLeft: 10
                                            }}
                                            // onClick={() => this.setState({ visibleEdit: true, data: tableMeta.rowData })}
                                            onClick={() => this.openPopUp(tableMeta.rowData, 'delete')}
                                        >                                        
                                            <div style={{ display: 'flex' }}>
faisalhamdi's avatar
faisalhamdi committed
603
                                                {!tableMeta.rowData[2].includes("PT TRIPUTRA INVESTINDO ARYA") ?
d.arizona's avatar
d.arizona committed
604 605 606 607 608 609 610 611 612 613
                                                <img src={Images.delete} /> :
                                                null
                                                }
                                            </div >
                                        </button>
                                    </a>
                                    <ReactTooltip border={true} id="delete" place="bottom" type="light" effect="solid" />
                                </span>
                            }
                        </div>
614 615 616 617 618 619 620 621 622
                    );
                }
            }
        }, {
            name: "ID",
            options: {
                customBodyRender: (val, tableMeta) => {
                    return (
                        <div style={{ display: 'flex' }}>
Deni Rinaldi's avatar
Deni Rinaldi committed
623
                            <span style={{ color: tableMeta.rowData[5] === "Active" ? "#656565" : 'rgba(0, 0, 0, 0.25)' }}>{val}</span>
624 625 626 627 628
                        </div >
                    );
                }
            }
        }, {
faisalhamdi's avatar
faisalhamdi committed
629
            name: "Company Name",
630 631 632 633
            options: {
                customBodyRender: (val, tableMeta) => {
                    return (
                        <div style={{ display: 'flex' }}>
Deni Rinaldi's avatar
Deni Rinaldi committed
634
                            <span style={{ color: tableMeta.rowData[5] === "Active" ? "#656565" : 'rgba(0, 0, 0, 0.25)' }}>{val}</span>
635 636 637 638 639
                        </div >
                    );
                }
            }
        }, {
640
            name: "Parent Company",
641 642 643 644
            options: {
                customBodyRender: (val, tableMeta) => {
                    return (
                        <div style={{ display: 'flex' }}>
Deni Rinaldi's avatar
Deni Rinaldi committed
645
                            <span style={{ color: tableMeta.rowData[5] === "Active" ? "#656565" : 'rgba(0, 0, 0, 0.25)' }}>{val}</span>
faisalhamdi's avatar
faisalhamdi committed
646 647 648 649 650
                        </div >
                    );
                }
            }
        }, {
faisalhamdi's avatar
faisalhamdi committed
651
            name: "Business Unit",
faisalhamdi's avatar
faisalhamdi committed
652 653 654 655
            options: {
                customBodyRender: (val, tableMeta) => {
                    return (
                        <div style={{ display: 'flex' }}>
Deni Rinaldi's avatar
Deni Rinaldi committed
656
                            <span style={{ color: tableMeta.rowData[5] === "Active" ? "#656565" : 'rgba(0, 0, 0, 0.25)' }}>{val}</span>
657 658 659 660 661 662 663 664 665 666
                        </div >
                    );
                }
            }
        }, {
            name: "Status",
            options: {
                customBodyRender: (val, tableMeta) => {
                    return (
                        <div style={{ display: 'flex' }}>
Deni Rinaldi's avatar
Deni Rinaldi committed
667
                            <span style={{ color: tableMeta.rowData[5] === "Active" ? "#656565" : 'rgba(0, 0, 0, 0.25)' }}>{val}</span>
668 669 670 671 672 673
                        </div >
                    );
                }
            }
        }]
        const data = [
faisalhamdi's avatar
faisalhamdi committed
674 675 676 677
            ["", "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"],
678
            ["", "-", "-", "-", "-", "-"],
679
        ]
d.arizona's avatar
d.arizona committed
680 681 682 683 684 685 686 687 688 689
        const loadingComponent = (
            <div style={{position: 'absolute', zIndex: 110, top: 0, left: 0, width: '100%', height: '100%', display: 'flex', justifyContent: 'center', alignItems: 'center', background: 'rgba(255,255,255,0.8)'}}>
              <PropagateLoader
                // css={override}
                size={20}
                color={"#274B80"}
                loading={this.state.loading}
              />
            </div>
          );
690 691 692
        return (
            <div style={{ height: this.props.height }}>
                {/* <Row> */}
Deni Rinaldi's avatar
Deni Rinaldi committed
693
                <div className={"main-color"} style={{ height: 195, width: '100%' }} />
faisalhamdi's avatar
faisalhamdi committed
694 695 696 697 698
                <Snackbar open={this.state.alert} autoHideDuration={6000} onClose={() => this.closeAlert()}>
                    <Alert onClose={() => this.closeAlert()} severity={this.state.tipeAlert}>
                        {this.state.messageAlert}
                    </Alert>
                </Snackbar>
d.arizona's avatar
d.arizona committed
699
                {this.state.loading && loadingComponent}
faisalhamdi's avatar
faisalhamdi committed
700
                {this.state.visiblePerusahaan === true ?
701 702
                    this.state.load && (
                        <div>
faisalhamdi's avatar
faisalhamdi committed
703
                            <div style={{ display: 'flex', flexDirection: 'row', justifyContent: 'space-between', paddingRight: 25, paddingLeft: 25, marginTop: -181 }}>
704
                                <label style={{ color: 'white', fontSize: 16, alignSelf: 'center', width: '20%', }}>Master Data - Company</label>
faisalhamdi's avatar
faisalhamdi committed
705
                                {/* <div style={{ color: 'white', width: '50%', height: 37, display: 'flex', backgroundColor: 'white', borderWidth: 2, alignItems: 'center', borderRadius: 6, paddingLeft: 5, paddingRight: 5, alignSelf: 'center' }}>
706 707 708 709 710 711 712 713
                                    <img src={Images.searchBlack} style={{ marginRight: 10 }} />
                                    <InputBase
                                        style={{ width: '100%' }}
                                        placeholder="Search"
                                        value={this.state.search}
                                        onChange={(e) => this.handleInputChange(e.target.value)}
                                        inputProps={{ 'aria-label': 'naked' }}
                                    />
faisalhamdi's avatar
faisalhamdi committed
714
                                </div> */}
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
                                <div style={{ width: '30%', justifyContent: 'flex-end', display: 'flex', flexFlow: 'wrap' }}>
                                    <a data-tip={'Download Template'} data-for="template">
                                        <button
                                            style={{
                                                backgroundColor: 'transparent',
                                                cursor: 'pointer',
                                                borderColor: 'transparent',
                                                margin: 5
                                            }}
                                            onClick={() => this.downloadFile()}
                                        >
                                            <img src={Images.template} />
                                        </button>
                                    </a>
                                    <ReactTooltip border={true} id="template" place="bottom" type="light" effect="solid" />
Deni Rinaldi's avatar
Deni Rinaldi committed
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
                                    {this.state.create && (
                                        <a data-tip={'Upload'} data-for="upload">
                                            <button
                                                style={{
                                                    backgroundColor: 'transparent',
                                                    cursor: 'pointer',
                                                    borderColor: 'transparent',
                                                    margin: 5
                                                }}
                                                onClick={() => this.setState({ visibleUpload: true })}
                                            >
                                                <img src={Images.upload} />
                                            </button>
                                        </a>
                                    )}
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
                                    <ReactTooltip border={true} id="upload" place="bottom" type="light" effect="solid" />
                                    <a data-tip={'Download'} data-for="download">
                                        <button
                                            style={{
                                                backgroundColor: 'transparent',
                                                cursor: 'pointer',
                                                borderColor: 'transparent',
                                                margin: 5
                                            }}
                                            onClick={() => this.downloadDataTable()}
                                        >
                                            <img src={Images.download} />
                                        </button>
                                    </a>
                                    <ReactTooltip border={true} id="download" place="bottom" type="light" effect="solid" />
                                    <a data-tip={'Visualization'} data-for="visual">
                                        <button
                                            style={{
                                                backgroundColor: 'transparent',
                                                cursor: 'pointer',
                                                borderColor: 'transparent',
                                                margin: 5
                                            }}
faisalhamdi's avatar
faisalhamdi committed
768
                                            onClick={() => this.setState({ visibleVisual: true, visiblePerusahaan: false })}
769
                                        >
faisalhamdi's avatar
faisalhamdi committed
770
                                            <img src={Images.visualisasi} />
771 772 773
                                        </button>
                                    </a>
                                    <ReactTooltip border={true} id="visual" place="bottom" type="light" effect="solid" />
Deni Rinaldi's avatar
Deni Rinaldi committed
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
                                    {this.state.create && (
                                        <a data-tip={'Add New'} data-for="create">
                                            <button
                                                style={{
                                                    backgroundColor: 'transparent',
                                                    cursor: 'pointer',
                                                    borderColor: 'transparent',
                                                    margin: 5,
                                                    marginRight: 20
                                                }}
                                                onClick={() => this.setState({ visibleCreate: true })}
                                            >
                                                <img src={Images.add} />
                                            </button>
                                        </a>
                                    )}
790 791
                                    <ReactTooltip border={true} id="create" place="bottom" type="light" effect="solid" />
                                </div>
faisalhamdi's avatar
faisalhamdi committed
792
                            </div>
793 794 795 796 797 798 799 800 801
                            <div style={{ padding: 25 }}>
                                <MuiThemeProvider theme={getMuiTheme()}>
                                    <MUIDataTable
                                        theme={getMuiTheme()}
                                        data={this.state.dataTable}
                                        columns={columns}
                                        options={options}
                                    />
                                </MuiThemeProvider>
faisalhamdi's avatar
faisalhamdi committed
802

803
                            </div>
faisalhamdi's avatar
faisalhamdi committed
804
                        </div>
805
                    )
faisalhamdi's avatar
faisalhamdi committed
806
                    :
807
                    this.state.visibleVisual === true ?
808
                        <VisualPerusahaan
Deni Rinaldi's avatar
Deni Rinaldi committed
809 810
                            buttonCreate={this.state.create}
                            buttonEdit={this.state.edit}
811 812
                            onClickClose={() => this.setState({ visibleVisual: false, visiblePerusahaan: true })}
                            height={this.props.height}
faisalhamdi's avatar
faisalhamdi committed
813
                            updateHierarchy={this.updateHierarchy.bind(this)}
d.arizona's avatar
d.arizona committed
814
                            handleLoading={() => this.setState({loading: true}, () => console.log('aksdkas'))}
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
                        />
                        :
                        <div>
                            <div style={{ display: 'flex', flexDirection: 'row', justifyContent: 'space-between', paddingRight: 25, paddingLeft: 25, marginTop: -150 }}>
                                <label style={{ color: 'white', fontSize: 16, alignSelf: 'center' }}>Preview Data</label>
                            </div>
                            <div style={{ padding: 25 }}>
                                {this.state.dataLoaded && (
                                    <MuiThemeProvider theme={getMuiTheme()}>
                                        <MUIDataTable
                                            theme={getMuiTheme()}
                                            data={this.state.rows}
                                            columns={this.state.cols}
                                            options={options2}
                                        />
                                    </MuiThemeProvider>
                                )}
                            </div>
                            <div style={{ display: 'flex', width: '100%', placeContent: 'flex-end', padding: 20 }}>
                                <button
                                    type="button"
                                    onClick={() => this.setState({ visiblePerusahaan: true })}
                                    style={{ marginRight: 20 }}
                                >
                                    <div style={{ width: 102, height: 30, border: 'solid 1px #354960', borderRadius: 5, alignItems: 'center', display: 'flex', justifyContent: 'center' }}>
a.bairuha's avatar
a.bairuha committed
840
                                        <span style={{ color: '#354960', fontSize: 11 }}>Cancel</span>
841 842 843 844
                                    </div>
                                </button>
                                <button
                                    type="button"
faisalhamdi's avatar
faisalhamdi committed
845 846 847
                                    disabled={this.state.buttonError == true ? true : false}
                                    onClick={() => this.uploadPerusahaan()}
                                    // onClick={() => this.state.buttonError ? this.setState({ popupError: true }) : this.uploadPerusahaan()}
848 849 850
                                    style={{}}
                                >
                                    <div style={{ width: 102, height: 30, backgroundColor: '#354960', borderRadius: 5, alignItems: 'center', display: 'flex', justifyContent: 'center' }}>
a.bairuha's avatar
a.bairuha committed
851
                                        <span style={{ color: '#fff', fontSize: 11 }}>Save</span>
852 853
                                    </div>
                                </button>
faisalhamdi's avatar
faisalhamdi committed
854
                            </div>
855
                        </div>
faisalhamdi's avatar
faisalhamdi committed
856
                }
857

858 859 860
                {this.state.visibleCreate && (
                    <CreatePerusahaan
                        onClickClose={() => this.setState({ visibleCreate: false })}
faisalhamdi's avatar
faisalhamdi committed
861 862
                        type={"create"}
                        createPerusahaan={this.createPerusahaan.bind(this)}
863 864 865 866
                    />
                )}

                {this.state.visibleEdit && (
faisalhamdi's avatar
faisalhamdi committed
867
                    <CreatePerusahaan
868 869
                        type={"edit"}
                        onClickClose={() => this.setState({ visibleEdit: false })}
faisalhamdi's avatar
faisalhamdi committed
870
                        data={this.state.rowData}
faisalhamdi's avatar
faisalhamdi committed
871
                        updatePerusahaan={this.updatePerusahaan.bind(this)}
872 873
                    />
                )}
faisalhamdi's avatar
faisalhamdi committed
874

faisalhamdi's avatar
faisalhamdi committed
875 876 877 878 879 880 881 882 883
                {/* {this.state.visibleEdit && (
                    <EditPerusahaan
                        type={"edit"}
                        onClickClose={() => this.setState({ visibleEdit: false })}
                        data={this.state.listData[this.state.selectIndex]}
                        updatePerusahaan={this.updatePerusahaan.bind(this)}
                    />
                )} */}

faisalhamdi's avatar
faisalhamdi committed
884 885 886 887 888 889 890 891 892
                {/* {this.state.visibleDelete && (
                    <DeletePerusahaan
                        type={"delete"}
                        onClickClose={() => this.setState({ visibleDelete: false })}
                        data={this.state.rowData}
                        deleteCompany={this.deleteCompany.bind(this)}
                    />
                )} */}

893 894 895 896
                {this.state.popupError && (
                    <PopUpFailedSave onClickClose={() => this.setState({ popupError: false })} />
                )}

faisalhamdi's avatar
faisalhamdi committed
897 898 899
                {this.state.visibleUpload && (
                    <div className="test app-popup-show">
                        <div className="popup-content background-white border-radius" style={{ borderRadius: 8 }}>
faisalhamdi's avatar
faisalhamdi committed
900
                            <div className="popup-panel grid grid-2x main-color" style={{ height: 64, borderTopRightRadius: 8, borderTopLeftRadius: 8 }}>
faisalhamdi's avatar
faisalhamdi committed
901 902 903 904 905 906 907 908 909 910 911
                                <div className="col-1" style={{ maxWidth: "inherit", display: 'flex', alignItems: 'center' }}>
                                    <div className="popup-title">
                                        <span style={{ color: '#fff', fontSize: 16, fontWeight: 'bold' }}>Upload File</span>
                                    </div>
                                </div>
                                <div className="col-2 content-right" style={{ maxWidth: "inherit", alignSelf: 'center' }}>
                                    <button
                                        type="button"
                                        className="btn btn-circle btn-white"
                                        onClick={() => this.setState({ visibleUpload: false })}
                                    >
Deni Rinaldi's avatar
Deni Rinaldi committed
912
                                        <img src={Images.close} />
faisalhamdi's avatar
faisalhamdi committed
913 914 915 916 917 918 919
                                    </button>
                                </div>
                            </div>
                            <UploadFile
                                type={this.state.uploadStatus}
                                percentage={this.state.percentage}
                                result={this.state.result}
faisalhamdi's avatar
faisalhamdi committed
920
                                acceptedFiles={["xlsx"]}
faisalhamdi's avatar
faisalhamdi committed
921 922 923 924
                                onHandle={(dt) => {
                                    this.fileHandler(dt)
                                    this.setState({ uploadStatus: 'idle', percentage: '0' })
                                }}
faisalhamdi's avatar
faisalhamdi committed
925 926
                                onUpload={() => {
                                    this.state.judul === "MASTER DATA - COMPANY" ?
faisalhamdi's avatar
faisalhamdi committed
927
                                    this.checkUpload() :
faisalhamdi's avatar
faisalhamdi committed
928 929
                                    this.setState({ alert: true, messageAlert: "Invalid Template", tipeAlert: 'warning'})
                                }}
faisalhamdi's avatar
faisalhamdi committed
930 931 932 933
                            />
                        </div>
                    </div>
                )}
faisalhamdi's avatar
faisalhamdi committed
934 935 936 937 938 939 940 941 942 943

                {this.state.visibleDelete && (
                    <PopUpDelete
                        rowData={this.state.rowData}
                        name={this.state.companyName}
                        onClickClose={() => this.setState({ visibleDelete: false })}
                        onClickDelete={this.deleteCompany.bind(this)}
                    />
                )}

944 945 946
            </div>
        );
    }
faisalhamdi's avatar
faisalhamdi committed
947
}