<?php
    // ini_set('display_errors',1);
    // ini_set('display_startup_erros',1);
    // error_reporting(E_ALL);
    require '../../../inc/conexao.php';
    require '../../../inc/functions.php';
    unset($_SESSION["apostas"]); // Reseta apostas
    require '../../../api/api.php';
    $r = api('/getPin', array('code' => $_GET["code"]));
    $r = json_decode($r, 1);



    foreach ($r['bets'] as $ret) {
      ########### SESSION ###########
      $match_id = $ret['match_id'];
      $_SESSION["apostas"][$match_id]["aposta_esporte"] = $ret['bet_code'];
      $_SESSION["apostas"][$match_id]["aposta_campeonato"] = $ret['event_name'];
      $_SESSION["apostas"][$match_id]["aposta_match_id"] = $match_id;
      $_SESSION["apostas"][$match_id]["aposta_jogo_nome"] = $ret['match_name'];
      $_SESSION["apostas"][$match_id]["aposta_data_jogo"] = $ret['match_date'];
      $_SESSION["apostas"][$match_id]["codigo_aposta"] = $ret['bet_code'];
      $_SESSION["apostas"][$match_id]["aposta_token"] = $ret['option_id'];
      $_SESSION["apostas"][$match_id]["aposta_nome_aposta"] = $ret['market'];
      $_SESSION["apostas"][$match_id]["aposta_opcao"] = $ret['option'];
      $_SESSION["apostas"][$match_id]["aposta_cotacao"] = $ret['odd'];
      ########### SESSION ###########
    }
    $_SESSION["pin"] = $ret['code'];
?>
<div class="page">
  <div class="navbar">
    <div class="navbar-inner sliding">
      <div class="left">
        <a href="#" class="link back">
          <i class="icon icon-back"></i>
          <span class="ios-only">Voltar</span>
        </a>
      </div>
      <div class="title">PIN | #<?php echo $_GET["code"]; ?></div>
      <div class="right">
        <a class="link icon-only">
        </a>
      </div>
    </div>
  </div>
  <div class="page-content">
    <form id="finishBetForm" action="/api/finishBet">
      <div class="list inline-labels no-hairlines-md">
         <ul id="">
            <li class="item-content item-input">
               <div class="item-inner">
                  <div class="item-title item-label">Cliente</div>
                  <div class="item-input-wrap">
                     <input type="text" name="cliente" placeholder="Ex: João" class="" value="<?php echo $r['client']; ?>">
                     <span class="input-clear-button"></span>
                  </div>
               </div>
            </li>
            <li class="item-content item-input">
               <div class="item-inner">
                  <div class="item-title item-label">Valor</div>
                  <div class="item-input-wrap">
                     <input type="tel" placeholder="Ex: 10,00" value="<?php echo $r['price']; ?>" name="valor" id="priceBet" onfocusin="adjustPrice(this);" onfocusout="maskMoney(this); calcReturn(this.value);" onkeyup="calcReturn(this.value);">
                     <span class="input-clear-button"></span>
                  </div>
               </div>
            </li>
            <li class="item-content item-input">
               <div class="item-inner">
                  <div class="item-title item-label">Poss. Retorno</div>
                  <div class="item-input-wrap">
                     <input type="text" disabled="disabled" name="return" id="totalReturns" value="<?php echo $r['return']; ?>">
                     <span class="input-clear-button"></span>
                  </div>
               </div>
            </li>
         </ul>
      </div>
    </form>
    <div class="list">
      <?php if(isset($r["error"])){ echo '<button class="col button button-fill color-red">'.$r["error"].'</button>'; } ?>
      <ul id="couponContent">
        <?php
          $cotacao = 0;
          foreach ($_SESSION["apostas"] as $k => $v) {
            $teams = explode(' x ', $v["aposta_jogo_nome"]);
            if($cotacao == 0){ $cotacao = $v["aposta_cotacao"]; } else { $cotacao *= $v["aposta_cotacao"]; }
        ?>
        <li class="option<?php echo $v['aposta_token']; ?>" id="item<?php echo $k; ?>">
          <a class="item-link item-content">
            <div class="item-inner item-inner-status">
              <div class="item-title">
                <div class="item-header"><?php echo $teams[0]; ?></div>
                <div class="item-header"><?php echo $teams[1]; ?></div>
                <div class="match_date"><?php echo desfazDatetime($v["aposta_data_jogo"]); ?> <br /><?php echo $v["aposta_nome_aposta"]; ?> <b>(<?php echo $v["aposta_opcao"]; ?>)</b></div>
              </div>
              <div class="item-after"><?php echo $v["aposta_cotacao"]; ?> &nbsp; <div class="chip chip-status color-red" onClick="clearOption(<?php echo $k; ?>);" data-match="<?php echo $k; ?>"> <div class="chip-label"><i class="material-icons">close</i></div> </div></div>
            </div>
          </a>
        </li>
        <?php } ?>
      </ul>
    </div><br /><br />
  </div>
  <div class="fab fab-extended color-red fab-left-bottom">
    <a onClick="clearOption('all');" data-match="all">
      <i class="icon material-icons md-only">delete</i>
      <div class="fab-text">Limpar</div>
    </a>
  </div>
  <div class="fab fab-extended color-green fab-right-bottom">
    <a href="#" onClick="finishBet();">
      <i class="icon material-icons md-only">check</i>
      <div class="fab-text">Finalizar</div>
    </a>
  </div>
</div>