<?php
   require $_SERVER["DOCUMENT_ROOT"].'/inc/conexao.php';
   require $_SERVER["DOCUMENT_ROOT"].'/inc/functions.php';

   // CONFERE PERMISSÃO
   $perms = $_SESSION["usuario"]["permissoes"];
   if($perms == ''){ $perms = array('13'); }

   $ret = array();
   $match = $mysqli->escape_string($_GET["registro"]);
   $xml = simplexml_load_file(str_replace('/cdn', '/live', APIPATH).'/aovivosbopcoes.php?jogo_id='.$match);

   // Configs do ao vivo
   $tempo_limite = (int)$cfg["config_aovivo_ate"];
   $desconto_aovivo = (int)$cfg["config_aovivo"];
   $tipo_desconto = '+';
   if($desconto_aovivo < 0){ $tipo_desconto = '-'; $desconto_aovivo = str_replace('-', '', $desconto_aovivo); }

   $ret['data']['data'] = (string)$xml->attributes()->data;
   $ret['data']['name_campeonato'] = (string)$xml->attributes()->name_campeonato;
   $ret['data']['name'] = (string)$xml->attributes()->name;
   $ret['data']['placar'] = explode('-', (string)$xml->attributes()->placar);
   $ret['data']['tempo'] = (string)$xml->attributes()->tempo;
   $ret['data']['atualizacao'] = (string)$xml->attributes()->atualizacao;
   $ret['data']['escanteios'] = explode('-', (string)$xml->attributes()->ICORNER);
   $ret['data']['amarelos'] = explode('-', (string)$xml->attributes()->YELLOW_CARD);
   $ret['data']['vermelhos'] = explode('-', (string)$xml->attributes()->RED_CARD);
   $ret['data']['substituicoes'] = explode('-', (string)$xml->attributes()->SUBSTITUITION);
   $ret['data']['goalkicks'] = '';
   $times = explode(' x ', $ret['data']['name']);
   $ret['data']['team1'] = $times[0];
   $ret['data']['team2'] = $times[1];

   if((int)$ret['data']['tempo'] > (int)$tempo_limite){
      $ret['markets']['JOGO INDISPONÍVEL'] = '';
      echo json_encode($ret); exit;
   }

   $mudancaOdds = array();
   foreach ($xml as $k => $bet) {
         if(!in_array('13', $perms) && (string)$bet->attributes()->name <> 'Vencedor do Encontro'){ continue; }
   		foreach ($bet as $k2 => $choice) {
            $i = count($mudancaOdds);
            $mudancaOdds[$i] = (string)$bet->attributes()->name.':'.(string)$choice->attributes()->name.':'.(string)$choice->attributes()->odd.':'.(string)$choice->attributes()->id;

            if(!isset($ret['markets'][(string)$bet->attributes()->name][(string)$choice->attributes()->name])){
               $ret['markets'][(string)$bet->attributes()->name][(string)$choice->attributes()->name]['id'] = (string)$choice->attributes()->id;
               $ret['markets'][(string)$bet->attributes()->name][(string)$choice->attributes()->name]['market'] = (string)$bet->attributes()->name;
               $ret['markets'][(string)$bet->attributes()->name][(string)$choice->attributes()->name]['name'] = (string)$choice->attributes()->name;
               $ret['markets'][(string)$bet->attributes()->name][(string)$choice->attributes()->name]['odd'] = alteraCotacao((string)$choice->attributes()->odd, $desconto_aovivo, $tipo_desconto);
               $ret['markets'][(string)$bet->attributes()->name][(string)$choice->attributes()->name]['locked'] = (string)$choice->attributes()->locked;
               $ret['markets'][(string)$bet->attributes()->name][(string)$choice->attributes()->name]['active'] = '';

               if($_SESSION["apostas"][$match]["aposta_token"] == (string)$choice->attributes()->id){
                  $ret['markets'][(string)$bet->attributes()->name][(string)$choice->attributes()->name]['active'] = 'bg-success';
               }
            }
   		}
   }

   echo json_encode($ret); exit;
?>