<?php

session_start();
$loja_id = $_SESSION['loja'];

header("Access-Control-Allow-Origin: *");
require_once("./conexao.php");
date_default_timezone_set('America/Bahia');
$conn = conn();
$data_hoje = date("Y-m-d");
$diaAnterior = gmdate("Y-m-d", time()-(3600*27));

if(empty($_POST)){
  $_POST = $_GET;
}

$cod = $_POST['cod'];

$hoje = date('Y-m-d');

$verify_loja = "";
if ($_SESSION['loja'] != 'todas') {
  $verify_loja = " AND PI.loja_id = $loja_id";
}
$SQL_geral = "SELECT PG.id_processo_iniciado, MAX(PG.id_processo) id_processo FROM processos_gerenciamento PG INNER JOIN processos_iniciados PI ON PI.id = PG.id_processo_iniciado WHERE PI.aposentadoria = 0 AND CAST(PI.data_cadastro as DATE) = '$hoje' $verify_loja GROUP BY PG.id_processo_iniciado ORDER BY PG.id_processo_iniciado DESC, PG.id_processo  DESC";
$query = mysqli_query($conn, $SQL_geral);
$solicitacoes = mysqli_fetch_all($query, MYSQLI_ASSOC);

if ($cod == 1) {
  $var = array(
    'total' => count($solicitacoes),
  );
}

if ($cod == 2) {
  $solicitacoes_filtered = array_filter($solicitacoes, function($v){
    return $v['id_processo'] == 7;
  });

  $var = array(
    'total' => count($solicitacoes_filtered),
  );
}

if ($cod == 3) {
  $solicitacoes_filtered = array_filter($solicitacoes, function($v){
    return $v['id_processo'] == 10;
  });

  $var = array(
    'total' => count($solicitacoes_filtered),
  );
}

if ($cod == 4) {
  $solicitacoes_filtered = array_filter($solicitacoes, function($v){
    return $v['id_processo'] == 9;
  });


  $var = array(
    'total' => count($solicitacoes_filtered),
  );
}

echo json_encode($var); 