import { createMuiTheme, FormControlLabel, MuiThemeProvider, Paper, TableCell, Tooltip, Typography, withStyles } from '@material-ui/core'
import MUIDataTable from 'mui-datatables';
import React, { Component } from 'react'
import NumberFormat from 'react-number-format';
import ReactTooltip from 'react-tooltip';
import api from '../../api';
import Images from '../../assets/Images';
var ct = require("../../library/CustomTable");
const getMuiTheme = () => createMuiTheme(ct.customTable3());

const options = ct.customOptionsFixedColumn();
const style = {
    position: "sticky",
    left: 0,
    background: "white",
    zIndex: 101,
};
const style2 = {
    position: "sticky",
    background: "white",
    zIndex: 100
};

const LightTooltip = withStyles((theme) => ({
    tooltip: {
        backgroundColor: theme.palette.common.white,
        color: 'rgba(0, 0, 0, 0.87)',
        boxShadow: theme.shadows[1],
        fontSize: 11,
    },
}))(Tooltip);
export default class CashFlow extends Component {
    constructor(props) {
        super(props)
        this.state = {
            dataTable: []
        }
    }

    componentDidMount() {
        this.setState({loading: true})
        this.getItemHierarki()
    }

    getItemHierarki() {
        this.setState({ loading: true })
        let payload = {
            "report_id": this.props.report_id,
            "revision": Number(this.props.revision),
            "periode": this.props.periode,
            "company_id": this.props.company.company_id,
            "submission_id": this.props.submissionID
        }
        // console.log(JSON.stringify(payload))
        api.create().getDetailReportMB(payload).then(response => {
            let dataTable = []
            console.log(response)
            let res = response.data.data
            const handlePushChild = (item) => {
                dataTable.push([
                    item.type_report_id,
                    item.id,
                    item.parent,
                    item.formula,
                    item.level,
                    item.description,
                    item.cash_flow.total_actual_before === null ? "0" : item.cash_flow.total_actual_before === "" ? "0" : item.cash_flow.total_actual_before,
                    item.type_report_id == 5 || item.type_report_id == 6 ? { value: item.cash_flow.january, formula: item.cash_flow.january_formula } : item.cash_flow.january,
                    item.type_report_id == 5 || item.type_report_id == 6 ? { value: item.cash_flow.february, formula: item.cash_flow.february_formula } : item.cash_flow.february,
                    item.type_report_id == 5 || item.type_report_id == 6 ? { value: item.cash_flow.march, formula: item.cash_flow.march_formula } : item.cash_flow.march,
                    item.type_report_id == 5 || item.type_report_id == 6 ? { value: item.cash_flow.april, formula: item.cash_flow.april_formula } : item.cash_flow.april,
                    item.type_report_id == 5 || item.type_report_id == 6 ? { value: item.cash_flow.may, formula: item.cash_flow.may_formula } : item.cash_flow.may,
                    item.type_report_id == 5 || item.type_report_id == 6 ? { value: item.cash_flow.june, formula: item.cash_flow.june_formula } : item.cash_flow.june,
                    item.type_report_id == 5 || item.type_report_id == 6 ? { value: item.cash_flow.july, formula: item.cash_flow.july_formula } : item.cash_flow.july,
                    item.type_report_id == 5 || item.type_report_id == 6 ? { value: item.cash_flow.august, formula: item.cash_flow.august_formula } : item.cash_flow.august,
                    item.type_report_id == 5 || item.type_report_id == 6 ? { value: item.cash_flow.september, formula: item.cash_flow.september_formula } : item.cash_flow.september,
                    item.type_report_id == 5 || item.type_report_id == 6 ? { value: item.cash_flow.october, formula: item.cash_flow.october_formula } : item.cash_flow.october,
                    item.type_report_id == 5 || item.type_report_id == 6 ? { value: item.cash_flow.november, formula: item.cash_flow.november_formula } : item.cash_flow.november,
                    item.type_report_id == 5 || item.type_report_id == 6 ? { value: item.cash_flow.december, formula: item.cash_flow.december_formula } : item.cash_flow.december,
                    item.cash_flow.total_current_year,
                    item.type_report_id == 5 || item.type_report_id == 6 ? 0 : item.cash_flow.total_next_year,
                    item.type_report_id == 5 || item.type_report_id == 6 ? 0 : item.cash_flow.total_more_year,
                    item.order
                ])
                
                if (item.children !== null) {
                    if (item.children.length > 0) {
                        item.children.map((items, indexs) => {
                            handlePushChild(items)
                        })
                    }
                }
            }
            res.map((item, index) => {
                dataTable.push([
                    item.type_report_id,
                    item.id,
                    item.parent,
                    item.formula,
                    item.level,
                    item.description,
                    item.cash_flow.total_actual_before === null ? "0" : item.cash_flow.total_actual_before === "" ? "0" : item.cash_flow.total_actual_before,
                    item.type_report_id == 5 || item.type_report_id == 6 ? { value: item.cash_flow.january, formula: item.cash_flow.january_formula } : item.cash_flow.january,
                    item.type_report_id == 5 || item.type_report_id == 6 ? { value: item.cash_flow.february, formula: item.cash_flow.february_formula } : item.cash_flow.february,
                    item.type_report_id == 5 || item.type_report_id == 6 ? { value: item.cash_flow.march, formula: item.cash_flow.march_formula } : item.cash_flow.march,
                    item.type_report_id == 5 || item.type_report_id == 6 ? { value: item.cash_flow.april, formula: item.cash_flow.april_formula } : item.cash_flow.april,
                    item.type_report_id == 5 || item.type_report_id == 6 ? { value: item.cash_flow.may, formula: item.cash_flow.may_formula } : item.cash_flow.may,
                    item.type_report_id == 5 || item.type_report_id == 6 ? { value: item.cash_flow.june, formula: item.cash_flow.june_formula } : item.cash_flow.june,
                    item.type_report_id == 5 || item.type_report_id == 6 ? { value: item.cash_flow.july, formula: item.cash_flow.july_formula } : item.cash_flow.july,
                    item.type_report_id == 5 || item.type_report_id == 6 ? { value: item.cash_flow.august, formula: item.cash_flow.august_formula } : item.cash_flow.august,
                    item.type_report_id == 5 || item.type_report_id == 6 ? { value: item.cash_flow.september, formula: item.cash_flow.september_formula } : item.cash_flow.september,
                    item.type_report_id == 5 || item.type_report_id == 6 ? { value: item.cash_flow.october, formula: item.cash_flow.october_formula } : item.cash_flow.october,
                    item.type_report_id == 5 || item.type_report_id == 6 ? { value: item.cash_flow.november, formula: item.cash_flow.november_formula } : item.cash_flow.november,
                    item.type_report_id == 5 || item.type_report_id == 6 ? { value: item.cash_flow.december, formula: item.cash_flow.december_formula } : item.cash_flow.december,
                    item.cash_flow.total_current_year,
                    item.type_report_id == 5 || item.type_report_id == 6 ? 0 : item.cash_flow.total_next_year,
                    item.type_report_id == 5 || item.type_report_id == 6 ? 0 : item.cash_flow.total_more_year,
                    item.order
                ])
                if (item.children !== null) {
                    if (item.children.length > 0) {
                        item.children.map((items, indexs) => {
                            handlePushChild(items)
                        })
                    }
                }
            })
            this.setState({ dataTable, loading: false, dataReal: res, editable: true }, () => console.log(dataTable))
        })
    }

    render() {
        let dataTable2 = this.state.dataTable
        let columns = [{
            name: "",
            options: {
                display: false
            }
        }, {
            name: "",
            options: {
                display: false
            }
        }, {
            name: "",
            options: {
                display: false
            }
        }, {
            name: "",
            options: {
                display: false
            }
        }, {
            name: "",
            options: {
                display: false
            }
        },
            {
                name: "(In Rupiah million)",
                options: {
                    customHeadRender: (columnMeta) => (
                        <TableCell key={columnMeta.index} style={{ ...style, top: 0, zIndex: 102, backgroundColor: '#1c71b8', width: 300 }}>
                            <Typography style={{ color: 'white', fontSize: 12, fontWeight: 'bold', textAlign: 'left' }}>{columnMeta.name}</Typography>
                        </TableCell>
                    ),
                    setCellProps: () => ({ style }),
                    customBodyRender: (val, tableMeta) => {
                        return (
                            <div style={{ width: 300 }}>
                                {tableMeta.rowData[24] ?
                                    tableMeta.rowData[24].length > 0 ?
                                        <div style={{ paddingLeft: 20 * Number(tableMeta.rowData[4]) }}>
                                            <LightTooltip title={"Report Items Not Registered"} arrow>
                                                <span style={{ fontSize: 12, color: 'red' }}>{tableMeta.rowData[0] === 4 ? "" : val}</span>
                                            </LightTooltip>
                                        </div>
                                        :
                                        tableMeta.rowData[4] == 0 ?
                                            <span style={{ fontSize: 12, fontWeight: 'bold' }}>{String(tableMeta.rowData[0] === 4 ? "" : val).toUpperCase()}</span>
                                            :
                                            <div style={{ paddingLeft: 20 * Number(tableMeta.rowData[4]) }}>
                                                <span style={{ fontSize: 12 }}>{tableMeta.rowData[0] === 4 ? "" : val}</span>
                                            </div>
                                    :
                                    tableMeta.rowData[4] == 0 ?
                                        <span style={{ fontSize: 12, fontWeight: 'bold' }}>{String(tableMeta.rowData[0] === 4 ? "" : val).toUpperCase()}</span>
                                        :
                                        <div style={{ paddingLeft: 20 * Number(tableMeta.rowData[4]) }}>
                                            <span style={{ fontSize: 12 }}>{tableMeta.rowData[0] === 4 ? "" : val}</span>
                                        </div>
                                }
                            </div>
                        )
                    }
                }
            },
            // {
            //     name: `MB 2021`,
            //     options: {
            //         customHeadRender: (columnMeta) => (
            //             <th style={{ ...style2, color: '#fff', backgroundColor: '#07a7d0', fontSize: 13, fontWeight: 1, width: 150, borderRight: "1px solid rgb(255, 255, 255)", padding: 0 }} >
            //                 {/* <TableCell style={{ ...style2, top: 0, zIndex: 99, backgroundColor: '#1c71b8', width: 96 }}>
            //                     <Typography style={{ color: 'white', fontSize: 12, fontWeight: 'bold', textAlign: 'center' }}>{columnMeta.name}</Typography>
            //                 </TableCell> */}
            //                 <div style={{ borderBottom: "1px #fff solid", backgroundColor: '#1c71b8', textAlign: 'center', fontSize: 12, fontWeight: 'bold', padding: 5 }}>{columnMeta.name}</div>
            //                 <div className="grid grid-3x" style={{ ...style2, color: '#fff', fontSize: 12, fontWeight: 'bold', position: "sticky" }}>
            //                     <div className="column-1 grid grid-4x" style={{ placeSelf: 'center', textAlign: 'center', backgroundColor: '#07a7d0' }}>
            //                         <div className="column-1" style={{ placeSelf: 'center', textAlign: 'center', padding: 5, borderRight: "1px #fff solid", backgroundColor: '#07a7d0' }}>
            //                             <span>{"1"}</span>
            //                         </div>
            //                         <div className="column-2" style={{ placeSelf: 'center', textAlign: 'center', padding: 5, borderRight: "1px #fff solid", backgroundColor: '#07a7d0' }}>
            //                             <span>{"2"}</span>
            //                         </div>
            //                         <div className="column-3" style={{ placeSelf: 'center', textAlign: 'center', padding: 5, borderRight: "1px #fff solid", backgroundColor: '#07a7d0' }}>
            //                             <span>{"3"}</span>
            //                         </div>
            //                         <div className="column-4" style={{ placeSelf: 'center', textAlign: 'center', padding: 5, borderRight: "1px #fff solid", backgroundColor: '#07a7d0' }}>
            //                             <span>{"4"}</span>
            //                         </div>
            //                     </div>
            //                     <div className="column-2 grid grid-4x" style={{ placeSelf: 'center', textAlign: 'center', backgroundColor: '#07a7d0' }}>
            //                         <div className="column-1" style={{ placeSelf: 'center', textAlign: 'center', padding: 5, borderRight: "1px #fff solid", backgroundColor: '#07a7d0' }}>
            //                             <span>{"5"}</span>
            //                         </div>
            //                         <div className="column-2" style={{ placeSelf: 'center', textAlign: 'center', padding: 5, borderRight: "1px #fff solid", backgroundColor: '#07a7d0' }}>
            //                             <span>{"6"}</span>
            //                         </div>
            //                         <div className="column-3" style={{ placeSelf: 'center', textAlign: 'center', padding: 5, borderRight: "1px #fff solid", backgroundColor: '#07a7d0' }}>
            //                             <span>{"7"}</span>
            //                         </div>
            //                         <div className="column-4" style={{ placeSelf: 'center', textAlign: 'center', padding: 5, borderRight: "1px #fff solid", backgroundColor: '#07a7d0' }}>
            //                             <span>{"8"}</span>
            //                         </div>
                                    
            //                     </div>
            //                     <div className="column-3 grid grid-4x" style={{ placeSelf: 'center', textAlign: 'center', backgroundColor: '#07a7d0' }}>
            //                         <div className="column-1" style={{ placeSelf: 'center', textAlign: 'center', padding: 5, borderRight: "1px #fff solid", backgroundColor: '#07a7d0' }}>
            //                             <span>{"9"}</span>
            //                         </div>
            //                         <div className="column-2" style={{ placeSelf: 'center', textAlign: 'center', padding: 5, borderRight: "1px #fff solid", backgroundColor: '#07a7d0' }}>
            //                             <span>{"10"}</span>
            //                         </div>
            //                         <div className="column-3" style={{ placeSelf: 'center', textAlign: 'center', padding: 5, borderRight: "1px #fff solid", backgroundColor: '#07a7d0' }}>
            //                             <span>{"11"}</span>
            //                         </div>
            //                         <div className="column-4" style={{ placeSelf: 'center', textAlign: 'center', padding: 5, backgroundColor: '#07a7d0' }}>
            //                             <span>{"12"}</span>
            //                         </div>
            //                     </div>
                                
            //                 </div>
            //             </th>
            //         ),
            //         setCellProps: () => ({ style2 }),
            //         customBodyRender: (val, tableMeta, updateValue) => {
            //             return (
            //                 <div>
            //                     <div className="grid grid-3x content-center">
            //                         <div className="grid grid-4x content-center">
            //                             <div className="col-1">
            //                                 <div style={{ textAlign: 'right', width: 90 }}>
            //                                     {/* {val.a} */}
            //                                 </div>
            //                             </div>
            //                             <div className="col-2">
            //                                 <div style={{ textAlign: 'right', width: 90 }}>
            //                                     {/* {val.b} */}
            //                                 </div>
            //                             </div>
            //                             <div className="col-3">
            //                                 <div style={{ textAlign: 'right', width: 90 }}>
            //                                     {/* {val.c} */}
            //                                 </div>
            //                             </div>
            //                             <div className="col-4">
            //                                 <div style={{ textAlign: 'right', width: 90 }}>
            //                                     {/* {val.c} */}
            //                                 </div>
            //                             </div>
            //                         </div>
            //                         <div className="grid grid-4x content-center">
            //                             <div className="col-1">
            //                                 <div style={{ textAlign: 'right', width: 90 }}>
            //                                     {/* {val.a} */}
            //                                 </div>
            //                             </div>
            //                             <div className="col-2">
            //                                 <div style={{ textAlign: 'right', width: 90 }}>
            //                                     {/* {val.b} */}
            //                                 </div>
            //                             </div>
            //                             <div className="col-3">
            //                                 <div style={{ textAlign: 'right', width: 90 }}>
            //                                     {/* {val.c} */}
            //                                 </div>
            //                             </div>
            //                             <div className="col-4">
            //                                 <div style={{ textAlign: 'right', width: 90 }}>
            //                                     {/* {val.c} */}
            //                                 </div>
            //                             </div>
            //                          </div>
            //                         <div className="grid grid-4x content-center">
            //                             <div className="col-1">
            //                                 <div style={{ textAlign: 'right', width: 90 }}>
            //                                     {/* {val.a} */}
            //                                 </div>
            //                             </div>
            //                             <div className="col-2">
            //                                 <div style={{ textAlign: 'right', width: 90 }}>
            //                                     {/* {val.b} */}
            //                                 </div>
            //                             </div>
            //                             <div className="col-3">
            //                                 <div style={{ textAlign: 'right', width: 90 }}>
            //                                     {/* {val.c} */}
            //                                 </div>
            //                             </div>
            //                             <div className="col-4">
            //                                 <div style={{ textAlign: 'right', width: 90 }}>
            //                                     {/* {val.c} */}
            //                                 </div>
            //                             </div>
            //                         </div>
            //                     </div>
            //                 </div>
            //             )
            //         }
            //     }
            // },
            {
                name: `${Number(this.props.periode) + 1}`,
                options: {
                    customHeadRender: (columnMeta) => (
                        <th style={{ ...style2, color: '#fff', backgroundColor: '#07a7d0', top: 0, zIndex: 102, fontSize: 13, fontWeight: 1, width: 150, borderRight: "1px solid rgb(255, 255, 255)", padding: 0 }} >
                            {/* <TableCell style={{ ...style2, top: 0, zIndex: 99, backgroundColor: '#1c71b8', width: 96 }}>
                                <Typography style={{ color: 'white', fontSize: 12, fontWeight: 'bold', textAlign: 'center' }}>{columnMeta.name}</Typography>
                            </TableCell> */}
                            <div style={{ borderBottom: "1px #fff solid", backgroundColor: '#1c71b8', textAlign: 'center', fontSize: 12, fontWeight: 'bold', padding: 5 }}>{columnMeta.name}</div>
                            <div className="grid grid-1x" style={{ ...style2, color: '#fff', fontSize: 12, fontWeight: 'bold', position: "sticky" }}>
                                <div className="column-1" style={{ placeSelf: 'center', textAlign: 'center', padding: 5, backgroundColor: '#07a7d0' }}>
                                    <span>{"ForeCast"}</span>
                                </div>
                            </div>
                        </th>
                    ),
                    setCellProps: () => ({ style2 }),
                    customBodyRender: (value, tableMeta, updateValue) => {
                        return (
                            <div style={{ textAlign: 'right' }}>
                                {tableMeta.rowData[0] === 4 || tableMeta.rowData[0] === 1 ?
                                    null
                                    :
                                    <div style={{ flex: 1 }}>
                                        <FormControlLabel
                                            style={{ margin: 0 }}
                                            value={value}
                                            control={
                                                <NumberFormat
                                                    thousandSeparator={true}
                                                    style={{ fontSize: 12, textAlign: 'right', borderColor: 'transparent', margin: 0, width: 96, backgroundColor: 'transparent' }}
                                                    type="text"
                                                    placeholder=""
                                                    value={Number(tableMeta[22]).toFixed(1)}
                                                    disabled={true}
                                                    decimalScale={1}
                                                    onBlur={(event) => {
                                                        // updateValue(event.target.value)
                                                        // handleChange(event.target.value, tableMeta)
                                                        console.log(dataTable2)
                                                    }}
                                                />
                                            }
                                        />
                                    </div>}
                            </div>
                        )
                    }
                }
            },
            {
                name: `${Number(this.props.periode) + 2}`,
                options: {
                    customHeadRender: (columnMeta) => (
                        <th style={{ ...style2, color: '#fff', backgroundColor: '#07a7d0', fontSize: 13, top: 0, zIndex: 102, fontWeight: 1, width: 150, borderRight: "1px solid rgb(255, 255, 255)", padding: 0 }} >
                            {/* <TableCell style={{ ...style2, top: 0, zIndex: 99, backgroundColor: '#1c71b8', width: 96 }}>
                                <Typography style={{ color: 'white', fontSize: 12, fontWeight: 'bold', textAlign: 'center' }}>{columnMeta.name}</Typography>
                            </TableCell> */}
                            <div style={{ borderBottom: "1px #fff solid", backgroundColor: '#1c71b8', textAlign: 'center', fontSize: 12, fontWeight: 'bold', padding: 5 }}>{columnMeta.name}</div>
                            <div className="grid grid-1x" style={{ ...style2, color: '#fff', fontSize: 12, fontWeight: 'bold', position: "sticky" }}>
                                <div className="column-1" style={{ placeSelf: 'center', textAlign: 'center', padding: 5, backgroundColor: '#07a7d0' }}>
                                    <span>{"ForeCast"}</span>
                                </div>
                            </div>
                        </th>
                    ),
                    setCellProps: () => ({ style2 }),
                    customBodyRender: (val, tableMeta, updateValue) => {
                        return (
                            <div>
                                {/* {val} */}
                            </div>
                        )
                    }
                }
            },
        ]
        return (
            <div style={{ height: this.props.height, backgroundColor: '#f8f8f8', marginBottom: 100, minHeight: 1000 }}>
                <div className={"main-color"} style={{ height: 78, flex: 1, display: 'flex', alignItems: 'center', paddingLeft: 20 }}>
                    <Typography style={{ fontSize: '16px', color: 'white' }}>Pengajuan Budget Tahunan</Typography>
                </div>
                <div style={{ flex: 1, padding: 20, width: '100%' }}>
                    <Paper style={{ paddingTop: 10, paddingBottom: 20 }}>
                        <div style={{ borderBottom: 'solid 1px #c4c4c4' }} >
                            <Typography style={{ fontSize: '12px', color: '#4b4b4b', margin: 10 }}>Budget Tahunan - Cash Flow</Typography>
                        </div>
                        <div style={{ padding: 20 }}>
                                <div style={{ display: 'flex', justifyContent: 'space-between' }}>
                                    <div>
                                        <Typography style={{ fontSize: '11px', color: '#4b4b4b' }}>{this.props.company.company_name}</Typography>
                                        <Typography style={{ fontSize: '11px', color: '#4b4b4b' }}>Period : {this.props.periode} (rev.{this.props.revision})</Typography>
                                        <Typography style={{ fontSize: '11px', color: '#4b4b4b' }}>in IDR mn</Typography>
                                    </div>
                                    <div style={{ width: '50%' }}>
                                        <div style={{ 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={() => null}
                                                >
                                                    <img src={Images.template} />
                                                </button>
                                            </a>
                                            <ReactTooltip border={true} id="template" place="bottom" type="light" effect="solid" />
                                            <a data-tip={'Upload'} data-for="upload">
                                                <button
                                                    style={{
                                                        backgroundColor: 'transparent',
                                                        cursor: 'pointer',
                                                        borderColor: 'transparent',
                                                        margin: 5
                                                    }}
                                                    onClick={() => null}
                                                >
                                                    <img src={Images.upload} />
                                                </button>
                                            </a>
                                            <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={() => null}
                                                >
                                                    <img src={Images.download} />
                                                </button>
                                            </a>
                                            <ReactTooltip border={true} id="download" place="bottom" type="light" effect="solid" />
                                        </div>
                                    </div>
                                </div>

                                <div style={{ marginTop: 20, width: this.props.width - (this.props.open === true ? 400 : 150) }}>
                                    {!this.state.loading && <MuiThemeProvider theme={getMuiTheme()}>
                                        <MUIDataTable
                                            data={dataTable2}
                                            columns={columns}
                                            options={options}
                                        />
                                    </MuiThemeProvider>}
                                </div>
                            </div>
                    </Paper>
                </div>
            </div>
        )
    }
}
