<?php

//use GridEditable\GridEditable;

echo "<h2 class=\"titulo-scripts\">Ativos</h2>";
$grid = new GridEditable('gridEditable', ['gridEditable', 'table', 'table-bordered', 'table-hover']);

$grid->setHeaderValue("SCR_IDSCRIPT", 'ID', 'id', '5%');
$grid->setHeaderValue("SCR_SCRIPT", 'Script', 'textarea', '40%');
$grid->setHeaderValue("SCR_DATAHORA", 'Data Hora', 'datetime', '10%');
$grid->setHeaderValue("SCR_DATAHORA_ALTERACAO", 'Data Hora Alteração', 'datetime', '10%');
$grid->setHeaderValue("SCR_IDFUNCIONARIO", 'Usuário', 'text', '20%');
$grid->setHeaderValue("acoes", 'Ações', 'actions', '15%');

if (!empty($model->ScriptsAtivos)) {
    foreach ($model->ScriptsAtivos as $key => $script) {
        $grid->setCellValue("SCR_IDSCRIPT", $script->SCR_IDSCRIPT, "id", true, "5%", false, false); // Código
        $grid->setCellValue("SCR_SCRIPT", $script->SCR_SCRIPT, "textarea", false, "40%", false, false); // Script
        $grid->setCellValue("SCR_DATAHORA", $script->SCR_DATAHORA, "datetime", true, "10%", false, false); // Data
        $grid->setCellValue("SCR_DATAHORA_ALTERACAO", $script->SCR_DATAHORA_ALTERACAO, "datetime", true, "10%", false, false); // Data Alteração
        $grid->setCellValue("SCR_IDFUNCIONARIO", $script->Funcionario, "text", true, "20%", false, false); // Funcionário

        $acoes = array();
        $acoes[] = 'order';
        $acoes[] = 'delete';
        $grid->setCellValue("acoes", $acoes, "actions", true, "15%", false, true); // Ações
    }
}

$grid->setNewLineConfig("SCR_IDSCRIPT", GridEditable::AUTO_VALUE, "id", true, "5%", false, false);
$grid->setNewLineConfig("SCR_SCRIPT", false, "textarea", false, "40%", false, false);
$grid->setNewLineConfig("SCR_DATAHORA", GridEditable::AUTO_VALUE, "datetime", true, "10%", false, false);
$grid->setNewLineConfig("SCR_DATAHORA_ALTERACAO", GridEditable::AUTO_VALUE, "datetime", true, "10%", false, false);
$grid->setNewLineConfig("SCR_IDFUNCIONARIO", User::getUser()->FUN_NOME, "text", true, "20%", false, false);

$acoes = array();
$acoes[] = 'order';
$acoes[] = 'delete';
$grid->setNewLineConfig("acoes", $acoes, "actions", true, "15%", false, true);

echo $grid->buildTable();

echo "<h2 class=\"titulo-scripts\">Histórico de Alterações</h2>";
if (!empty($model->AlteracoesScripts)) {
    foreach ($model->AlteracoesScripts as $key => $alteracao) {
        $id = "toggle_" . Util::uid();

        $idTipo = constant("HistoricoAlteracaoScripts::TIPO_{$alteracao->HIS_TIPOALTERACAO}");
        $tipo = $alteracao->getTipo($idTipo);
        echo "<div class=\"historico-scripts\" id=\"historico{$alteracao->HIS_IDSCRIPT}\">";
            echo "<div class=\"toggleHistoricoAlteracoesScripts\">";
                echo "<a class=\"toggle_historico_alteracoes\" href=\"#\" data-toggle=\"{$id}\">";
                    echo "Script {$alteracao->HIS_IDSCRIPT} | ";
                    echo "Ordem Execução: {$alteracao->HIS_ORDEMEXECUCAO} | ";
                    echo "{$tipo} | ";
                    echo "{$alteracao->HIS_DATAHORA} | ";
                    echo "{$alteracao->Funcionario} <i class=\"fa fa-caret-down\"></i>";
                echo "</a>";
            echo "</div>";
            echo "<div class=\"{$id}\" style=\"display: none;\">";
                echo "<pre>";
                    echo $alteracao->HIS_SCRIPT;
                echo "</pre>";
            echo "</div>";
        echo "</div>";
    }
}
?>

<?php
$cs = Yii::app()->getClientScript();

$script = <<<JAVASCRIPT
    $(document).ready(function() {
        var saveUrl = createUrl('solicitacoes/saveScripts', {idPendencia: {$model->SOL_IDPENDENCIA}});
        var gridEditable = $("#gridEditable").gridEditable(saveUrl);

        gridEditable.on("afterSave", function(event) {
            var scripts = $(this).find('table > tbody').find('tr:not(".new"):not(".ignore")');
            $("#countScripts").html(scripts.length);
        });
    });
JAVASCRIPT;

if ($model->podeCadastrarAlterarScripts()) {
    $cs->registerScriptFile(FaHtml::getJsUrl('gridEditable.js'), CClientScript::POS_END);
    $cs->registerCssFile(FaHtml::getCssUrl('gridEditable.css'));

    $cs->registerScriptFile(FaHtml::getJsUrl('bootbox.min.js'), CClientScript::POS_END);

    $cs->registerScript(Util::uid(), $script);
}

$css = <<<CSS
    .titulo-scripts {
        margin: 0;
        font-size: 12px;
    }
    .historico-scripts {
        margin-bottom: 10px;
    }
    .toggleHistoricoAlteracoesScripts {
        margin-bottom: 5px;
    }
    .historico-scripts .toggle_historico_alteracoes i {
        top: 0;
        position: initial;
    }
CSS;
$cs->registerCss(Util::uid(), $css);
