<?php namespace Excel\Lib;
use Dever;
Dever::apply('autoload', 'excel', 'vendor');
class Export extends Core
{
    public function act($data = array(), $header = array(), $fileName = '', $sheet = 0, $sheetName = '', $return = false, $xls = false, $password = '', $save = '')
    {
        if (!$xls) {
            $xls = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
        }
        if ($sheet > 0) {
            $xls->createSheet();
        }
        $act = $xls->setActiveSheetIndex($sheet);
        if ($sheetName) {
            $act->setTitle($sheetName);
        }
        $row = 1;
        if($header) {
            $i = 0;
            if (isset($header['top'])) {
                foreach($header['top'] as $v) {
                    $act->setCellValue($this->cell[$i] . $row, $v);
                    $act->getColumnDimension($this->cell[$i])->setWidth(30);
                    $i++;
                }
                $row++;
                unset($header['top']);
            }
            $i = 0;
            foreach($header as $v) {
                $act->setCellValue($this->cell[$i] . $row, $v);
                $act->getColumnDimension($this->cell[$i])->setWidth(30);
                $i++;
            }
            $row++;
        }

        if($data) {
            $i = 0;
            $height = $max = 80;
            foreach($data as $v) {
                $j = 0;
                foreach($v as $cell) {
                    //$cell = strip_tags($cell);
                    $html = \Dever\Helper\Str::ishtml($cell);
                    if ($html) {
                        $wizard = new \PhpOffice\PhpSpreadsheet\Helper\Html;
                        $cell = $wizard->toRichTextObject('<?xml encoding="UTF-8">' . $cell);
                    }
                    if (!$html && (strstr($cell, '.jpg') || strstr($cell, '.gif') || strstr($cell, '.png'))) {
                        $key = ($i+$row);
                        $value = false;

                        if (strpos($cell, '||')) {
                            $t = explode('||', $cell);
                            $cell = $t[1];
                            $value = $t[0];
                        }
                        $temp = explode(',', $cell);

                        foreach ($temp as $ck => $cv) {
                            $objDrawing[$ck] = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
                            /*
                            if (Dever::project('upload')) {
                                $cv = str_replace('.jpg', '_t1.jpg', $cv);
                                $cv = str_replace('.png', '_t1.png', $cv);
                                $cv = Dever::load('upload/view')->get($cv);
                            }
                            $cv = Dever::local($cv);
                            */
                            if (!is_file($cv)) {
                                continue;
                            }
                            $objDrawing[$ck]->setPath($cv);
                            $objDrawing[$ck]->setHeight($height);
                            //$objDrawing[$ck]->setWidth(150);
                            $objDrawing[$ck]->setCoordinates($this->cell[$j] . ($i+$row));
                            $objDrawing[$ck]->setOffsetX(12);
                            if ($ck == 0) {
                                $offsetY = 5;
                            } else {
                                $offsetY = $offsetY + $height + 5;
                            }
                            $objDrawing[$ck]->setOffsetY($offsetY);
                            $objDrawing[$ck]->setWorksheet($act);
                        }
                        if ($value) {
                            $act->setCellValue($this->cell[$j] . ($i+$row), $value);
                        }
                        
                        $th = $height * count($temp);
                        if ($th > $max) {
                            $max = $th;
                        }
                        $act->getRowDimension($i+$row)->setRowHeight($max);
                        
                    } else {
                        if (!$cell) {
                            //$cell = "";
                        }
                        if (is_numeric($cell) && mb_strlen($cell) >= 10) {
                            $cell .= "\t";
                        }
                        $act->setCellValue($this->cell[$j] . ($i+$row), $cell);
                        $act->getStyle($this->cell[$j] . ($i+$row))->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
                    }
                    
                    $act->getColumnDimension($this->cell[$j])->setAutoSize(true);
                    $act->getColumnDimension($this->cell[$j])->setWidth(30);
                    $j++;
                }
                $i++;
            }
        }
        if ($header && $return) {
            return $xls;
        }
        if (!$fileName) {
            $fileName = uniqid(time(),true);
        }
        $write = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($xls, "Xlsx");
        if ($save) {
            $save = Dever::file('excel/' . $save . '.xlsx');
            $write->save($save);
            if ($password) {
                Dever::apply('autoload', 'excel', 'secure');
                $encrypt = new \Nick\SecureSpreadsheet\Encrypt();
                $encrypt->input($save)->password($password)->output($save);
            }
            return $save;
        } else {
            if ($password) {
                $save = Dever::file('excel/' . $save . '.xlsx');
                $write->save($save);
                Dever::apply('autoload', 'excel', 'secure');
                $encrypt = new \Nick\SecureSpreadsheet\Encrypt();
                $encrypt->input($save)->password($password)->output($save);
                header('Content-Type: application/octet-stream');
                header('Content-Transfer-Encoding: Binary');
                header("Content-disposition: attachment; filename=\"" . basename($save) . "\"");
                readfile($save);
                @unlink($save);
            } else {
                ob_end_clean();
                header('Content-Type: application/vnd.ms-excel');
                header('pragma:public');
                header("Content-Disposition:attachment;filename=$fileName.xlsx");
                $write->save('php://output');
            }
        }
    }
}