<?php
namespace App\Alias;

use Slam\Excel\Helper as ExcelHelper;

class PHPExcel
{
  protected $name;
  protected $filename;
  protected $columns;
  protected $data;

  public function __construct($name, $filename)
  {
    $this->name = $name;
    $this->filename = $filename;
  }
  public function addColumns($fields)
  {
    $columns = [];
    foreach ($fields as $i => $field) {
      if (is_object($field)) {
        if (isset($field->style)) {
          $style = $this->getExcelStyle($field->style);
        } else {
          $style = $this->getExcelStyle();
        }
        $column = new ExcelHelper\Column($field->name, $field->name, 10, $style);
      } elseif (is_array($field)) {
        if (isset($field['style'])) {
          $style = $this->getExcelStyle($field['style']);
        } else {
          $style = $this->getExcelStyle();
        }
        $column = new ExcelHelper\Column($field['name'], $field['name'], 10, $style);
      } else {
        $style = $this->getExcelStyle();
        $column = new ExcelHelper\Column($field, $field, 10, $style);
      }
      $columns []= $column;
    }
    $collection = new ExcelHelper\ColumnCollection($columns);
    $this->columns = $collection;
  }
  protected function getExcelStyle($style = 'text')
  {
    switch (strtolower($style)) {
      case 'date':
        return new ExcelHelper\CellStyle\Date();
      case 'mes':
        return new Excel\Style\Mes();
      case 'currency':
      case 'amount':
        return new ExcelHelper\CellStyle\Amount();
      case 'number':
      case 'integer':
        return new ExcelHelper\CellStyle\Integer();
      case 'percent':
      case 'percentage':
        return new ExcelHelper\CellStyle\Percentage();
      case 'text':
      case 'string':
      default:
        return new ExcelHelper\CellStyle\Text();
    }
  }
  public function addData($data)
  {
    if ($this->data == null) {
      $this->data = [];
    }
    $this->data = array_merge($data);
  }
  public function addRow($rowData)
  {
    if ($this->data == null) {
      $this->data = [];
    }
    $this->data []= $rowData;
  }
  public function addTotals($totals)
  {
    $columns = (array) $this->columns;
    $columns = array_pop($columns);
    $ts = [];
    foreach ($columns as $column) {
      $col = $column->getHeading();
      if (isset($totals[$col])) {
        $ts[$col] = $this->getTotal($col, $totals[$col]);
        continue;
      }
      $ts[$col] = '';
    }
    $this->data []= $ts;
  }
  protected function getTotal($col, $aggr)
  {
    $col_num = $this->getColNumber($col);
    $col = $this->getColName($col_num);
    switch(strtolower($aggr)) {
      case 'sum':
        $num = 109;
        break;
      case 'count':
        $num = 102;
        break;
      case 'counta':
        $num = 103;
        break;
      default:
        $num = 0;
    }
    if ($num > 0) {
      $end = count($this->data) + 2;
      $str = "=SUBTOTAL({$num};{$col}3:{$col}{$end})";
      return $str;
    }
    return $aggr;
  }
  protected function getColNumber($col)
  {
    $columns = (array) $this->columns;
    $columns = array_keys(array_pop($columns));
    return array_search($col, $columns);
  }
  protected function getColName($col_num)
  {
    $cols = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $N = strlen($cols);
    $name = '';
    if ($col_num > $N) {
      $name .= $cols[floor($col_num / $N)];
      $col_num = $N * ($col_num / $N - floor($col_num / $N));
    }
    $name .= $cols[$col_num];
    return $name;
  }
  public function informe()
  {
    header("Content-Type: application/octet-stream; charset=utf-8");
		header('Content-Transfer-Encoding: binary');
		header('Content-Disposition: attachment; filename="' . $this->filename . '"');
		header('Cache-Control: max-age=0');

    $pE = new ExcelHelper\TableWorkbook('php://output');
    $ws = $pE->addWorksheet($this->name);

    $table = new ExcelHelper\Table($ws, 0, 0, $this->name, new \ArrayIterator($this->data));
    $table->setColumnCollection($this->columns);

    $pE->writeTable($table);

    $pE->close();
  }
}
