<?php
extract(@$_POST);

if (isset($_POST['acao'])) { 
	if (@$busca) { 
		if (@$_POST['categoria']) {
			$where = " nome = '". $busca ."'"; 
			$categorias = Categoria::listar($where, null, 'nome');
			if ($categorias) {
			    foreach($categorias as $c) {        
			        $categoriaId = $c->id;
			        $categoria = $c->nome;
			        $exames = Exames::listar('cp_categoria.id = '.$categoriaId, null, 'cp_categoria.nome, cp_exames.nome');
			        $examesArr = null;
			        if ($exames) {
			            foreach($exames as $e) {
			                $examesArr[] = array("id" => $e->id,
			                					 "title" => $e->nome,
			                                     "description" => $e->descricao);         
			            }
			        }
			        $arr[] = array("title" => $categoria,
			                       "exames" => $examesArr);

			    }
			}
		} else {
			$where = ''; 
			$categorias = Categoria::listar($where, null, 'nome');
			if ($categorias) {
			    foreach($categorias as $c) {        
			        $categoriaId = $c->id;
			        $categoria = $c->nome;
			        $exames = Exames::listar('cp_categoria.id = '.$categoriaId .' AND ((cp_exames.nome LIKE "%'.$busca.'%") OR (cp_exames.descricao LIKE "%'.$busca.'%"))', null, 'cp_categoria.nome, cp_exames.nome');
			        $examesArr = null;
			        if ($exames) {
			            foreach($exames as $e) {
			                $examesArr[] = array("id" => $e->id,
			                					 "title" => $e->nome,
			                                     "description" => $e->descricao);         
			            }
			        }
			        if (!empty($examesAr)) { 
				        $arr[] = array("title" => $categoria,
				                       "exames" => $examesArr);
				    }

			    }
			}
		}
	} else {
		$arr = null;
	}
}


?>
<section class="exames">
    <div class="container">
        <h1 class="title">Consulte a lista de exames disponíveis.</h1>
        <form method="post">
        	<input type='hide' name="acao" value="buscar">
			<div class="row center">
				<div class="col-lg-4">
					<div class="buscar">
						<input type="text" class="buscar__input" placeholder="buscar exame" name="busca">
						<button class="buscar__btn"></button>
					</div>
				</div>
				<div class="col-lg-8">
					<div class="filtro">
						<h3 class="filtro__title">Buscar por:</h3>
						<div class="filtro__group">
							<span class="filtro__switch js-filtro-switch">
								<input type="hidden" name="categoria" value="false">
							</span>
							<p class="filtro__description">Categoria</p>
						</div>
						<div class="filtro__group">
							<span class="filtro__switch js-filtro-switch">
								<input type="hidden" name="exame" value="false">
							</span>
							<p class="filtro__description">Exame</p>
						</div>
					</div>
				</div>
			</div>
		</form>
        <div id="wrap-list">
            <div class="list">
            	<?php if (!empty($arr)) {
            		for($x=0; $x< count($arr); $x++){
            			$exames = $arr[$x]['exames'];
            	?>
                <h2 class="list__title"><?php echo $arr[$x]['title'];?></h2>
                <?php if (!empty($exames)) {
                	for($e=0; $e< count($exames); $e++){?>
                <ul>
                    <li class="list__toggle">
                        <a class="list__item" ><?php echo $exames[$e]['title'];?></a>
                        <p class="list__description"><?php echo $exames[$e]['description'];?></p>
                    </li>
                </ul>
                <?php }}}} ?>
            </div>
        </div>
    </div>
</section>
