Perusahaan.js 19.5 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 9
import MUIDataTable from "mui-datatables";
import { render } from '@testing-library/react';
import { TextField, InputBase } from "@material-ui/core";
faisalhamdi's avatar
faisalhamdi committed
10
import { ExcelRenderer } from 'react-excel-renderer';
faisalhamdi's avatar
faisalhamdi committed
11 12
import CreatePerusahaan from "./CreatePerusahaan";
import VisualPerusahaan from "./VisualPerusahaan";
faisalhamdi's avatar
faisalhamdi committed
13
import api from "../../../api";
14 15 16 17

var ct = require("../../../library/CustomTable");
const getMuiTheme = () => createMuiTheme(ct.customTable());
const options = ct.customOptions();
faisalhamdi's avatar
faisalhamdi committed
18
const options2 = ct.customOptions2();
19 20 21 22 23 24 25

export default class Perusahaan extends Component {
    constructor(props) {
        super(props)
        this.state = {
            visibleCreate: false,
            visibleEdit: false,
faisalhamdi's avatar
faisalhamdi committed
26
            visibleVisual: false,
faisalhamdi's avatar
faisalhamdi committed
27 28 29 30 31 32 33 34
            dataTable: [],
            listData: [],
            data: [],
            search: "",
            visiblePerusahaan: true,
            cols: null,
            rows: null,
            dataLoaded: false
35
        }
faisalhamdi's avatar
faisalhamdi committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
        this.fileHandler = this.fileHandler.bind(this);
    }

    fileHandler = (event) => {
        let fileObj = event
        ExcelRenderer(fileObj, (err, resp) => {
            if (err) {
                console.log(err);
            }
            else {
                let judul = resp.rows[0]
                let isi = resp.rows.slice(1)
                // let body = isi.map((item) => {
                //     return {
                //         item
                //     ]
                // })
                console.log(JSON.stringify(isi));
                this.setState({
                    dataLoaded: true,
                    cols: judul,
                    rows: isi
                });
            }
        });
61 62 63
    }

    componentDidMount() {
faisalhamdi's avatar
faisalhamdi committed
64 65 66 67
        this.getData()
    }

    getData() {
faisalhamdi's avatar
faisalhamdi committed
68
        api.create().getPerusahaan().then((response) => {
faisalhamdi's avatar
faisalhamdi committed
69 70 71 72
            console.log(response)
            if (response.data.status == 'success') {
                let data = response.data.data
                let listData = data.map((item, index) => {
faisalhamdi's avatar
faisalhamdi committed
73
                    return [index, item.company_id, item.company_name, item.parentName, item.businessUnitName, item.total_report, item.status]
faisalhamdi's avatar
faisalhamdi committed
74 75 76 77 78 79
                })
                this.setState({ dataTable: listData, listData: response.data.data })
            } else {
                alert(response.data.message)
            }
        })
80 81
    }

faisalhamdi's avatar
faisalhamdi committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    openPopUp(index, type) {
        if (type === 'edit') {
            this.setState({
                selectIndex: index,
                visibleEdit: true
            })
        } else {
            this.setState({
                data: this.state.listData[index],
                visibleCreate: true
            })
        }
    }

    handleInputChange(e) {
        this.setState({ search: e })
        let body = {
            "keyword": e
        }
        api.create().searchPerusahaan(body).then(response => {
            // console.log(response.data);
            if (response.data.status == 'success') {
                let data = response.data.data
                let listData = data.map((item, index) => {
                    // return [index, item.business_unit_id, item.business_unit_name, item.status]
                })
                this.setState({ dataTable: listData, listData: response.data.data })
            } else {
                alert(response.data.message)
            }
        })
    }

    updatePerusahaan = (payload) => {
        this.setState({ visibleEdit: false })
        api.create().updatePerusahaan(payload).then(response => {
            if (response.data.status == 'success') {
                this.getData()
            } else {
                alert(response.data.message)
            }
        })
    }

    createPerusahaan = (payload) => {
        this.setState({ visibleCreate: false })
        api.create().createPerusahaan(payload).then(response => {
            if (response.data.status == 'success') {
                this.getData()
            } else {
                alert(response.data.message)
            }
        })
    }

faisalhamdi's avatar
faisalhamdi committed
137 138
    downloadFile = async () => {
        let res = await fetch(
faisalhamdi's avatar
faisalhamdi committed
139
            "https://tia.eksad.com/tia-reporting-dev/public/attachment/download_file?fileName=CompanyTemplate&&fileType=xlsx"
faisalhamdi's avatar
faisalhamdi committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153
        )
        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 Perusahaan.xlsx';
            a.click();
        }
    }

    downloadDataTable = async () => {
        let res = await fetch(
faisalhamdi's avatar
faisalhamdi committed
154
            "https://tia.eksad.com/tia-reporting-dev/public/company/export_company"
faisalhamdi's avatar
faisalhamdi committed
155 156 157 158 159 160 161 162 163 164 165 166
        )
        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 = 'Perusahaan.xlsx';
            a.click();
        }
    }

167 168 169 170 171 172 173 174 175 176 177 178 179
    render() {
        const columns = [{
            name: "Action",
            options: {
                customBodyRender: (val, tableMeta) => {
                    return (
                        <div style={{ display: 'flex' }}>
                            <button
                                style={{
                                    backgroundColor: 'transparent',
                                    cursor: 'pointer',
                                    borderColor: 'transparent'
                                }}
faisalhamdi's avatar
faisalhamdi committed
180 181
                                // onClick={() => this.setState({ visibleEdit: true, data: tableMeta.rowData })}
                                onClick={() => this.openPopUp(tableMeta.rowIndex, 'edit')}
182 183 184 185 186 187 188 189 190 191 192 193 194
                            >
                                <img src={Images.editCopy} />
                            </button>
                        </div >
                    );
                }
            }
        }, {
            name: "ID",
            options: {
                customBodyRender: (val, tableMeta) => {
                    return (
                        <div style={{ display: 'flex' }}>
faisalhamdi's avatar
faisalhamdi committed
195
                            <span style={{ color: tableMeta.rowData[6] === "Aktif" ? "#656565" : 'rgba(0, 0, 0, 0.25)' }}>{val}</span>
196 197 198 199 200 201 202 203 204 205
                        </div >
                    );
                }
            }
        }, {
            name: "Nama Perusahaan",
            options: {
                customBodyRender: (val, tableMeta) => {
                    return (
                        <div style={{ display: 'flex' }}>
faisalhamdi's avatar
faisalhamdi committed
206
                            <span style={{ color: tableMeta.rowData[6] === "Aktif" ? "#656565" : 'rgba(0, 0, 0, 0.25)' }}>{val}</span>
207 208 209 210 211
                        </div >
                    );
                }
            }
        }, {
212
            name: "Parent Company",
213 214 215 216
            options: {
                customBodyRender: (val, tableMeta) => {
                    return (
                        <div style={{ display: 'flex' }}>
faisalhamdi's avatar
faisalhamdi committed
217
                            <span style={{ color: tableMeta.rowData[6] === "Aktif" ? "#656565" : 'rgba(0, 0, 0, 0.25)' }}>{val}</span>
218 219 220 221 222 223 224 225 226 227
                        </div >
                    );
                }
            }
        }, {
            name: "Unit Bisnis",
            options: {
                customBodyRender: (val, tableMeta) => {
                    return (
                        <div style={{ display: 'flex' }}>
faisalhamdi's avatar
faisalhamdi committed
228 229 230 231 232 233 234 235 236 237 238 239
                            <span style={{ color: tableMeta.rowData[6] === "Aktif" ? "#656565" : 'rgba(0, 0, 0, 0.25)' }}>{val}</span>
                        </div >
                    );
                }
            }
        }, {
            name: "Jumlah Laporan",
            options: {
                customBodyRender: (val, tableMeta) => {
                    return (
                        <div style={{ display: 'flex' }}>
                            <span style={{ color: tableMeta.rowData[6] === "Aktif" ? "#656565" : 'rgba(0, 0, 0, 0.25)' }}>{val}</span>
240 241 242 243 244 245 246 247 248 249
                        </div >
                    );
                }
            }
        }, {
            name: "Status",
            options: {
                customBodyRender: (val, tableMeta) => {
                    return (
                        <div style={{ display: 'flex' }}>
faisalhamdi's avatar
faisalhamdi committed
250
                            <span style={{ color: tableMeta.rowData[6] === "Aktif" ? "#656565" : 'rgba(0, 0, 0, 0.25)' }}>{val}</span>
251 252 253 254 255 256
                        </div >
                    );
                }
            }
        }]
        const data = [
faisalhamdi's avatar
faisalhamdi committed
257 258 259 260
            ["", "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"],
261 262 263 264 265
            ["", "-", "-", "-", "-","-"],
        ]
        return (
            <div style={{ height: this.props.height }}>
                {/* <Row> */}
Deni Rinaldi's avatar
Deni Rinaldi committed
266
                <div className={"main-color"} style={{ height: 199, width: '100%' }} />
faisalhamdi's avatar
faisalhamdi committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
                {this.state.visiblePerusahaan === true ?
                    <div>
                        <div style={{ display: 'flex', flexDirection: 'row', justifyContent: 'space-between', paddingRight: 25, paddingLeft: 25, marginTop: -150 }}>
                            <label style={{ color: 'white', fontSize: 16, alignSelf: 'center', width: '20%' }}>Master Data - Perusahaan</label>
                            <div style={{ color: 'white', width: '50%', height: 37, display: 'flex', backgroundColor: 'white', borderWidth: 2, alignItems: 'center', borderRadius: 6, paddingLeft: 5, paddingRight: 5, alignSelf: 'center' }}>
                                <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' }}
                                />
                            </div>
                            <div style={{ width: '20%', justifyContent: 'space-around', display: 'flex', flexFlow: 'wrap' }}>
                                <button
                                    style={{
                                        backgroundColor: 'transparent',
                                        cursor: 'pointer',
faisalhamdi's avatar
faisalhamdi committed
286 287 288 289 290 291 292 293 294 295 296 297 298
                                        borderColor: 'transparent',
                                        margin: 5
                                    }}
                                    onClick={() => this.downloadFile()}
                                >
                                    <img src={Images.template} />
                                </button>
                                <button
                                    style={{
                                        backgroundColor: 'transparent',
                                        cursor: 'pointer',
                                        borderColor: 'transparent',
                                        margin: 5
faisalhamdi's avatar
faisalhamdi committed
299 300 301 302 303 304 305 306 307
                                    }}
                                    onClick={() => this.setState({ visibleUpload: true })}
                                >
                                    <img src={Images.upload} />
                                </button>
                                <button
                                    style={{
                                        backgroundColor: 'transparent',
                                        cursor: 'pointer',
faisalhamdi's avatar
faisalhamdi committed
308 309 310 311 312 313 314 315 316 317 318 319 320
                                        borderColor: 'transparent',
                                        margin: 5
                                    }}
                                    onClick={() => this.downloadDataTable()}
                                >
                                    <img src={Images.download} />
                                </button>
                                <button
                                    style={{
                                        backgroundColor: 'transparent',
                                        cursor: 'pointer',
                                        borderColor: 'transparent',
                                        margin: 5
faisalhamdi's avatar
faisalhamdi committed
321 322 323
                                    }}
                                    onClick={() => null}
                                >
faisalhamdi's avatar
faisalhamdi committed
324
                                    <img src={Images.visualisasi} onClick={() => this.setState({ visibleVisual: true, visiblePerusahaan: false })} />
faisalhamdi's avatar
faisalhamdi committed
325 326 327 328 329
                                </button>
                                <button
                                    style={{
                                        backgroundColor: 'transparent',
                                        cursor: 'pointer',
faisalhamdi's avatar
faisalhamdi committed
330 331
                                        borderColor: 'transparent',
                                        margin: 5
faisalhamdi's avatar
faisalhamdi committed
332 333 334 335 336 337
                                    }}
                                    onClick={() => this.setState({ visibleCreate: true })}
                                >
                                    <img src={Images.add} />
                                </button>
                            </div>
338
                        </div>
faisalhamdi's avatar
faisalhamdi committed
339 340 341 342 343 344 345 346 347 348 349 350 351
                        <div style={{ padding: 25 }}>
                            <MuiThemeProvider theme={getMuiTheme()}>
                                <MUIDataTable
                                    theme={getMuiTheme()}
                                    data={this.state.dataTable}
                                    columns={columns}
                                    options={options}
                                />
                            </MuiThemeProvider>

                        </div>
                    </div>
                    :
faisalhamdi's avatar
faisalhamdi committed
352 353 354 355 356 357
                    this.state.visibleVisual == true ?
                    <VisualPerusahaan
                        onClickClose={() => this.setState({ visibleVisual: false, visiblePerusahaan: true })}
                        height= {this.props.height}
                    />
                    :
faisalhamdi's avatar
faisalhamdi committed
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
                    <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 }}>
375
                            <button
faisalhamdi's avatar
faisalhamdi committed
376 377 378
                                type="button"
                                onClick={() => this.setState({ visiblePerusahaan: true })}
                                style={{ marginRight: 20 }}
379
                            >
faisalhamdi's avatar
faisalhamdi committed
380 381 382
                                <div style={{ width: 102, height: 30, border: 'solid 1px #354960', borderRadius: 5, alignItems: 'center', display: 'flex', justifyContent: 'center' }}>
                                    <span style={{ color: '#354960', fontSize: 11 }}>Batal</span>
                                </div>
383
                            </button>
faisalhamdi's avatar
faisalhamdi committed
384 385 386
                            <div style={{ width: 102, height: 30, backgroundColor: '#354960', borderRadius: 5, alignItems: 'center', display: 'flex', justifyContent: 'center' }}>
                                <span style={{ color: '#fff', fontSize: 11 }}>Simpan</span>
                            </div>
387 388
                        </div>
                    </div>
faisalhamdi's avatar
faisalhamdi committed
389
                }
390

391 392 393
                {this.state.visibleCreate && (
                    <CreatePerusahaan
                        onClickClose={() => this.setState({ visibleCreate: false })}
faisalhamdi's avatar
faisalhamdi committed
394 395
                        type={"create"}
                        createPerusahaan={this.createPerusahaan.bind(this)}
396 397 398 399 400 401 402
                    />
                )}

                {this.state.visibleEdit && (
                    <CreatePerusahaan
                        type={"edit"}
                        onClickClose={() => this.setState({ visibleEdit: false })}
faisalhamdi's avatar
faisalhamdi committed
403 404
                        data={this.state.listData[this.state.selectIndex]}
                        updatePerusahaan={this.updatePerusahaan.bind(this)}
405 406
                    />
                )}
faisalhamdi's avatar
faisalhamdi committed
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440

                {this.state.visibleUpload && (
                    <div className="test app-popup-show">
                        <div className="popup-content background-white border-radius" style={{ borderRadius: 8 }}>
                            <div className="popup-panel grid grid-2x" style={{ backgroundColor: '#51c6ea', height: 64, borderTopRightRadius: 8, borderTopLeftRadius: 8 }}>
                                <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 })}
                                    >
                                        <i className="fa fa-lg fa-times" style={{ color: 'white' }} />
                                    </button>
                                </div>
                            </div>
                            <UploadFile
                                type={this.state.uploadStatus}
                                percentage={this.state.percentage}
                                result={this.state.result}
                                acceptedFiles={["pdf"]}
                                onHandle={(dt) => {
                                    this.fileHandler(dt)
                                    this.setState({ uploadStatus: 'idle', percentage: '0' })
                                }}
                                onUpload={() => this.setState({ visibleUpload: false, visiblePerusahaan: false })}
                            />
                        </div>
                    </div>
                )}
441 442 443
            </div>
        );
    }
faisalhamdi's avatar
faisalhamdi committed
444
}