<?php
namespace lib\excel;
/**
 * excelBase
 * @author HzqGhost admin@whiledo.com QQ:313143468
 * @version 1.0.0
 *
 */
class excelBase
{
    public $dirname = "";
    public $filename = "";
    public $headerFilename = null;
    protected $isConstMemory = true; //固定内存模式
    protected $fp = null; //流
    protected $isOutput = false; //是否输出到客户端
	public function __construct()
    {
        
    }

	public function header(string $outFilename=''):excelInterface
    {
        $filename = $outFilename ? $outFilename : $this->filename;
        $this->isOutput = true;
        ob_end_clean();
        header('Content-Description: File Transfer');
        header("Content-type:application/vnd.ms-excel");
        header('Content-Disposition: attachment; filename='.basename($filename));
        ob_start();
        return $this;
    }

    public function setConstMemory(bool $constMemory):excelInterface
    {
        $this->isConstMemory = $constMemory;
        return $this;
    }

    public function setDirFile(string $fullfilename):excelInterface
    {
        $this->setDir(dirname($fullfilename));
        $this->setFile(basename($fullfilename));
        return $this;
    }

    public function setDir(string $dirname):excelInterface
    {
        $this->dirname = $dirname;
        return $this;
    }

    public function setFile(string $filename):excelInterface
    {
        $this->filename = basename($filename);
        return $this;
    }

    public function setStream($fp, string $filename):excelInterface{
        if(!is_resource($fp)){
            trigger_error( slg('请传递一个文件流'), E_USER_ERROR);
        }
        $this->fp = $fp;
        $this->headerFilename = $filename;
        return $this;
    }

    public function formatSheetName(string $sheetName):string
    {
        $sheetName = str_replace(['/','\\',':','?','*','[',']','"',"'"],' ',$sheetName);
        return substr($sheetName,0,31);
    }

    public function formatFileName(string $filename):string
    {
    	return str_replace(['/','\\',':','?','*','[',']','"',"'","|","<",">"],'_',$filename);
    }

    public function each($callback,$sheetName=null)
    {
        
    }

    public function toArray($types=[]):array
    {
        return [];
    }

    public function toCsv($dist):bool
    {
        if(empty($this->dirname)){
            trigger_error( slg('未定义excel输出目录,请先使用 $excel->setDir($dirname)'), E_USER_ERROR);
        }
        if(empty($this->filename)){
            trigger_error( slg('未定义excel输出文件,请先使用 $excel->setFile($filename)'), E_USER_ERROR);
        }
        return false;
    }

    public function zipFile(array $filelist,string $zipfile, bool $isDelete=true):excelInterface
    {
        $zip = new \ZipArchive();
        if ($zip->open($zipFile,\ZipArchive::CREATE|\ZipArchive::OVERWRITE) === true) {
            foreach($fileList as $file=>$ename){
                $zip->addFile($file,$ename);
            }
            if($isDelete){
                foreach($fileList as $file=>$ename){
                    unlink($file);
                }
            }
            $zip->close();
        }
    }

    //数字转字母列
    protected function int2Excel($num)
    {
    	$az = 26;
    	$m = (int)($num % $az);
    	$q = (int)($num / $az);
    	$letter = chr(ord('A') + $m);
    	if ($q > 0) {
    		return int2Excel($q - 1) . $letter;
    	}
    	return $letter;
    }

}