<?php

/**
 * DBOX extension
 * @author N!k <nikola.tomic@diginout.com>
 * @version 1.0.0
 */
class DigPivot extends CWidget
{
    /**
     * @var array Konfiguracija pivot tabele.
     */
    public $config = array();


    /**
     * @var array Prikaz redova.
     */
    public $rows = array();

    /**
     * @var array  Prikaz kolona.
     */
    public $cols = array();

    /**
     * @var text SQL ya prikay
     */
    public $sql = 'SELECT *  FROM analizaprodanihartikala_view';

    /**
     * @var array Data provider.
     */
    public $data = array();

    /**
     * U slucaju da je editovanje prikazuje na drugi nacin.
     */
    public $edit_mode = false;

    public $view = 'index';


    /**
     * @var string To store the base url of the assets for the widget.
     */
    private $baseUrl;

    /**
     * Priprema widget
     * i registruje css i js fajlove.
     */
    public function init()
    {
        parent::init();
        $this->registerScripts();
        $this->setujView();

    }

    /**
     * Objavljuje   assets
     */
    protected function registerScripts()
    {
        $basePath = dirname(__FILE__) . DIRECTORY_SEPARATOR .
            'assets' . DIRECTORY_SEPARATOR;
        $baseUrl = Yii::app()->getAssetManager()->publish($basePath, false, 0, YII_DEBUG);


        // yabrana ucitavanja ybog konflikta ?? moyda i ne treba u pitanju je samo redosled
        Yii::app()->clientScript->scriptMap = array(
          //  'jquery.min.js' => false,
        );

        $cs = Yii::app()->clientScript;

        // JS
        $cs->registerScriptFile($baseUrl . '/d3.min.js');
        $cs->registerScriptFile($baseUrl . '/c3.min.js');

       // $cs->registerScriptFile($baseUrl . '/jquery_pivot.js');
       // $cs->registerScriptFile($baseUrl . '/pivot.min.js');
        $cs->registerScriptFile($baseUrl . '/jquery-ui.min.js');
        $cs->registerScriptFile($baseUrl . '/pivot.js');

        $cs->registerScriptFile($baseUrl . '/c3_renderers.min.js');
        $cs->registerScriptFile($baseUrl . '/export_renderers.js');

        $cs->registerScriptFile($baseUrl . '/jquery.ui.touch-punch.min.js');
       // $cs->registerScriptFile($baseUrl . '/tips_data.min');
        //tips_data.min

        // CSS
        $cs->registerCssFile($baseUrl . '/c3.min.css');
        $cs->registerCssFile($baseUrl . '/pivot.css');
        $cs->registerCssFile($baseUrl . '/fixedheader.css');
        $cs->registerCssFile($baseUrl . '/webpivot.css');
        $cs->registerCssFile($baseUrl . '/bootstrap.min.css');





    }

    public function setujView()
    {
        if ($this->edit_mode) {
            $this->view = 'index_edit';
        }
    }


    public function podesiData()
    {
        $dataProvider = new CSqlDataProvider($this->sql);
        $this->data = CJSON::encode($dataProvider->getData());
    }


    /**
     * @return string
     * Vraća konfiguraciju redova i kolona...
     */
    public function vratiConfig()
    {
        // ako je edit mod dodaj skriptu za rifresh na kraju komfiga
        $this->editModeConfig();
        return $this->config;

    }

    /**
     * @return string
     * Dodaje kod za rifres u edit modu
     */
    public function editModeConfig()
    {


        $rifresCODE = ',  onRefresh: function (config) {
                    var config_copy = JSON.parse(JSON.stringify(config));
                    //delete some values which are functions
                    delete config_copy["aggregators"];
                    delete config_copy["renderers"];
                    delete config_copy["derivedAttributes"];
                    //delete some bulky default values
                    delete config_copy["rendererOptions"];
                    delete config_copy["localeStrings"];
                    $("#Report_pivot_config").text(JSON.stringify(config_copy, undefined, 2));
                    $("#config_json").text(JSON.stringify(config_copy, undefined, 2));}}';
        if ($this->edit_mode) {
            $len = strlen($this->config);
            $this->config = substr_replace($this->config, $rifresCODE, $len - 1);

        }
        return $this->config;

    }

    /**
     * Run the widget
     */
    public function run()
    {
        // $this->podesiData(); // ucitava podatke na osnovu sqla
        $this->render($this->view,
            array(
                'data' => $this->data,
                'config' => $this->vratiConfig(),
            )
        );

    }

    /**
     * @return CActiveRecord
     */
    protected function getModel()
    {
        return CActiveRecord::model($this->modelName);
    }
}