<?php if (!defined('BASEPATH')) exit('No direct script access allowed');


use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class Import extends Builder_Controller {

	function __construct()
	{
		  parent::__construct();
		  require APPPATH . 'libraries/vendor/autoload.php';
		  $this->load->model('cad_model');
	}
	
	function index()
	{
	    echo('hello world'); //teste 123
	}
	
	function index1()
	{
	    $inputFileName = '../html/assets/uploads/imoveis.xlsx';
	    
	    /**  Create a new Reader of the type defined in $inputFileType  **/
        //$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xls');
        /**  Load $inputFileName to a Spreadsheet Object  **/
        //$spreadsheet = $reader->load($inputFileName);
	    
	    $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileName);
	    
	    //var_dump($spreadsheet); exit();
	    
	    //$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileName);
	    
	    //$valida = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(1, 2)->getValue();
	    
	    if(strcasecmp(trim($this->get_cell_value($spreadsheet,1,2)),'Empreendimento') == 0)
	    {
	        $worksheet = $spreadsheet->getActiveSheet(); 
            $highestRow = $worksheet->getHighestRow(); // e.g. 10
            
            $import = array();
            for($row = 3; $row < $highestRow; $row ++)
            {
                $import[] = array(
                    'area_total' => $this->get_cell_value($spreadsheet,10,$row),
                    'area_privativa' => $this->get_cell_value($spreadsheet,11,$row),
                    'quartos' => $this->get_cell_value($spreadsheet,13,$row),
                    'vaga' => $this->get_vagas($this->get_cell_value($spreadsheet,14,$row)),
                    'val_pretendido' => $this->get_cell_value($spreadsheet,15,$row)
                );                
            }
            
            var_dump($import);
	        
	        //$teste = $worksheet->getRowIterator();
	        
	        /*echo '<table>' . PHP_EOL;
	        foreach ($worksheet->getRowIterator() as $row) {
            echo '<tr>' . PHP_EOL;
            $cellIterator = $row->getCellIterator();
            $cellIterator->setIterateOnlyExistingCells(FALSE); // This loops through all cells,
                                                               //    even if a cell value is not set.
                                                               // By default, only cells that have a value
                                                               //    set will be iterated.
                                                               
            $count = 0;                                                            
            foreach ($cellIterator as $letter => $cell) {
                echo '<td>' .
                     //$cell->getValue();
                     $letter.
                     
                '</td>' . PHP_EOL;
                $count++;
            }
                echo '</tr>' . PHP_EOL;
            }
            echo '</table>' . PHP_EOL;*/
	        
	        //echo 'true';
	    }
	    
	    /*'area_privativa',
        'area_total',
        'vaga',
        'val_pretendido',
        'quartos',*/
	    
	    /*->rangeToArray(
        'A2',     // The worksheet range that we want to retrieve
        NULL,        // Value that should be returned for empty cells
        TRUE,        // Should formulas be calculated (the equivalent of getCalculatedValue() for each cell)
        TRUE,        // Should values be formatted (the equivalent of getFormattedValue() for each cell)
        TRUE         // Should the array be indexed by cell row and cell column
        );*/

	    
	    /*$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($inputFileName);
        $spreadSheet = $reader->load($reader);*/

        /** Load $inputFileName to a Spreadsheet Object  **/
        //$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileName);
	    
	    //var_dump($worksheet);
	}
	
	private function get_vagas($str)
	{
	    $output = preg_split( "/e|,/", $str );
	    
	    return count($output);
	}
	
	private function get_cell_value($spreadsheet,$column,$row)
	{
	    return $spreadsheet->getActiveSheet()->getCellByColumnAndRow($column,$row)->getValue();
	}
	
}