<?php
    $this->PageTitle = 'Prospectos / Clientes';

    $botoes = '{atividade}';
    if (in_array(User::getPrivilegio(), [1, 6, 7, 16, 19, 20])) {
        $botoes .= " {solicitarSenha}";
    }

    if (in_array(User::getPrivilegio(), [1, 6, 7, 16, 19, 20])) {
        $botoes .= " {acessarCliente}";
    }

    // if (User::getId() == 481 || User::getId() == 461 || User::getId() == 17) {
    //     $botoes .= " {solicitarSenha} {acessarCliente}";
    // }

    $botoes .= " {view} {update}";
    if (User::isAdmin()) {
        $botoes .= ' {delete}';
    }

    $imagem = (User::canCreate()) ? FaHtml::iconNovo() : FaHtml::iconBlank();

    $this->menu = array(
        array('label' => $imagem, 'url' => array('create'), 'linkOptions' => array('target' => '_blank', 'class' => 'novo')),
        array('label' => FaHtml::iconSetaClose(), 'url' => '#', 'linkOptions' => array('class' => 'search-button')),
        array('label' => FaHtml::iconRefresh(), 'url' => 'javascript:void(0);', 'linkOptions' => array('onclick' => 'reloadFrame();', 'class' => 'refresh-button'))
    );

    function cssInativo($cliente, $situacao, $row) {
        $return = ($row%2 == 0?'even':'odd');
        if ($cliente->countTitulosPendentes() > 0 || $situacao == 'I') {
            $return .= ' row-red';
        }

        return $return;
    }

    $dataProvider = $model->search();
?>

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

<style type="text/css">
    /* ultima atividade */
    #admin-grid table th:nth-child(1),
    #admin-grid table td:nth-child(1){
        text-align: center;
        min-width: 60px!important;
        max-width: 60px!important;
            width: 60px!important;
    }
    /* telefone */
    #admin-grid table th:nth-child(3),
    #admin-grid table td:nth-child(3){
        text-align: center;
        min-width: 100px!important;
        max-width: 100px!important;
            width: 100px!important;
    }
    /* telefone */
    #admin-grid table th:nth-child(5),
    #admin-grid table td:nth-child(5){
        text-align: center;
        min-width: 150px!important;
        max-width: 150px!important;
            width: 150px!important;
    }
    /* telefone */
    #admin-grid table th:nth-child(6),
    #admin-grid table td:nth-child(6){
        text-align: center;
        min-width: 120px !important;
        max-width: 140px !important;
            width: 140px!important;
            text-align: center;
            padding:  5px;
            margin-top: 0px;
            overflow: hidden;
    }
</style>
<?php
if (isset($_GET['busca'])) {
    $this->widget('ext.Utils.widgets.CrmGridView', array(
        'id' => 'admin-grid',
        'dataProvider' => $dataProvider,
        'afterAjaxUpdate'=>'updated',
        'selectableRows' => 1,
        'pager' => Util::getPagerDefaultConfig(),
        'rowCssClassExpression' => 'cssInativo($data, $data->CLI_SITUACAO, $row)',
        'columns' => array(
            array(
                'name' => 'CLI_DT_ULTIMA_ATIVIDADE_COMERCIAL',
                'value' => '$data->CLI_DT_ULTIMA_ATIVIDADE_COMERCIAL',
            ),
            array(
                'name' => 'CLI_NOME',
                'value' => 'Util::getNomeImplantacao($data->CLI_NOME, $data->CLI_IMPLANTACAO) . Util::getPendencias($data, true). Util::getInfoCliente($data)',
                'type' => 'raw'
            ),
            'CLI_FONE1',
            array(
                'name' => 'CLI_CIDADE',
                'value' => 'GxHtml::valueEx($data->Cidade)',
            ),
            array(
                'name' => 'CLI_SITUACAO',
                'value' => 'Util::getStatusCliente($data->CLI_SITUACAO)',
            ),
            array(
                'class' => 'CButtonColumn',
                'template' => $botoes,
                'viewButtonOptions' => array('target' => '_blank'),
                'updateButtonOptions' => array('target' => '_blank'),
                'buttons' => array(
                    'solicitarSenha' => array(
                        'label' => 'Solicitar senha da área dos clientes',
                        'imageUrl' => FaHtml::getImgUrl('ico-trocar-senha.png'),
                        'url' => 'Yii::app()->createUrl("clientes/SolicitarSenha", array("id"=>$data->CLI_IDCLIENTE))',
                        'options' => array('target' => '_blank'),
                        'visible' => '$data->temAcessoAreaClientes()'
                    ),
                    'acessarCliente' => array(
                        'label' => 'Acessar o ambiente do cliente',
                        'imageUrl' => FaHtml::getImgUrl('ico-acesso.png'),
                        'url' => 'Yii::app()->createUrl("clientes/acessarCliente", array("id"=>$data->CLI_IDCLIENTE))',
                        'options' => array('target' => '_blank'),
                        'visible' => '$data->temAcessoAreaClientes()'
                    ),
                    'atividade' => array(
                        'label' => 'Inserir atividade',
                        'imageUrl' => FaHtml::getImgUrl('ico-solciitacoes.png'),
                        'url' => 'Yii::app()->createUrl("atendimentos/create", array("CLI_IDCLIENTE"=>$data->CLI_IDCLIENTE))',
                        'options' => array('target' => '_blank'),
                    ),
                    'view' => array(
                        'label' => 'Exibir',
                        'url' => 'Yii::app()->createUrl("clientes/view", array("id"=>$data->CLI_IDCLIENTE))',
                    ),
                    'update' => array(
                        'label' => 'Editar',
                        'url' => 'Yii::app()->createUrl("clientes/update", array("id"=>$data->CLI_IDCLIENTE))',
                        'visible' => '(Yii::app()->user->permissao == "1" or Yii::app()->user->permissao == "7") or ($data->CLI_SITUACAO != "A" and $data->CLI_SITUACAO != "I" and $data->CLI_SITUACAO != "C") or (Yii::app()->user->permissao == 11)',
                    ),
                    'delete' => array(
                        'label' => 'Excluir',
                        'url' => 'Yii::app()->createUrl("clientes/delete", array("id"=>$data->CLI_IDCLIENTE))',
                    ),
                ),
            ),
        ),
    ));
}
?>