import React from 'react';
import { Page, Text, View, Document, StyleSheet } from '@react-pdf/renderer'

const styles = StyleSheet.create({
    page: {
        backgroundColor: "#ffffff"
    },
    section: {
        margin: 5,
        padding: 2,
        flexGrow: 1,
        flexDirection: "row",
    },
    recordContainer:{
        display: "flex",
        flexDirection: "column",
        padding: 5,
        margin: 20,
        border: "1px solid rgb(212, 212, 212)"
    },
    label:{
        fontSize: 10,
        fontWeight: "bold",
        borderBottom: "1px solid rgb(212, 212, 212)",
        backgroundColor: "lightgrey",
        padding: 5

    },
    attributeValue: {
        fontSize: 10,
        borderLeft: "1px solid rgb(212, 212, 212)",
        padding: 5

    }

})

const ElementRows = ({ele, keys, objectIndex}) => {
    
    return (
        <View style={styles.recordContainer} break={objectIndex === 0 ? false : true}>
            {
                keys.map((theKey, theIndex) => (
                    <React.Fragment key={`attribute-${theKey}}`}>
                        <View style={styles.section}>
                            <Text style={styles.label}>{theKey}</Text>
                            <Text style={styles.attributeValue}>{ele[theKey]}</Text>
                        </View>
                    </React.Fragment>
                    )
                )
            }
        
        </View>
    )
}
const DefaultTemplateDocument = ({data}) => {
    var headers = null
    if(data.length > 0){
        headers = Object.keys(data[0])
        headers.sort()
    }

    return (
        <Document>
            <Page size="A4" style={styles.page}>
                {
                    data ? 
                        data.map((ele, index) => <ElementRows key={`row-${index}`} ele={ele} keys={headers} objectIndex={index}/>)
                    : <Text>No data present</Text>
                }
                
                
            </Page>
        </Document>
  )
}
export default DefaultTemplateDocument