<?php

    namespace Pixebuilder\Element;
    
    // prevent direct file access
	if( !defined( 'ABSPATH' ) ) exit;

    class Table extends Item{

        static function get_name(){
            return 'table';
        }

        static function get_default(){
            return array(
                'padding' => array( 'bottom' => '40px' )
            );
        }

        static function get_title(){
            return esc_html__('Table', 'pixebuilder');
        }

        static function get_icon(){
            return 'toc';
        }
        
        static function render_content($settings){
            if( empty($settings['_tab']) || !is_array($settings['_tab']) ) return '';

            $tab = $settings['_tab'];
            $row = empty($settings['row'])? 0: absint($settings['row']);
            $col = empty($settings['col'])? 0: absint($settings['col']);
            $layout = empty($settings['layout']) || !is_array($settings['layout'])? array(): $settings['layout'];

            if( empty($col) ){
                $col = 1;
            }
            if( empty($row) ){
                $row = (int) ceil(count($tab) / $col);
            }
            if( empty($row) ) return '';

            $has_header = empty($layout['header']) || $layout['header'] !== 'no';
            $has_footer = empty($layout['footer']) || $layout['footer'] !== 'no';
            $head_atts = array(
                'typo-preset' => empty($settings['head-typo']['preset'])? '': $settings['head-typo']['preset']
            );
            $body_atts = array(
                'typo-preset' => empty($settings['body-typo']['preset'])? '': $settings['body-typo']['preset']
            );
            $footer_atts = array(
                'typo-preset' => empty($settings['footer-typo']['preset'])? '': $settings['footer-typo']['preset']
            );

            $rows = array();
            for( $i = 0; $i < $row; $i++ ){
                $row_html = '<tr>';
                for( $j = 0; $j < $col; $j++ ){
                    $index = ($i * $col) + $j;
                    $cell = empty($tab[$index]) || !is_array($tab[$index])? array(): $tab[$index];
                    $content = empty($cell['content']) || !is_array($cell['content'])? array(): $cell['content'];

                    $row_html .= '<td>';
                    if( !empty($content) ){
                        $row_html .= pixe_get_builder_content($content);
                    }
                    $row_html .= '</td>';
                }
                $row_html .= '</tr>';
                $rows[] = $row_html;
            }

            $ret = '<table>';
            if( $has_header && !empty($rows) ){
                $ret .= '<thead ' . pixe_get_data_atts($head_atts) . '>' . array_shift($rows) . '</thead>';
            }
            if( $has_footer && !empty($rows) ){
                $footer = array_pop($rows);
            }else{
                $footer = '';
            }
            if( !empty($rows) ){
                $ret .= '<tbody ' . pixe_get_data_atts($body_atts) . '>' . implode('', $rows) . '</tbody>';
            }
            if( !empty($footer) ){
                $ret .= '<tfoot ' . pixe_get_data_atts($footer_atts) . '>' . $footer . '</tfoot>';
            }
            $ret .= '</table>';

            return $ret;
        }

    }

    \Pixe_Builder::register_element('Pixebuilder\Element\Table');
    
    
    // register tab style
    function table_style(){
?>
.pixe-item-table table{ position: relative; text-align: center; }
.pixe-item-table table tr{ position: relative; }
.pixe-item-table table thead{ color: var(--pixe--color--heading); font-weight: 600; }
.pixe-item-table table th,
.pixe-item-table table td{ position: relative; vertical-align: middle; border-top-width: 0; border-left-width: 0; }
.pixe-item-table table th:last-child,
.pixe-item-table table td:last-child{ border-right: 0; }
.pixe-item-table table tbody tr:last-child td{ border-bottom: 0; }
.pixe-item-table table tfoot th,
.pixe-item-table table tfoot td{ border-top-width: 1px; border-bottom-width: 0; }
<?php
    }
    \Pixe_Dependency::get()->register_style('table', 'Pixebuilder\Element\table_style');
