<?php
$database = (config('SQL_USE_2_DB',true) ? $dB2 : $dB);

# Obtener Datos Tabla
$GiftCode   =   $database->query_fetch("SELECT * From WEBENGINE_GIFT_CODE ORDER BY id DESC");

if(check_value($_GET['id'])) {
    try {
        if(!Validator::UnsignedNumber($_GET['id'])) throw new Exception("ID Invalido.");
        
        $DeletedCode = $database->query_fetch_single("SELECT * FROM WEBENGINE_GIFT_CODE WHERE id = ?", array($_GET['id']));
        if(!is_array($DeletedCode)) throw new Exception("<b>[ GIFT Code ]</b> El ID no existe.");
    
        $database->query("DELETE FROM WEBENGINE_GIFT_CODE WHERE id = ?", array($DeletedCode['id']));
        
        message('success', '<b>[ GIFT Code ]</b> Codigo Eliminado correctamente.');
    } catch(Exception $ex) {
        message('error', $ex->getMessage());
    }
}

echo '<br><h1>[GIFT Code] Codigos Generados</h1>';

echo '<div class="row">';
	echo '<div class="col-md-12">';
    echo '<div class="table-responsive">';
            echo '<table id="zero_config" class="table table-striped table-sm align-middle">';
            echo '<thead class="table-dark text-white text-center">';
            echo '<tr>';
                echo '<th></th>';
                echo '<th>Tipo de Codigo</th>';
                echo '<th>Codigo</th>';
                echo '<th>Usos Maximos</th>';
                echo '<th>Usados</th>';
                echo '<th>Puntos Level</th>';
                echo '<th>Niveles</th>';
                echo '<th>Resets</th>';
                echo '<th>M/G Resets</th>';
                echo '<th>Tipo Vip</th>';
                echo '<th>Dias de Vip</th>';
                echo '<th>Tipo Coins</th>';
                echo '<th>Cantidad Coins</th>';
                echo '<th>Usuario</th>';
                echo '<th>Personaje</th>';
                echo '<th>Da Box</th>';
                echo '<th>Tipo de Entrega</th>';
                echo '<th>Grupo Item</th>';
                echo '<th>Index Item</th>';
                echo '<th>Level Item</th>';
                echo '<th>Item Name</th>';
            echo '</tr>';
            echo '</thead>';
			echo '<tbody>';    
            if(is_array($GiftCode)){
                foreach($GiftCode as $Codes)
                {
                    if($Codes['VipType'] == 0) $TipoVip = '<b style="color:#ff0000;">Sin Vip</b>';
                    else if($Codes['VipType'] == 1) $TipoVip = '<b style="color:#ff8130;">Bronce</b>';
                    else if($Codes['VipType'] == 2) $TipoVip = '<b style="color:#424242;">Plata</b>';
                    else if($Codes['VipType'] == 3) $TipoVip = '<b style="color:#ff9300;">Oro</b>';

                    if($Codes['TypeCode'] == 1) $TipeCode = '<b style="color:red;">Seteo</b>';
                    else $TipeCode = '<b style="color:green;">Agregado</b>';

                    if($Codes['TypeWare'] == 1) $TipeWare = '<b style="color:red;">Gremory Case</b>';
                    else $TipeWare = '<b style="color:green;">Warehouse</b>';

                    if($Codes['DarItem'] == 1) $ActItem = '<b style="color:green;">Activado</b>';
                    else $ActItem = '<b style="color:red;">Desactivado</b>';

                    if($Codes['Usuario'] == '0') $UsuarioAll = '<b style="color:green;">General</b>';
                    else $UsuarioAll = $Codes['Usuario'];

                    if($Codes['Personaje'] == '0') $PersonajeAll = '<b style="color:green;">General</b>';
                    else $PersonajeAll = $Codes['Personaje'];
                    
                    echo '<tr>';
                        echo '<td><a href="index.php?module='.$_REQUEST['module'].'&config=giftcode&id='.$Codes['id'].'" class="btn btn-danger btn-xs">X</a></td>';
                        echo '<td>'.$TipeCode.'</td>';
                        echo '<td><b>'.$Codes['Codigo'].'</b></td>';
                        echo '<td>'.$Codes['UsedMax'].'</td>';
                        echo '<td>'.$Codes['UsedActual'].'</td>';
                        echo '<td><b>'.number_format($Codes['pLevel'],0,',','.').'</b></td>';
                        echo '<td><b>'.number_format($Codes['Niveles'],0,',','.').'</b></td>';
                        echo '<td><b>'.number_format($Codes['Resets'],0,',','.').'</b></td>';
                        echo '<td><b>'.number_format($Codes['MResets'],0,',','.').'</b></td>';
                        echo '<td>'.$TipoVip.'</td>';
                        echo '<td><b>'.$Codes['VipDays'].'</b></td>';
                        echo '<td><b style="color:#006cff;">'.$Codes['TypeCoin'].'</b></td>';
                        echo '<td><b>'.number_format($Codes['Coins'],0,',','.').'</b></td>';
                        echo '<td>'.$UsuarioAll.'</td>';
                        echo '<td>'.$PersonajeAll.'</td>';
                        echo '<td>'.$ActItem.'</td>';
                        echo '<td>'.$TipeWare.'</td>';
                        echo '<td>'.$Codes['iGroup'].'</td>';
                        echo '<td>'.$Codes['iIndex'].'</td>';
                        echo '<td>'.$Codes['iLevel'].'</td>';
                        echo '<td>'.$Codes['iName'].'</td>';
                    echo '</tr>';
                }     
            } 
            echo '</tbody>';  
            echo '</table>';
            echo '</div>';
	echo '</div>';
echo '</div>';
