<?php
$this->PageTitle = 'Atendimentos / Visitas';

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

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

if (User::canCreate()) {
    $imagem = FaHtml::iconBlank();
} else {
    $imagem = FaHtml::iconBlank();
}

$this->menu = array(
    array('label' => $imagem, 'url' => array('screate')),
    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'))
);
?>

<?php
if (isset($_GET['busca'])) {
    $Motivos = Motivos::model()->findAll("MOT_TIPO = 1 and MOT_SITUACAO='A'");
    foreach ($Motivos as $Motivos) {
        $Motivo[] = $Motivos->MOT_IDMOTIVO;
    }
    $model->Motivos_filtro = $Motivo;
    if (User::isRevenda() && empty($model->ATD_IDCLIENTE)) {
        $model->ATD_IDCLIENTE = GxHtml::listData(Clientes::model()->findAll('CLI_CIDADE IN (' . User::getCidades(true) . ')'), 'CLI_IDCLIENTE', 'CLI_IDCLIENTE');
    }
    $dataProvider = $model->search();
?>
<div class="search-form" <?php if($dataProvider->totalItemCount > 0) echo 'style="display: none;"'; ?> >
    <?php
    $this->renderPartial('_ssearch', array(
        'model' => $model,
    ));
    ?>
</div><!-- search-form -->
<?php
    $this->widget('ext.Utils.widgets.CrmGridView', array(
        'id' => 'admin-grid',
        'dataProvider' => $dataProvider,
        'afterAjaxUpdate'=>'updated',
        'selectableRows' => 1,
        'pager' => Util::getPagerDefaultConfig(),
        'columns' => array(
            'ATD_DTCADASTRO',
            array(
                'name' => 'ATD_IDCLIENTE',
                'value' => 'GxHtml::valueEx($data->Cliente)'
            ),
            array(
                'name' => 'ATD_ATENDENTE',
                'value' => 'GxHtml::valueEx($data->Atendente)'
            ),
            array(
                'name' => 'ATD_TIPO',
                'value' => 'Util::getTipoAtendimento(explode(",", $data->ATD_TIPO))'
            ),
            'ATD_HORAINICIO',
            array(
                'class' => 'CButtonColumn',
                'template' => $botoes,
                'buttons' => array
                    (
                    'view' => array
                        (
                        'label' => 'Exibir',
                        'url' => 'Yii::app()->createUrl("atendimentos/sview", array("id"=>$data->ATD_IDATENDIMENTO))',
                    ),
                    'update' => array
                        (
                        'label' => 'Editar',
                        'url' => 'Yii::app()->createUrl("atendimentos/supdate", array("id"=>$data->ATD_IDATENDIMENTO))',
                    ),
                    'delete' => array
                        (
                        'label' => 'Excluir',
                        'url' => 'Yii::app()->createUrl("atendimentos/sdelete", array("id"=>$data->ATD_IDATENDIMENTO))',
                    ),
                ),
            ),
        ),
    ));
}