<?php
// app/api/transactions/currencies

    header("Content-Type:application/json");
    include("../dbconn.php");
    include("../utils.php");
    
    // parametros obligatorios
    $parmsob = array("sessionid");
    if (!parametrosValidos($_GET, $parmsob)){
        badEnd("400", array("msg"=>"Parametros obligatorios " . implode(", ", $parmsob)));
    }
    
    //Declaracion de variables
    $out = new stdClass();
    $sessionid = $_GET["sessionid"];
    $currencyid = "";
    
    //Validamos la session
    isSessionValid($db, $sessionid);
    
    // Si se envia el currencyid, se arma el query
    if(isset($_GET['currencyid']) && $_GET['currencyid'] != ''){
        // Si se envia una moneda base, buscaremos las cuentas por las monedas ref de los pares donde se encuentre
        
        // Consultaremos las monedas ref de los pares donde esta la moneda base
        $sql = "SELECT refcurrencyid, currencyid ".
        "       FROM pairs ".
        "       WHERE currencyid = ".$_GET['currencyid'].
        "       OR refcurrencyid =".$_GET['currencyid'];
        if (!$rs=$db->query($sql))
            badEnd("500", array("msg"=>$db->error.$sql));
            
        $count = 0;
        $currencyid .= " ";
        
        // Con las monedas recogidas armamos la condicion
        while($row = $rs->fetch_assoc()){
            
            if($count == 0){
                $currencyid .= " AND (pairs.currencyid = ".($_GET['currencyid'] == $row['currencyid'] ? $row['refcurrencyid'] : $row['currencyid']).
                "               OR pairs.refcurrencyid = ".($_GET['currencyid'] == $row['currencyid'] ? $row['refcurrencyid'] : $row['currencyid']).") ";
                $count++;
            }else{
                $currencyid .= " AND (pairs.currencyid = ".($_GET['currencyid'] == $row['currencyid'] ? $row['refcurrencyid'] : $row['currencyid']).
                "               OR pairs.refcurrencyid = ".($_GET['currencyid'] == $row['currencyid'] ? $row['refcurrencyid'] : $row['currencyid']).") ";
            }
            
        }
        
        $currencyid .= " ";
    }
    
    //Consultamos la cantidad de monedas bases que retornaremos
    $sql = "SELECT COUNT(DISTINCT currencies.id) AS qty ".
    "       FROM pairs, currencies ".   
    "       WHERE (pairs.currencyid = currencies.id ".
    "       OR pairs.refcurrencyid = currencies.id) ".
    "       ".$currencyid;
    if (!$rs=$db->query($sql))
        badEnd("500", array("msg"=>$db->error.$sql));
        
    $row = $rs->fetch_assoc();
    $out->numofrecords = (int)$row['qty'];
    //$out->Zprobando = $sql;
    
    // Consultamos los datos de las monedas base
    $sql = "SELECT DISTINCT currencies.id AS currencyid, ".
    "       currencies.name AS currencyname, ".
    "       currencies.symbol AS currencysymbol, ".
    "       currencies.icon AS currencyicon, ".
    "       currencies.decimals AS decimals, ".
    "       currencies.interface AS interface, ".
    "       currencies.status AS status ".
    "       FROM pairs, currencies ".
    "       WHERE (pairs.currencyid = currencies.id ".
    "       OR pairs.refcurrencyid = currencies.id) ".
    "       ".$currencyid;
    if (!$rs=$db->query($sql))
        badEnd("500", array("msg"=>$db->error.$sql));
        
    //Ordenamos los datos
    while($row = $rs->fetch_assoc()){
        $record = new stdClass();
        
        $record->id = (int)$row['currencyid'];
        $record->name = $row['currencyname'];
        $record->symbol = $row['currencysymbol'];
        $record->icon = $row['currencyicon'];
        $record->decimals = (int)$row['decimals'];
        $record->interface = (int)$row['interface'];
        
        // Estado de la moneda
        $record->status = new stdClass();
        $record->status->id = (int)$row['status'];
        $record->status->dsc = intval($row['status']) == 1 ? 'Activo' : 'Inactivo';
        
        $records [] = $record;
    }
    
    $out->records = $records;
    
    header("HTTP/1.1 200");
    echo (json_encode($out));
    die();
?>