<style type="text/css">
    .title strong {font-size: 17px;}
    .summary {float: right;}
    label {margin-top: 0;}
    summary {position: fixed;z-index: 1031;left: 0;bottom: 0;padding: 10px;}
    #page .container #content {margin-top: 0!important;}
    body {padding: 0 !important;}
    .btn i {margin-right: 0 !important;}
    select, input[type="file"], .select2-container, .chosen-container, .ui-multiselect,input {max-width: 90%!important;}
    input {height: 20px!important;}
    .grid {padding: 10px;background: white;}
    .buttons button {margin-top: 10px;}
    form {margin-bottom: 0!important;}
    h1 {margin-bottom: 0!important;padding-bottom: 0!important;}
    td span {display: block;float: left;min-width: 50px;min-height: 29px;}
    td a  { color: #005580; }
    .pager {
        color: transparent;
    }
</style>

<div class="grid">
    <?php
    $this->pageTitle = false;
    $form = $this->beginWidget('GxActiveForm', array('action' => Yii::app()->createUrl($this->route),'method' => 'get'));
    $this->widget('application.extensions.EChosen.EChosen', array('target' => '.echosen','useJQuery' => true));
    ?>
    <p class="title">
    <strong>Auditoria Documentos</strong>
    <span class="summary"><?php  if(($count=$dataProvider->getItemCount())>0) {

        $pagination=$dataProvider->getPagination();
        $total=$dataProvider->getTotalItemCount();
        $start=$pagination->currentPage*$pagination->pageSize+1;
        $end=$start+$count-1;
        if($end>$total)
        {
            $end=$total;
            $start=$end-$count+1;
        }
        $summaryText=Yii::t('zii','Displaying {start}-{end} of 1 result.|Displaying {start}-{end} of {count} results.',$total);
        echo strtr($summaryText,array(
            '{start}'=>$start,
            '{end}'=>$end,
            '{count}'=>$total,
            '{page}'=>$pagination->currentPage+1,
            '{pages}'=>$pagination->pageCount,
        ));
        }
        ?></span>
    </p>

    <div class="col-md-3">
        <?php echo $form->label($model, 'situacao_revisao'); ?>
        <?php echo $form->dropDownList($model, 'situacao_revisao', [Clientes::STATUS_DOCUMENTOS_REVISAO_PENDENTE => 'Pendentes de revisão', Clientes::STATUS_DOCUMENTOS_REVISAO_REVISADO => 'Revisado', Clientes::STATUS_DOCUMENTOS_REVISAO_CLIENTE_RECENTE => 'Cliente recente'], array('multiple' => 'multiple', 'class' => 'echosen', 'data-placeholder' => 'Todos')); ?>
    </div>

    <div class="col-md-3">
        <?php echo $form->label($model, 'CLI_NOME'); ?>
        <?php echo $form->textField($model, 'CLI_NOME'); ?>
    </div>

    <div class="col-md-3">
        <?php echo $form->label($model, 'CLI_IDCLIENTE'); ?>
        <?php echo $form->textField($model, 'CLI_IDCLIENTE'); ?>
    </div>
    <div class="col-xs-3 buttons">
        <?php echo FaHtml::searchButton(); ?>
    </div>
    <div class="clearfix"></div>

    <?php $this->endWidget(); ?>

    <table class="table table-hover table-responsive">
        <thead class="tableFloatingHeaderOriginal">
            <tr>
                <th>Código CRM</th>
                <th>Razão Social</th>
                <th>Contrato</th>
                <th>Data de Assinatura</th>
                <th>Data de Ativação</th>
                <th>Início da Carência</th>
                <th>Fim da Carência</th>
                <th>Início da Cobrança</th>
                <th>Primeira Mensalidade</th>
                <th>Valor Contrato</th>
                <th>Valor Atual</th>
                <th>Adendos</th>
                <th>Pedidos Esperados</th>
                <th>Pedidos Encontrados</th>
                <th>Status</th>
                <th>Ações</th>
            </tr>
        </thead>
        <tbody>
        <?php
        if ($dataProvider->getItemCount() <= 0) {
            echo "<p>Nenhum dado encontrado!</p>";
        } else {
        foreach ($dataProvider->getData() as $cliente) {
            $contrato = Contratos::model()->find("
                COT_IDCLIENTE = {$cliente->CLI_IDCLIENTE} AND
                COT_STATUS = 2 AND
                COT_SITUACAO = " . Contratos::CONTRATO . "
            ");
            $contratoCancelado = Contratos::model()->count("
                COT_IDCLIENTE = {$cliente->CLI_IDCLIENTE} AND
                COT_STATUS = 16 AND
                COT_SITUACAO = " . Contratos::CONTRATO . "
            ");
            $atividades = !$contrato ? 0 : Atendimentos::model()->count("
                ATD_IDCLIENTE = {$cliente->CLI_IDCLIENTE} AND
                ATD_IDCONTRATO = {$contrato->COT_IDCONTRATO}
            ");
            $contratoCondition = $contrato ? " ADD_IDCONTRATO = {$contrato->COT_IDCONTRATO}" : " ADD_IDCONTRATO is null";
            $adendos = Contratos::model()->count("
                COT_IDCLIENTE = {$cliente->CLI_IDCLIENTE} AND
                COT_STATUS in (2, 19) AND
                COT_SITUACAO = " . Contratos::ADENDO . "
            ");
            $pedidosEsperados = Contratos::model()->count("
                COT_IDCLIENTE = {$cliente->CLI_IDCLIENTE} AND
                COT_STATUS = 2 AND
                COT_SITUACAO in (" . Contratos::ADENDO . ", " . Contratos::CONTRATO . ") AND
                COT_VLRIMPLANTACAO > 0
            ");
            $pedidosEncontrados = Contratos::model()->count("
                COT_IDCLIENTE = {$cliente->CLI_IDCLIENTE} AND
                COT_STATUS = 2 AND
                COT_SITUACAO = " . Contratos::PEDIDO_IMPLANTACAO . "
            ");
            ?>
            <tr>
                <td><?= $cliente->CLI_IDCLIENTE; ?></td>
                <td>
                    <a href="<?=Yii::app()->createUrl('clientes/view', array('id' => $cliente->CLI_IDCLIENTE)) ?>" target="_blank">
                        <?= $cliente->CLI_NOME; ?>
                    </a>
                </td>
                <?php if($contrato) { ?>
                    <td>
                        <a href="<?=Yii::app()->createUrl('contratos/view', array('id' => $cliente->CLI_IDCLIENTE)) ?>" target="_blank">
                            <?= $contrato->COT_IDCONTRATO;?>
                        </a>
                    </td>
                    <td><?= $contrato->COT_DTCADASTRO;?></td>
                    <td><?= $contrato->COT_DTATIVACAO;?></td>
                    <td><?= $contrato->COT_DTINICARENCIA;?></td>
                    <td><?= $contrato->COT_DTFIMCARENCIA;?></td>
                    <td><?= $contrato->COT_DTINICOBRANCA;?></td>
                    <td><?= $contrato->COT_DTPRICOBRANCA;?></td>
                    <td><?= $contrato->COT_VLRMENSAL;?></td>
                <?php } else if ($contratoCancelado > 0) { ?>
                    <td>
                        <a href="<?=Yii::app()->createUrl('clientes/view', array('id' => $cliente->CLI_IDCLIENTE, 'page' => 'documentos')) ?>" target="_blank">
                        Contrato inativo
                        </a>
                    </td>
                    <td>Contrato inativo</td>
                    <td>Contrato inativo</td>
                    <td>Contrato inativo</td>
                    <td>Contrato inativo</td>
                    <td>Contrato inativo</td>
                    <td>Contrato inativo</td>
                    <td>Contrato inativo</td>
                <?php } else { ?>
                    <td>Sem contrato</td>
                    <td>Sem contrato</td>
                    <td>Sem contrato</td>
                    <td>Sem contrato</td>
                    <td>Sem contrato</td>
                    <td>Sem contrato</td>
                    <td>Sem contrato</td>
                    <td>Sem contrato</td>
                <?php } ?>
                <td><?= $cliente->CLI_VLRMENSAL; ?></td>
                <td><?= $adendos; ?></td>
                <td><?= $pedidosEsperados; ?></td>
                <td><?= $pedidosEncontrados; ?></td>
                <td><?= Clientes::$tipo_registro_revisao[(int)$cliente->CLI_STATUS_REVISAO] ?></td>
                <td>
                    <a href="<?=Yii::app()->createUrl('auditoriaDados/resolver', array('id' => $cliente->CLI_IDCLIENTE)) ?>" class="btn btn-success" target="_blank">
                        <i class="fa fa-edit"></i>
                    </a>
                </td>
            </tr>
        <?php 
            }
        } ?>
        </tbody>
    </table>
    <?php
        $pager = [
            'pages' => $dataProvider->getPagination(),
            'prevPageLabel' => 'Anterior',
            'nextPageLabel' => 'Próxima',
            'lastPageLabel' => 'Última',
            'firstPageLabel' => 'Primeira',
            'htmlOptions'   => array(
                'class' => 'navbar navbar-inverse navbar-fixed-bottom yiiPager text-right'
            )
        ];

        if($pager['pages']->getPageCount() > 1) {
            echo '<div class="pager">';
            $this->widget('CLinkPager',$pager);
            echo '</div>';
        } else {
            $this->widget('CLinkPager',$pager);
        }
    ?>
    <summary>
        <?php
            if(($count=$dataProvider->getItemCount())>0) {

            $pagination=$dataProvider->getPagination();
            $total=$dataProvider->getTotalItemCount();
            $start=$pagination->currentPage*$pagination->pageSize+1;
            $end=$start+$count-1;
            if($end>$total)
            {
                $end=$total;
                $start=$end-$count+1;
            }
            $summaryText=Yii::t('zii','Displaying {start}-{end} of 1 result.|Displaying {start}-{end} of {count} results.',$total);
            echo strtr($summaryText,array(
                '{start}'=>$start,
                '{end}'=>$end,
                '{count}'=>$total,
                '{page}'=>$pagination->currentPage+1,
                '{pages}'=>$pagination->pageCount,
            ));
            }
            ?>
    </summary>
</div>