import React, { Component } from "react";
import { Container, Row, Col } from "react-bootstrap";
import { makeStyles, createMuiTheme, MuiThemeProvider } from '@material-ui/core/styles';
import SearchIcon from '@material-ui/icons/Search';
import Images from "../../assets/Images";
import MUIDataTable from "mui-datatables";
import { TextField, InputBase } from "@material-ui/core";
import { ExcelRenderer } from 'react-excel-renderer';
import UploadFile from "../../library/Upload";
import CreateReportItems from "./formReportItems/CreateReportItems";
import EditReportItems from "./formReportItems/EditReportItems";
import VisualReportItems from "./formReportItems/VisualReportItems";

var ct = require("../../library/CustomTable");
const getMuiTheme = () => createMuiTheme(ct.customTable());
const options = ct.customOptions();
const options2 = ct.customOptions2();

export default class ReportItems extends Component {
    constructor(props) {
        super(props)
        this.state = {
            dataTable: [],
            listData: [],
            data: [],
            search: "",
            itemReport: true,
            visualisasi: false,
            add: false,
            edit: false,
            dataLoaded: false,
            cols: null,
            rows: null
        }

        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
                });
            }
        });
    }

    closeEdit() {
        this.setState({edit: false})
    }
    
    closeAdd() {
        this.setState({add: false})
    }
    
    render() {
        const columns = [{
            name: "Action",
            options: {
                customBodyRender: (val, tableMeta) => {
                    return (
                        <div style={{ display: 'flex' }}>
                            <button
                                style={{
                                    backgroundColor: 'transparent',
                                    cursor: 'pointer',
                                    borderColor: 'transparent'
                                }}
                                
                            >
                                <img src={Images.editCopy} />
                            </button>
                        </div >
                    );
                }
            }
        }, "ID", "Jenis Laporan", "Perusahaan", "Order", "Deskripsi", "Parent ID", "UOM", "Weight", "Tipe Data", "Status"]
        const data = [
            ["", "1", "KPI", "TIA", "1", "Financial Perspective", "0", "-", "35%", "Formula", "Aktif"],
            ["", "2", "KPI", "TIA", "2", "Trading Profit", "1", "Rp Bio", "35%", "Formula", "Aktif"],
            ["", "3", "KPI", "TIA", "3", "Revenue", "1", "Rp Bio", "0%", "Formula", "Aktif"],
            ["", "4", "KPI", "TIA", "4", "EBITDA", "1", "Rp Bio", "0%", "Formula", "Non Aktif"],
            ["", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"],
        ]
        const options = {
            filter: false,
            sort: false,
            responsive: "scroll",
            print: false,
            download: false,
            selectableRows: false,
            viewColumns: false,
            rowsPerPage: 5,
            search: false
        }

        return (
            <div>
                {this.state.itemReport === true ?
                    <div style={{ height: this.props.height }}>
                    <div className={"main-color"} style={{ height: 199, width: '100%' }} />
                        <div>
                            <div style={{ display: 'flex', flexDirection: 'row', justifyContent: 'space-between', paddingRight: 25, paddingLeft: 25, marginTop: -150 }}>
                                <label style={{ color: 'white', fontSize: 16, alignSelf: 'center' }}>Master Data - Item Report</label>
                                <div style={{ color: 'white', width: '50%', height: 37, display: 'flex', backgroundColor: 'white', borderWidth: 2, alignItems: 'center', borderRadius: 6, paddingLeft: 5, paddingRight: 5 }}>
                                    <img src={Images.searchBlack} style={{ marginRight: 10 }} />
                                    <InputBase
                                        style={{ width: '100%' }}
                                        placeholder="Search"
                                        value={""}
                                        onChange={(e) => this.handleInputChange(e)}
                                        inputProps={{ 'aria-label': 'naked' }}
                                    />
                                </div>
                                <div style={{ width: '20%', justifyContent: 'space-around', display: 'flex' }}>
                                    <button
                                        style={{
                                            backgroundColor: 'transparent',
                                            cursor: 'pointer',
                                            borderColor: 'transparent',
                                            padding: 0
                                        }}
                                        onClick={() => null}
                                    >
                                        <img src={Images.template} />
                                    </button>
                                    <button
                                        style={{
                                            backgroundColor: 'transparent',
                                            cursor: 'pointer',
                                            borderColor: 'transparent'
                                        }}
                                        onClick={() => null}
                                    >
                                        <img src={Images.upload} onClick={() => this.setState({ visibleUpload: true })} />
                                    </button>
                                    <button
                                        style={{
                                            backgroundColor: 'transparent',
                                            cursor: 'pointer',
                                            borderColor: 'transparent'
                                        }}
                                        onClick={() => null}
                                    >
                                        <img src={Images.download} />
                                    </button>
                                    <button
                                        style={{
                                            backgroundColor: 'transparent',
                                            cursor: 'pointer',
                                            borderColor: 'transparent'
                                        }}
                                        onClick={() => null}
                                    >
                                        <img src={Images.visualisasi} onClick={() => this.setState({ visualisasi: true, itemReport: false })} />
                                    </button>
                                    <button
                                        style={{
                                            backgroundColor: 'transparent',
                                            cursor: 'pointer',
                                            borderColor: 'transparent'
                                        }}
                                    >
                                        <img src={Images.add}  onClick={() => this.setState({ add: true })} />
                                    </button>
                                </div>
                            </div>
                            <div style={{ padding: 25 }}>
                                <MuiThemeProvider theme={getMuiTheme()}>
                                    <MUIDataTable
                                        theme={getMuiTheme()}
                                        data={data}
                                        columns={columns}
                                        options={options}
                                    />
                                </MuiThemeProvider>
                            </div>
                        </div>
                    </div>
                :    
                this.state.visualisasi == true ?
                    <VisualReportItems
                        onClickClose={() => this.setState({ visualisasi: false, itemReport: true })}
                        height= {this.props.height}
                    />
                :
                <div style={{ height: this.props.height }}>
                        <div style={{ height: 199, width: '100%' }} className={"main-color"}/>
                        <div> 
                            <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={options}
                                            />
                                        </MuiThemeProvider>
                                    )}
                                </div>
                                <div style={{ display: 'flex', width: '100%', placeContent: 'flex-end', padding: 20 }}>
                                    <button
                                        type="button"
                                        onClick={() => this.setState({ itemReport: true })}
                                        style={{ marginRight: 20}}
                                    >
                                        <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>
                                    </button>
                                    <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>
                                </div>
                            </div>
                        </div>
                    </div>
                }
                {this.state.add && (
                    <CreateReportItems 
                        onClickClose={this.closeAdd.bind(this)}
                        data={this.state.indexData}
                    />
                )}  
                {this.state.edit && (
                    <EditReportItems 
                        onClickClose={this.closeEdit.bind(this)}
                        data={this.state.indexData}
                    />
                )}  
                {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, itemReport: false })}
                            />
                        </div>
                    </div>
                )}
            </div> 
        );
    }
}
