import { Empty, Form } from 'antd';
import {Grid, GridItem} from '../../components/Grid'
import '../../styles/index.css'

const renderSwitchableColumn = (column, itm) => {
  if(column.render) {
    return ( 
      <Form.Item
        name={column.dataIndex}
        style={{margin: 0}}
        rules={column.formItemRules}
      >
        {column.render(itm[column.dataIndex])}    
      </Form.Item>
    )
  }

  return <p className='m-5px-auto'>{itm[column.dataIndex]}</p>
}

export const View = ({columns, item}) => {
  if (!item || !columns?.length)
    return <Empty image={Empty.PRESENTED_IMAGE_SIMPLE}/>

  const colsCount = 3
  const viewItems = columns.map( (column, i) => {
    const row = Math.floor(i / colsCount) + 1
    const colb = i % colsCount

    return <>
      <GridItem 
        row={row} 
        col={colb*2 + 1}
        style={{background:'#00000005', 
                //width: '300px',
                border:'1px solid black'
              }}
      >
        {column.title}
      </GridItem>

      <GridItem 
        row={row} 
        col={colb*2 + 2}
        style={{background:'#00000005',
                //width: '150px',
                border:'1px solid black', 
                justifyContent:'right', 
                marginRight:'16px', 
                fontWeight:'bold', 
                textAlign:'right'}}
      >
        {renderSwitchableColumn(column, item)}
      </GridItem>
    </>
  })

  return <>
    <Grid>
      {viewItems}
    </Grid>
  </>
}