<?php
    $this->PageTitle = "Níveis de Complexidade";

    $botoes = (User::canUpdate()) ? '{view} {update}' : '{view}';

    if (User::isAdmin()) {
        $botoes .= ' {status}';
    }

    $menu = array();

    if (User::canCreate()) {
        $menu[] = array(
            'label' => FaHtml::iconNovo(),
            'url' => array('create'),
            'linkOptions' => array(
                'target' => '_blank',
                'class' => 'novo'
            )
        );
    }

    $menu[] = array(
        'label' => FaHtml::iconSetaClose(),
        'url' => '#',
        'linkOptions' => array(
            'class' => 'search-button'
        )
    );
    $menu[] = array(
        'label' => FaHtml::iconRefresh(),
        'url' => 'javascript:void(0);',
        'linkOptions' => array(
            'onclick' => 'reloadFrame();',
            'class' => 'refresh-button'
        )
    );

    $this->menu = $menu;
?>

<div class="search-form" style="display: <?= (isset($_GET['busca'])) ? 'none' : 'block'; ?>">
    <?php $this->renderPartial('_search', array('model' => $model)); ?>
</div><!-- search-form -->

<?php
$this->widget('ext.Utils.widgets.CrmGridView', array(
    'id' => 'admin-grid',
    'dataProvider' => $dataProvider,
    'afterAjaxUpdate'=>'updated',
    'pager' => Util::getPagerDefaultConfig(),
    'columns' => array(
        'CPX_ID',
        'CPX_DESCRICAO',
        'CPX_HORAS_TOTAIS',
        array(
            'name'  => 'CPX_STATUS',
            'value' => '$data->getStatus()',
            'htmlOptions' => array(
                'class' => 'colunaStatus'
            )
        ),
        array(
            'class' => 'CButtonColumn',
            'template' => $botoes,
            'viewButtonOptions' => array('target'=>'_blank'),
            'updateButtonOptions' => array('target'=>'_blank'),
            'buttons'=>array(
                'view' => array(
                    'label' => 'Exibir',
                    'url' => 'Yii::app()->createUrl("complexidades/view", array("id"=>$data->CPX_ID))',
                ),
                'update' => array(
                    'label' => 'Editar',
                    'url' => 'Yii::app()->createUrl("complexidades/update", array("id"=>$data->CPX_ID))',
                ),
                'status' => array(
                    'label' => 'Alterar Status',
                    'imageUrl' => FaHtml::getImgUrl('ico-power.png'),
                    'url' => 'Yii::app()->createUrl("complexidades/status", array("id"=>$data->CPX_ID))',
                    'options' => array(
                        'class' => 'changeStatus'
                    ),
                ),
            ),
        ),
    ),
));

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

$script = <<<JAVASCRIPT
    $(document).on('click', '.changeStatus', function(event) {
        event.preventDefault();
        var href = $(this).attr('href');
        var tr = $(this).parents('tr');
        var colunaStatus = tr.find('.colunaStatus');

        if (confirm('Deseja alterar o status?')) {
            $.ajax({
                url: href,
                type: 'GET',
            })
            .done(function(status) {
                colunaStatus.html(status);
            })
            .fail(function(error) {
                alert(error.responseText);
            });
        }
        return false;
    });
JAVASCRIPT;

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