<?php

namespace App\GenDocument\SpecialObject;

class General_NColumns extends GeneralAbstract
{

	public function render()
	{
		///////////////////////////////////////////////////////////////////////////////////////////////
		// MAIN TABLE PROPERTIES //////////////////////////////////////////////////////////////////////
		///////////////////////////////////////////////////////////////////////////////////////////////
		$table=new \DTMControl_table();

		$table->attributes['paddingTop'] = 		$this->settings['paddingTop'];
		$table->attributes['paddingBottom'] = 	$this->settings['paddingBottom'];
		$table->attributes['paddingLeft'] = 	$this->settings['paddingLeft'];
		$table->attributes['paddingRight'] = 	$this->settings['paddingRight'];

		$table->attributes['Xmm'] = 		$this->settings['Xmm'];
		$table->attributes['Ymm'] = 		$this->settings['Ymm'];
		$table->attributes['fixed'] = 		$this->settings['fixed'];
		$table->attributes['block'] = 		true;
		$table->attributes['layer'] = 		$this->settings['layer'];
		$table->attributes['orderIndex'] = 	$this->settings['orderIndex'];

		$table->attributes['show_nbsp']['th'] = true;
		$table->attributes['show_nbsp']['tr'] = true;
		$table->attributes['show_nbsp']['tf'] = true;


		// determine the number of columns
		$numberOfColumns=$this->settings['columnsCount'];
		if (!$numberOfColumns) {
			$numberOfColumns = 2; // fallback to 2 columns (the default)
		}

		$columnsList=[];
		for($i=1;$i<=$numberOfColumns;$i++){
			$columnsList[]="column{$i}";
		}

		///////////////////////////////////////////////////////////////////////////////////////////////
		// TABLE CONTENT //////////////////////////////////////////////////////////////////////////////
		///////////////////////////////////////////////////////////////////////////////////////////////

		$fontFamilyList=[];$WmmList=[];
		$style_boldList=[];$style_italicList=[];$style_underlineList=[];
		$fontSizeList=[];$colorList=[];$backgroundColorList=[];$alignList=[];
		$internal_margin_left=[];$internal_margin_right=[];
		foreach($columnsList as $thisCol) {
			$fontFamilyList[]=$this->settings['column_'.$thisCol.'Font'];
			$WmmList[]=$this->settings['column_'.$thisCol.'Width'];
			$style_boldList[]=(bool)$this->settings['column_'.$thisCol.'TextStyle']['bold'];
			$style_italicList[]=(bool)$this->settings['column_'.$thisCol.'TextStyle']['italic'];
			$style_underlineList[]=(bool)$this->settings['column_'.$thisCol.'TextStyle']['underline'];
			$fontSizeList[]=$this->settings['column_'.$thisCol.'FontSize'];
			$colorList[]=$this->settings['column_'.$thisCol.'TextColor'];
			$backgroundColorList[]=$this->settings['column_'.$thisCol.'BackgroundColor'];
			$alignList[]=$this->settings['column_'.$thisCol.'Alignment'];

			$internal_margin_left[]=$this->settings['column_'.$thisCol.'InternalMarginLeft'];
			$internal_margin_right[]=$this->settings['column_'.$thisCol.'InternalMarginRight'];
		}

		// add this bulk list of values. Empty items will be ignored anyway
		$trData=[];
		foreach($columnsList as $thisCol) {
			$trData[] = $this->settings['column_'.$thisCol.'Text'];
		}
		$table->add_tr_simple($trData, 'tr');

		// SET THE ATTRIBTUES FOR INVOICE ROWS ////////////////////////////////////////////////////////
		$table->setTdAttributeArr('fontFamily', $fontFamilyList, 'keep');
		$table->setTdAttributeArr('style_bold', $style_boldList, 'keep');
		$table->setTdAttributeArr('style_italic', $style_italicList, 'keep');
		$table->setTdAttributeArr('style_underline', $style_underlineList, 'keep');
		$table->setTdAttributeArr('fontSize', $fontSizeList, 'keep');
		$table->setTdAttributeArr('color', $colorList, 'keep');
		$table->setTdAttributeArr('backgroundColor', $backgroundColorList, 'keep');
		$table->setTdAttributeArr('align', $alignList, 'keep');
		$table->setTdAttributeArr('Wmm', $WmmList, 'keep');
		$table->setTdAttributeArr('internal_margin_left', $internal_margin_left, 'keep');
		$table->setTdAttributeArr('internal_margin_right', $internal_margin_right, 'keep');

		///////////////////////////////////////////////////////////////////////////////////////////////
		// FINISH TABLE ///////////////////////////////////////////////////////////////////////////////
		///////////////////////////////////////////////////////////////////////////////////////////////

		// SET BORDER PROPERTIES FOR THE WHOLE TABLE //////////////////////////////////////////////////
		if($this->settings['borderVisibility']['top'])
			$table->setTdAttribute('border_top', true);
		if($this->settings['borderVisibility']['left'])
			$table->setTdAttribute('border_left', true);
		if($this->settings['borderVisibility']['bottom'])
			$table->setTdAttribute('border_bottom', true);
		if($this->settings['borderVisibility']['right'])
			$table->setTdAttribute('border_right', true);

		$table->setTdAttributeArr('border_color', [$this->settings['borderColor']], 'wrap');
		$table->setTdAttributeArr('border_thickness', [$this->settings['borderThickness']], 'wrap');
		$table->setTdAttributeArr('border_style', [$this->settings['borderStyle']], 'wrap');

		$table->setTdAttributeArr('border_style', [$this->settings['borderStyle']], 'wrap','th');
		$table->setTdAttributeArr('border_thickness', [$this->settings['borderThickness']], 'wrap','th');
		$table->setTdAttributeArr('border_color', [$this->settings['borderColor']], 'wrap', 'th');

		$table->attributes['borderCollapse']=true;

		$return[]=$table->exportAsControl();
		return $return;

	}

}