<h1 class="page-header">[GIFT Code] Configuracion</h1>

<?php
$database = (config('SQL_USE_2_DB',true) ? $dB2 : $dB);
function GetGiftCode($longitud) {
    $Code = '';
    $pattern = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $max = strlen($pattern)-1;
    for($i = 0; $i < $longitud; $i++){
        $Code .= $pattern[ mt_rand(0,$max) ];
    } 
    return $Code;
}
$Codigo = '';
$Codigo = GetGiftCode(15);

# Obtener Datos Tabla
$GiftCode   =   $database->query_fetch("SELECT * From WEBENGINE_GIFT_CODE ORDER BY id DESC");
$CodeUse    =   $database->query_fetch_single("SELECT Codigo From WEBENGINE_GIFT_CODE Where Codigo='".$_POST['gift_code']."'");
$UserUsed   =   $database->query_fetch_single("SELECT Usuario From WEBENGINE_GIFT_CODE Where Usuario='".$_POST['gift_user']."'");
$CharUsed   =   $database->query_fetch_single("SELECT Personaje From WEBENGINE_GIFT_CODE Where Personaje='".$_POST['gift_character']."'");

if(check_value($_POST['submit_codigo'])) {
    try {
        # Verificaciones
        if(!check_value($_POST['gift_tipeset'])) throw new Exception("<b>[ GIFT Code ]</b> Tipo de Code Incorrecto.");
        if(!check_value($_POST['gift_code'])) throw new Exception("<b>[ GIFT Code ]</b> Codigo Incorrecto.");
        if(!check_value($_POST['gift_usedmax'])) throw new Exception("<b>[ GIFT Code ]</b> Maximos Usos Incorrecto.");
        if(!check_value($_POST['gift_plvl'])) throw new Exception("<b>[ GIFT Code ]</b> Puntos por Level Incorrecto.");
        if(!check_value($_POST['gift_niveles'])) throw new Exception("<b>[ GIFT Code ]</b> Niveles Incorrecto.");
        if(!check_value($_POST['gift_resets'])) throw new Exception("<b>[ GIFT Code ]</b> Resets Incorrecto.");
        if(!check_value($_POST['gift_mresets'])) throw new Exception("<b>[ GIFT Code ]</b> Master/Grand Resets Incorrecto.");
        if(!check_value($_POST['gift_tvip'])) throw new Exception("<b>[ GIFT Code ]</b> Tipo de Vip Incorrecto.");
        if(!check_value($_POST['gift_dvip'])) throw new Exception("<b>[ GIFT Code ]</b> Dias de Vip Incorrecto.");
        if(!check_value($_POST['gift_tmoneda'])) throw new Exception("<b>[ GIFT Code ]</b> Nombre de Coin Incorrecto.");
        if(!check_value($_POST['gift_coins'])) throw new Exception("<b>[ GIFT Code ]</b> Cantidad de Coins Incorrecto.");
        if(!check_value($_POST['gift_user'])) throw new Exception("<b>[ GIFT Code ]</b> Usuario Incorrecto.");
        if(!check_value($_POST['gift_character'])) throw new Exception("<b>[ GIFT Code ]</b> Personaje Incorrecto.");
        if(!check_value($_POST['gift_item'])) throw new Exception("<b>[ GIFT Code ]</b> Activar/Desactivar Item Incorrecto.");
        if(!check_value($_POST['gift_igroup'])) throw new Exception("<b>[ GIFT Code ]</b> Grupo de Item Incorrecto.");
        if(!check_value($_POST['gift_iindex'])) throw new Exception("<b>[ GIFT Code ]</b> Index de Item Incorrecto.");
        if(!check_value($_POST['gift_ilevel'])) throw new Exception("<b>[ GIFT Code ]</b> Level de Item Incorrecto.");
        if(!Validator::UnsignedNumber($_POST['gift_usedmax'])) throw new Exception("<b>[ GIFT Code ]</b> Maximos Usos Invalido.");
        if(!Validator::UnsignedNumber($_POST['gift_plvl'])) throw new Exception("<b>[ GIFT Code ]</b> Puntos por Level Invalido.");
        if(!Validator::UnsignedNumber($_POST['gift_niveles'])) throw new Exception("<b>[ GIFT Code ]</b> Niveles Invalido.");
        if(!Validator::UnsignedNumber($_POST['gift_resets'])) throw new Exception("<b>[ GIFT Code ]</b> Resets Invalido.");
        if(!Validator::UnsignedNumber($_POST['gift_mresets'])) throw new Exception("<b>[ GIFT Code ]</b> Master/Grand Resets Invalido.");
        if(!Validator::UnsignedNumber($_POST['gift_tvip'])) throw new Exception("<b>[ GIFT Code ]</b> Tipo de Vip Invalido.");
        if(!Validator::UnsignedNumber($_POST['gift_dvip'])) throw new Exception("<b>[ GIFT Code ]</b> Dias de Vip Invalido.");
        if(!Validator::UnsignedNumber($_POST['gift_coins'])) throw new Exception("<b>[ GIFT Code ]</b> Cantidad de Coins Invalido.");
        if(!Validator::UnsignedNumber($_POST['gift_item'])) throw new Exception("<b>[ GIFT Code ]</b> Activar/Desactivar Item Invalido.");
        if(!Validator::UnsignedNumber($_POST['gift_warecase'])) throw new Exception("<b>[ GIFT Code ]</b> Tipo de Entrega Invalido.");
        if(!Validator::UnsignedNumber($_POST['gift_igroup'])) throw new Exception("<b>[ GIFT Code ]</b> Grupo de Item Invalido.");
        if(!Validator::UnsignedNumber($_POST['gift_iindex'])) throw new Exception("<b>[ GIFT Code ]</b> Index de Item Invalido.");
        if(!Validator::UnsignedNumber($_POST['gift_ilevel'])) throw new Exception("<b>[ GIFT Code ]</b> Level de Item Invalido.");
        if($CodeUse['Codigo'] == $_POST['gift_code'] ) throw new Exception("<b>[ GIFT Code ]</b> El Codigo ya existe, intenta nuevamente.");	


        $query  = "INSERT INTO WEBENGINE_GIFT_CODE ";
        $query .= "(TypeCode,Codigo,UsedMax,UsedActual,pLevel,Niveles,Resets,MResets,VipType,VipDays,TypeCoin,Coins,Usuario,Personaje,DarItem,TypeWare,iName,iGroup,iIndex,iLevel) ";
        $query .= "VALUES (".$_POST['gift_tipeset'].",'".$_POST['gift_code']."',".$_POST['gift_usedmax'].",0,".$_POST['gift_plvl'].",".$_POST['gift_niveles'].",".$_POST['gift_resets'].",".$_POST['gift_mresets'].",".$_POST['gift_tvip'].",".$_POST['gift_dvip'].",'".$_POST['gift_tmoneda']."',".$_POST['gift_coins'].",'".$_POST['gift_user']."','".$_POST['gift_character']."',".$_POST['gift_item'].",".$_POST['gift_warecase'].",'".$_POST['gift_iname']."',".$_POST['gift_igroup'].",".$_POST['gift_iindex'].",".$_POST['gift_ilevel'].")";

        $result = $database->query_fetch_single($query);

        message('success', '<b>[ GIFT Code ]</b> Se ha generado correctamente el codigo para un uso de <b>'.$_POST['gift_usedmax'].'</b> veces.');
    }catch(Exception $ex) {
		message('error', $ex->getMessage());
	}
}

echo '<div class="row">';
	echo '<div class="col-md-12">';
		echo '<form action="?module=giftcode" method="post" role="form">';
            echo '<table class="table table-striped table-bordered table-hover" style="table-layout: fixed;">';
            echo '<tr>';
                echo '<td colspan="2">';
                    echo '<b>[ Tipo de Agregado/Seteo ]</b><br/><span>- Si elige la opcion <b>Agregado</b> los <b>puntos por level, resets, grand/master resets, etc</b> se agregaran a los que el usuario ya posea.</span><br/><span>- Si elige la opcion <b>Seteo</b> los <b>puntos por level, resets, grand/master resets, etc</b> del personaje se reemplazaran por los que coloco en su codigo.</span>';
                echo '</td>';
                echo '<td colspan="3">';
                    echo '<select class="form-select my-1 mr-sm-2" id="gift_tipeset" name="gift_tipeset">';
						echo '<option value="0" selected>Agregado</option>';
						echo '<option value="1">Seteo</option>';
					echo '</select>';
                echo '</td>';
            echo '</tr>';
            echo '<tr>';
                echo '<td colspan="5">';
                    echo '<div class="form-group text-center">';
                        echo '<label for="gift_code">Codigo de Regalo</label>';
                        echo '<input type="text" name="gift_code" class="form-control bg-success text-white text-center fs-3" id="gift_code" value="'.$Codigo.'" readonly>';
                    echo '</div>';
                echo '</td>';
            echo '</tr>';            
            echo '<tr>';
                echo '<td>';
                    echo '<div class="form-group">';
                        echo '<label for="gift_usedmax">Usos Maximos</label>';
                        echo '<input type="text" name="gift_usedmax" class="form-control" id="gift_usedmax" value="1" required>';
                    echo '</div>';
                echo '</td>';
                echo '<td>';
                    echo '<div class="form-group">';
                        echo '<label for="gift_plvl">Puntos de Nivel</label>';
                        echo '<input type="number" name="gift_plvl" class="form-control" id="gift_plvl" value="0" required>';
                    echo '</div>';
                echo '</td>';
                echo '<td>';
                    echo '<div class="form-group">';
                        echo '<label for="gift_niveles">Nivel</label>';
                        echo '<input type="number" name="gift_niveles" class="form-control" id="gift_niveles" value="0" required>';
                    echo '</div>';
                echo '</td>';
                echo '<td>';
                    echo '<div class="form-group">';
                        echo '<label for="gift_resets">Resets</label>';
                        echo '<input type="number" name="gift_resets" class="form-control" id="gift_resets" value="0" required>';
                    echo '</div>';
                echo '</td>';
                echo '<td>';
                    echo '<div class="form-group">';
                        echo '<label for="gift_mresets">Master/Grand Resets</label>';
                        echo '<input type="number" name="gift_mresets" class="form-control" id="gift_mresets" value="0" required>';
                    echo '</div>';
                echo '</td>';
            echo '</tr>';
            echo '<tr>';
                echo '<td colspan="2">';
                    echo '<b>[ Tipo de Vip ]</b><br/><span><b>0</b> - Sin VIP | <b>1</b> - Vip Bronce | <b>2</b> - Vip Plata | <b>3</b> = Vip Oro.</span>';
                echo '</td>';
                echo '<td>';
                    echo '<div class="form-group">';
                        echo '<label for="gift_tvip">Tipo de Vip</label>';
                        echo '<input type="number" name="gift_tvip" class="form-control" id="gift_tvip" value="0" required>';
                    echo '</div>';
                echo '</td>';
                echo '<td colspan="2">';
                    echo '<div class="form-group">';
                        echo '<label for="gift_dvip">Dias de Vip</label>';
                        echo '<input type="number" name="gift_dvip" class="form-control" id="gift_dvip" value="0" required>';
                    echo '</div>';
                echo '</td>';
            echo '</tr>';
            echo '<tr>';
                echo '<td colspan="2">';
                    echo '<b>[ Moneda ]</b><br/><span>Colocar exactamente nombre de la Coin a usar. <b>[WCoinC, WCoinP o GoblinPoints ]</b> .</span>';
                echo '</td>';
                echo '<td>';
                    echo '<div class="form-group">';
                        echo '<label for="gift_tmoneda">Moneda</label>';
                        echo '<input type="text" name="gift_tmoneda" class="form-control" id="gift_tmoneda" value="WCoinC" required>';
                    echo '</div>';
                echo '</td>';
                echo '<td colspan="2">';
                    echo '<div class="form-group">';
                        echo '<label for="gift_coins">Cantidad de Coins</label>';
                        echo '<input type="number" name="gift_coins" class="form-control" id="gift_coins" value="0" required>';
                    echo '</div>';
                echo '</td>';
            echo '</tr>';
            echo '<tr>';
                echo '<td colspan="2">';
                    echo '<b>[ Nombre de Usuario y Personaje ]</b><br/><span>Si el valor esta en <b>0</b> en ambos campos el codigo puede usarlo cualquier cuenta.</span>';
                echo '</td>';
                echo '<td>';
                    echo '<div class="form-group">';
                        echo '<label for="gift_user">Usuario</label>';
                        echo '<input type="text" name="gift_user" class="form-control" id="gift_user" value="0" required>';
                    echo '</div>';
                echo '</td>';
                echo '<td colspan="2">';
                    echo '<div class="form-group">';
                        echo '<label for="gift_character">Personaje</label>';
                        echo '<input type="text" name="gift_character" class="form-control" id="gift_character" value="0" required>';
                    echo '</div>';
                echo '</td>';
            echo '</tr>';
            echo '<tr>';
                echo '<td colspan="2">';
                    echo '<b>[ Activar / Desactivar el Dar Box ]</b><br/><span>Si esta <b>desactivado</b> el personaje no recibira ningun item.</span>';
                echo '</td>';
                echo '<td colspan="3">';
                    echo '<select class="form-select my-1 mr-sm-2" id="gift_item" name="gift_item">';
						echo '<option value="0" selected>Desactivado</option>';
						echo '<option value="1">Activado</option>';
					echo '</select>';
                echo '</td>';
            echo '</tr>';
            echo '<tr>';
                echo '<td colspan="2">';
                    echo '<b>[ Gremory Case o Warehouse ]</b><br/><span>Elige si desea entregar el item al <b>Gremory Case</b> o al <b>Warehouse</b> (Baul).</span>';
                echo '</td>';
                echo '<td colspan="3">';
                    echo '<select class="form-select my-1 mr-sm-2" id="gift_warecase" name="gift_warecase">';
						echo '<option value="0" selected>Warehouse</option>';
						echo '<option value="1">Gremory Case</option>';
					echo '</select>';
                echo '</td>';
            echo '</tr>';
            echo '<tr>';
                echo '<td>';
                    echo '<b>[ Item Box a Dar ]</b><br/><span>Colocar el <b> Index, group y level</b> de la box a entregar.</span>';
                echo '</td>';
                echo '<td>';
                    echo '<div class="form-group">';
                        echo '<label for="gift_igroup">Grupo</label>';
                        echo '<input type="number" name="gift_igroup" class="form-control" id="gift_igroup" value="14" required>';
                    echo '</div>';
                echo '</td>';
                echo '<td>';
                    echo '<div class="form-group">';
                        echo '<label for="gift_iindex">Index</label>';
                        echo '<input type="number" name="gift_iindex" class="form-control" id="gift_iindex" value="55" required>';
                    echo '</div>';
                echo '</td>';
                echo '<td>';
                    echo '<div class="form-group">';
                        echo '<label for="gift_ilevel">Level</label>';
                        echo '<input type="number" name="gift_ilevel" class="form-control" id="gift_ilevel" value="0" required>';
                    echo '</div>';
                echo '</td>';
                echo '<td>';
                    echo '<div class="form-group">';
                        echo '<label for="gift_iname">Item Name</label>';
                        echo '<input type="text" name="gift_iname" class="form-control" id="gift_iname" value="Nombre Item" required>';
                    echo '</div>';
                echo '</td>';
            echo '</tr>';

            echo '</table>';
            echo '<input type="submit" name="submit_codigo" class="btn btn-primary" value="Generar nuevo Gift Code"/>';
		echo '</form>';
	echo '</div>';
echo '</div>';
