<?php 
    e($box_atendimento); 
    
    $tabelas = [
        (object)['id_titulo' => 'novos','titulo_section' => 'Novos','atendimentos' => $atendimentosAguardando??[]],
        (object)['id_titulo' => 'retornos','titulo_section' => 'Retornos','atendimentos' => $atendimentosProximoContato??[]],
        (object)['id_titulo' => 'finalizados','titulo_section' => 'Finalizados','atendimentos' => $atendimentosFinalizados??[]],
    ]
?>

<div class="animated fadeIn">
    <div class="row">
        <div class="col-lg-12 my-5">
            <ul id="myTab" class="nav nav-tabs" role="tablist">
                <?php foreach($tabelas as $key=>$tabela){ ?>
                <li class="nav-item">
                    <a class="nav-link <?php e($key == 0?"active show":"") ?>" data-toggle="tab" href="#<?php e($tabela->id_titulo) ?>" role="tab" aria-controls="<?php e($tabela->titulo_section) ?>" aria-selected="<?php e($key == 0?"true":"false") ?>">
                        <?php e($tabela->titulo_section) ?>
                        <span class="badge badge-pill badge-danger"><?php e(count($tabela->atendimentos)) ?></span>
                    </a>
                </li>
                <?php } ?>
            </ul>
            
            <div class="tab-content">
                <?php foreach($tabelas as $key=>$tabela){ ?>
                <div class="tab-pane <?php e($key == 0?"active show":"") ?>" id="<?php e($tabela->id_titulo) ?>" role="tabpanel">
                    <?php if($tabela->id_titulo == 'novos'){ ?>
                    <div class="row">
                        <div class="col-md-12">
                            <div class="box-title-section">
                                <div class="form-inline">
                                    <div class="form-group">
                                        <label class="mr-1 text-value text-primary">Lista de confirmação</label>
                                    </div>
                                </div>
                                <hr>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <?php foreach($resumo as $card){ ?>
                        <div class="col-md-4">
                            
                            <div class="card card-accent-primary">
                                <div class="card-header"><?= $card->empreendimento->emp_razao_social ?><span class="badge badge-pill badge-primary float-right"><?= $card->countEmpreendimento ?></span></div>
                                <div class="card-body">
                                    <ul class="list-group list-group-flush">
                                        
                                    <?php foreach ($card->eventos as $key=>$evento){ ?>
                                        <li class="list-group-item px-0"><?=$key?> <span class="badge badge-pill badge-primary float-right"><?=count($evento)?></span></li>
                                    <?php } ?>  
                                    </ul>
                                </div>
                            </div>
                        </div>
                        <?php } ?>
                    </div>
                    <?php } ?>
                    
                    <div class="table-container table-responsive">
                        <table class="display table table-striped table-bordered" style="width:100%">
                            <thead>
                                <tr>
                                    <th>Nome</th>
                                    <th>E-mail</th>
                                    <th class="sorting_asc_custom">Data captura</th>
                                    <th>Empreendimento</th>
                                    <th>Evento</th>
                                    <?php if($tabela->id_titulo != 'novos'){ ?>
                                    <th style="min-width: 130px;">Último atendimento</th>
                                    <th style="min-width: 130px;">Proximo atendimento</th>
                                    <?php } ?>
                                    <th style="min-width: 70px;">Ação</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach($tabela->atendimentos as $row) { $id = $row->id_captura; ?>
                                
                                <tr id="divImovel_<?php e($id) ?>" class="trData" data-atendimento='<?php e(json_encode($row->atendimento)) ?>'>
                                    
                                    <td><?php e('<span class="d-none">'.$row->datetime_captura.'</span>'.$row->nome); ?></td>
                                    <td><?php e($row->email); ?></td>
                                    <?php 
                                        $nenv = '<span class="d-none">000000000</span>'.'<span class="badge badge-danger">Não enviado</span>'; 
                                        $denv = '<span class="d-none">'.$row->datetime_captura.'</span>'.(date("d/m/Y H:i:s", strtotime($row->datetime_captura)));
                                    ?>
                                    <td><?php e($row->datetime_captura == 0 ? $nenv:$denv); ?></td>
                                    <td><?php e($row->empreendimento->emp_razao_social); ?></td>
                                    <td><?php e($row->evento->uuid_evento??'Outros'); ?></td>
                                    
                                    <?php if($tabela->id_titulo != 'novos'){ ?>
                                        <td><p class="resumo-text m-0"><?php e(end($row->atendimento)->observacao??''); ?></p></td>
                                        <?php 
                                            $proximo_contato = end($row->atendimento)->proximo_contato??'';
                                            if($proximo_contato == 0 || $proximo_contato == '0000-00-00 00:00:00'){
                                                 $proximo_contato = '';
                                                 $alert = '<span class="d-none">z'.$proximo_contato.'</span>';
                                            }else{
                                                $alert = '<span class="d-none">a'.$proximo_contato.'</span>'.
                                                         '<span class="badge badge-danger">'.date("d/m/Y", strtotime($proximo_contato)).'</span>';
                                            }
                                            $normal = '<span class="d-none">b'.$proximo_contato.'</span>'.(date("d/m/Y", strtotime($proximo_contato)));
                                        ?>
                                        <td>
                                            <?php 
                                            $proximo_contato = end($row->atendimento)->proximo_contato??'';
                                            if($proximo_contato == 0 || $proximo_contato == '0000-00-00 00:00:00'){
                                                 $proximo_contato = '';
                                                 $alert = '<span class="d-none">z'.$proximo_contato.'</span>';
                                            }else{
                                                $alert = '<span class="d-none">a'.$proximo_contato.'</span>'.
                                                         '<span class="badge badge-danger">'.date("d/m/Y", strtotime($proximo_contato)).'</span>';
                                            }
                                            $normal = '<span class="d-none">b'.$proximo_contato.'</span>'.(date("d/m/Y", strtotime($proximo_contato)));
                                            
                                            e($proximo_contato <= date("Y-m-d H:i:s") ? $alert:$normal);
                                            ?>
                                        </td>
                                    <?php } ?>
                                    
                                    <td class="tdAction_<?php e($id) ?>" >
                                        <?php 
                                            foreach ($row->actions??[] as $action){
                                                e($action);
                                            } 
                                        ?>
                                        
                                        <button class="btn btn-sm btn-primary set_leed" data-id="<?php e($id); ?>" data-toggle="modal" data-target="#atendimentoRapidoModal" data-toggle="tooltip" data-original-title="Atendimento rápido">
                                            <i class="icon-plus"></i>
                                        </button>
                                        
                                        <button class="btn btn-sm btn-outline-success mr-1 addAtendimentoComdono set_leed" data-id="<?php e($id); ?>" data-toggle="tooltip" data-original-title="Atendimento completo">
                                            <i class="icon-check"></i>
                                        </button>
                                    </td>
                                </tr>
                                <?php } ?>
                            </tbody>
                        </table>
                    </div>
                </div>
                <?php } ?>
            </div>
        </div>
    </div>
</div>