<?php

/**
 * +----------------------------------------------------------------------
 * | Created By PhpStorm
 * +----------------------------------------------------------------------
 * | DATE: 2021/7/13
 * +----------------------------------------------------------------------
 * | TIME: 16:06
 * +----------------------------------------------------------------------
 * | Author: YangXing(925032253@qq.com)
 * +----------------------------------------------------------------------
 * | Class: Write
 * +----------------------------------------------------------------------
 */

namespace servers\excel;

use \Vtiful\Kernel\Excel;
use Vtiful\Kernel\Excel as xlsx;
use \Vtiful\Kernel\Format;



/**
 * Class Write
 * @package Servers\Excel
 */
class write extends \servers\excel\excel
{

    /**
     * @var null
     */
    private $colorStyle = null;

    /**
     * @var null
     */
    private $boldStyle = null;

    /**
     * @var object|Excel|null
     */
    public $fileObject = null;

    /**
     * @var array
     */
    private $config = [];

    /**
     * 文件名称
     * @var string
     */
    protected $fileName = '';

    /**
     * excel 操作类
     * @var object
     */
    protected $excel = null;

    /**
     * write constructor.
     * @param $fileName
     */
    public function __construct($fileName)
    {
        $this->fileName = $fileName;

        $this->config['path'] = $this->writePath();

        $this->excel = new xlsx($this->config);

        //固定内存模式
        $this->fileObject = $this->excel->constMemory($this->fileName);
        return $this;
    }

    /**
     * @param $header
     * @return $this
     */
    public function setHeader($header)
    {
        $this->header = $header;
        $this->fileObject->header($header);
        return $this;
    }

    /**
     * @return $this
     */
    public function setBold($range)
    {
        if(is_null($this->boldStyle)){
            $fileHandle = $this->fileObject->getHandle();
            $format     = new Format($fileHandle);
            $this->boldStyle  = $format->bold()->toResource();
        }
        $this->fileObject->setRow($range, 15, $this->boldStyle);
        return $this;
    }

    /**
     * @param $range
     * @return $this
     */
    public function setColor($range)
    {
        if(is_null($this->colorStyle))
        {
            $fileHandle = $this->fileObject->getHandle();
            $format     = new Format($fileHandle);
            $this->colorStyle = $format->fontColor(Format::COLOR_RED)->toResource();
        }

        $this->fileObject->setRow($range, 15, $this->colorStyle); // 应用样式
        return $this;
    }

    /**
     * @param $row
     * @param $rowNumber
     * @return $this
     */
    public function writeRow($row,$rowNumber)
    {
        if($rowNumber % 1000000 == 0) $this->fileObject->addSheet();
        $this->fileObject->data([$row]);
        return $this;
    }

    /**
     * 返回文件名
     * @return mixed
     */
    public function output()
    {
        return $this->fileObject->output();
    }
}
