<?php

    $this->PageTitle = 'Controle de Cobrança';

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

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

    $imagem = FaHtml::iconBlank();

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

    if (!isset($_GET['busca']) || empty($_GET['busca'])) {
        $model->ATD_DTCADASTRO_INICIAL = date('d/m/Y', strtotime('-3 days'));
        $model->ATD_DTCADASTRO_FINAL = date('d/m/Y', strtotime('+3 days'));
        $model->ATD_SITUACAO = "A";
        $model->ATD_COBRAR_ATENDIMENTO = "1";
        $model->ATD_STATUS_COBRANCA = "3";
    }

    if (User::getPrivilegio() == 6 && User::getSetor() == 5) {
        $model->ATD_IDFUNCIONARIO = User::getId();
    }
    if (isset($_GET['Atendimentos'])) {
        $model->setAttributes($_GET['Atendimentos']);
    }

    $model->BuscaVendedor = 1;
    $dataProvider = $model->cobranca();


?>
<div class="search-form" <?= $dataProvider->totalItemCount > 0 ? 'style="display: none;"' : ""; ?> >
    <?php $this->renderPartial('_search', array('model' => $model, 'cobranca_visita' => false)); ?>
</div><!-- search-form -->

<?php
$this->widget('ext.Utils.widgets.CrmGridView', array(
    'id' => 'admin-grid',
    'dataProvider' => $dataProvider,
    'afterAjaxUpdate'=>'updated',
    'pager' => Util::getPagerDefaultConfig(),
    'selectableRows' => 1,
    'columns' => [
        'ATD_DTCADASTRO',
        [
            'name' => 'ATD_IDCLIENTE',
            'value' => 'GxHtml::valueEx($data->Cliente)',
        ],
        [
            'name' => 'Autor',
            'header' => 'Autor',
            'value' => 'GxHtml::valueEx($data->Funcionario)',
        ],
        [
            'name' => 'ATD_OBS',
            'value' => 'substr($data->ATD_OBS, 0, 100)',
        ],
        [
            'name' => 'ATD_STATUS_COBRANCA',
            'value' => '$data->translateStatusCobranca($data->ATD_STATUS_COBRANCA)',
        ],
        [
            'class' => 'CButtonColumn',
            'template' => $botoes,
            'viewButtonOptions' => ['target' => '_blank'],
            'updateButtonOptions' => ['target' => '_blank'],
            'buttons' => [
                'view' => [
                    'label' => 'Exibir',
                    'url' => 'Yii::app()->createUrl("atendimentos/cobrancaView",
                                                    ["id"=>$data->ATD_IDATENDIMENTO, "retorno"=>"agenda"])',
                ],
                'update' => [
                    'label' => 'Editar',
                    'url' => 'Yii::app()->createUrl("atendimentos/cobrancaUpdate",
                                                    ["id"=>$data->ATD_IDATENDIMENTO])',
                    'visible' => '$data->ATD_STATUS_COBRANCA == 3',
                ],
                'delete' => [
                    'label' => 'Excluir',
                    'url' => 'Yii::app()->createUrl("atendimentos/cobrancaDelete",
                                                    ["id"=>$data->ATD_IDATENDIMENTO])',
                ],
            ],
        ],
    ],
));