<?PHP
include('header.php'); 
$sorting = (isset($_GET["sorting"])) ? valideint($_GET["sorting"]) : '1';
$active1 = $active2 = $active3 = $active4 = NULL;
switch($sorting){
case 1:$active1 = "active"; $sql_req = "ORDER BY `joindate` DESC";break;
case 2:$active2 = "active"; $sql_req = "AND `toref`>'0'";break;
case 3:$active3 = "active"; $sql_req = "AND `id` IN (SELECT `rid` FROM `b_jarmarka`)";break;
case 4:$active4 = "active"; $sql_req = "AND `autoproc`>'0'";break;
default:$active1 = "active"; $sql_req = "ORDER BY `joindate` DESC";
}
echo "<span class='top-bl-s-verh'>Автоматический рефбэк новым рефералам</span>
<div class='menucenter-top1'>
<table class='profile' width='100%' border='0' cellpadding='0' cellspacing='0' style='margin-bottom: 0;'>
<tr><td class='value1'>
Выбирите необходимый рефбэк для своих рефералов. Не скупитесь на этом значении и получите большой приток в свою команду.
</td></tr>
</table><br />";
echo "
<div id='info_ref'></div>
<div class='slider'></div><br>";
echo "<center><span id='slider-result'>$new_rb</span><input type='hidden' id='new_rb_num'> <span style='font: 30px sans-serif;color: #135b11;'>%</span> <img src='/css/img/save_rb.png' onclick='new_rb();'  title='Сохранить рефбек' style='margin-left:20px;'></center>";
echo "";
?>
<script type="text/javascript" src="https://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<script type="text/javascript" src="/js/jquery.tablesorter.js"></script>
<script type="text/javascript" src="/js/jquery.tablesorter.widgets.js"></script>
<script type="text/javascript" src="/js/jquery.tablesorter.pager.js"></script>
<?
require_once('bbcode.php');
$username = $_SESSION["username"];
$result = mysql_query("SELECT * FROM b_users WHERE username='$username'");
$duser = mysql_fetch_assoc($result);
if(isset($_POST["text"])){
  $text_sql = bbcod($_POST["text"], true, true);

  $ref_auto=mysql_query("SELECT `id_us` FROM `still_auto_refbek` WHERE `id_us` = '".$duser["id"]."' LIMIT 1");
  if(mysql_num_rows($ref_auto) > 0){
    $ra = mysql_fetch_assoc($ref_auto);

    mysql_query("UPDATE `still_auto_refbek` SET `text` = '$text_sql', `refbek` = '$refbek_sql' WHERE `id_us` = '".$ra["id_us"]."'");
    echo'<span class="taskquestion"><center>Данные успешно изменены</center></span>';
  }else{

    mysql_query("INSERT INTO `still_auto_refbek` (`id_us`, `text`, `refbek`) VALUES ('".$duser["id"]."','$text_sql','$refbek_sql')");
    echo'<span class="taskquestion"><center>Данные успешно сохранены</center></span>';
  }

}
$ref_auto=mysql_query("SELECT `text`, `refbek` FROM `still_auto_refbek` WHERE `id_us` = '".$duser["id"]."' LIMIT 1");
$ra = mysql_fetch_assoc($ref_auto);
?> 
<h2>Ваше приветствие рефералам</h2>
<form id="comment" method="POST" action="">
<table class='profile' width='100%' border='0' cellpadding='0' cellspacing='0' style='margin-bottom: 0;'>
<tr>
<td class="value">
<textarea id="comess" name="text" rows=0 class="comtextur" style="width:100%;height: 200px;"><?=bbb($ra["text"])?></textarea>
</td></tr></table><br /><center>
<input type='submit' class='button-green' style="height:26px;" value='Сохранить приветствие' />
</center>
</form>
</div>
<script>
function hidemsg(){ $('#info_ref').fadeOut('slow'); $("#iinfo_ref").attr("style", ""); if(tm)clearTimeout(tm);}

$(document).ready(function(){
$( ".slider" ).slider({
animate: true,
range: "min",
value: <?=$new_rb?>,
min: 0,
max: 100,
step: 1,
slide: function( event, ui ) {
$( "#slider-result" ).html(ui.value);
},
change: function(event, ui) {
$('#new_rb_num').attr('value', ui.value);
}
});
});
$('table').tablesorter({
    theme: 'green',
    sortInitialOrder: "desc"
}).tablesorterPager({
    container: $(".pager"),
    page: 0,
    size: 10,
    cssNext: '.next',
    cssPrev: '.prev',
    cssFirst: '.first',
    cssLast: '.last',
    cssGoto: '.gotoPage',
    cssPageDisplay: '.pagedisplay',
    cssPageSize: '.pagesize',
    cssDisabled: 'disabled'
});

var progress = false;

function refback_show(id){
var text = $.trim($("#bonusadd_"+id).text());
if(text != '') {
$("#bonusadd_"+id).html("");return false;
}
var proc_form = 
"<tr id='bonusadd_" + id + "'>"+
"<td class='ext' colspan='6'>"+
"<a class='addbudget' href='javascript:void(0);' onclick='refback_add(" + id + ",0);'>нет</a>"+
"<a class='addbudget' href='javascript:void(0);' onclick='refback_add(" + id + ",10);'>10%</a>"+
"<a class='addbudget' href='javascript:void(0);' onclick='refback_add(" + id + ",20);'>20%</a>"+
"<a class='addbudget' href='javascript:void(0);' onclick='refback_add(" + id + ",30);'>30%</a>"+
"<a class='addbudget' href='javascript:void(0);' onclick='refback_add(" + id + ",40);'>40%</a>"+
"<a class='addbudget' href='javascript:void(0);' onclick='refback_add(" + id + ",50);'>50%</a>"+
"<a class='addbudget' href='javascript:void(0);' onclick='refback_add(" + id + ",60);'>60%</a>"+
"<a class='addbudget' href='javascript:void(0);' onclick='refback_add(" + id + ",70);'>70%</a>"+
"<a class='addbudget' href='javascript:void(0);' onclick='refback_add(" + id + ",80);'>80%</a>"+
"<a class='addbudget' href='javascript:void(0);' onclick='refback_add(" + id + ",90);'>90%</a>"+
"</td></tr>";
$(proc_form).insertAfter("#users_"+id);
}

function refback_add(id, num){
if(!progress){
$.ajax({
type: "POST",url: "ajax/referals.php",
data: { 'func' : 'refback_add', "id_us" : id, "num" : num},
beforeSend: function() {progress = true;},
success: function(data) {
$("#sref-bonus_"+id).removeAttr('title');
if(num == 0){
$("#sref-bonus_"+id).hide('slow');
}else{
$("#sref-bonus_"+id).attr('title','Установлен бонус: '+num+'%').show('slow');
}
$("#bonusadd_"+id).remove();
progress = false;
}
});
}
}

function new_rb(){
if(!progress){
var num = $.trim($("#new_rb_num").val());
$.ajax({
type: "POST",url: "ajax/referals.php",
data: { 'func' : 'new_rb', 'num' : num},
beforeSend: function() {progress = true; $('#info_ref').html('').attr("style", "");},
success: function(data) {
if(data == 1){
$("#info_ref").html("<?=$infstart?><span class='taskquestion'><center>Рефбек успешно выставлен!</center></span><br /><?=$infend?>"); tm = setTimeout(function() { hidemsg() }, 3000);
}else if(data == 2){
$("#info_ref").html("<?=$infsterr?><span class='taskquestion'><center>Неверное значение рефбека!</center></span><br /><?=$infederr?>"); tm = setTimeout(function() { hidemsg() }, 3000);
}else{$("#info_ref").html("<?=$infsterr?> ERROR!<?=$infederr?>"); tm = setTimeout(function() { hidemsg() }, 3000);}
progress = false;
}
});
}
}

function add_jar(id){
var text = $.trim($("#exblock_"+id).text());
if(text != '') {
$("#exblock_"+id).html("");return false;
}
var jar_form = 
"<tr id='exblock_" + id + "'>"+
"<td class='ext' colspan='6' align='center'>"+
"<input type='text' id='price_ref' size='5' value=''>"+
" <a class='btn-blue136' href='javascript:void(0);' onclick='send_jar(" + id + ");'>Выставить на продажу</a>"+
"</td></tr>"
$(jar_form).insertAfter("#users_"+id);
}

function send_jar(id){
if(!progress){
var price = $.trim($("#price_ref").val());
var valute = "<?=$sitevalname?>";
$.ajax({
type: "POST",url: "ajax/referals.php",
data: { 'func' : 'send_jar', "id_us" : id, "price" : price},
beforeSend: function() {progress = true; $('#info_ref').html('').attr("style", "");},
success: function(data) {
if(data != ''){
$("#info_ref").html("<?=$infstart?>"+data+"<?=$infend?>");tm = setTimeout(function() { hidemsg() }, 3000);
}else{
$("#sale_jar_"+id).attr('title','Продаётся за '+price+''+valute).show('slow');
$("#success_s_j_"+id).html("<a class='refex-back' href='javascript:void(0);' onclick='back_ref("+id+");' title='Вернуть рефералла с ярмарки'></a>");
}
$("#exblock_"+id).remove();
progress = false;
}
});
}
}

function back_ref(id) {
if(!progress){
$.ajax({
type: "POST",url: "ajax/referals.php",
data: { 'func' : 'back_ref', "id_us" : id},
beforeSend: function() {progress = true;},
success: function(data) {
$("#sale_jar_"+id).removeAttr('title');
$("#sale_jar_"+id).hide('slow');
$("#success_s_j_"+id).html("<a class='refexchange' onclick='add_jar("+id+");' title='Продать на ярмарке'></a>");
progress = false;
}
});
}
}

function inf_user(id){
if(!progress){
var text = $.trim($("#infous_"+id).text());
if(text != '') {
$("#infous_"+id).remove(); return false;
}
$.ajax({
type: "POST",url: "ajax/inf_user.php",
data: { 'func' : 'inf_user', 'id' : id },
beforeSend: function() {progress = true;},
success: function(data) {
$(data).insertAfter("#users_"+id);
progress = false;
}
});
}
}

function dell_ref(id){
if(!progress){
if(confirm("Вы что хотите исключить реферала?")) {
$.ajax({
type: "POST",url: "ajax/referals.php",
data: { 'func' : 'dell_ref', "id" : id},
beforeSend: function() {progress = true;},
success: function(data) {
$("#users_"+id).remove();
progress = false;
}
});
}
}
}
      
function ref_show(num){
if(!progress){
if(num == '') { num = '0'; }
$.ajax({
type: "POST",url: "ajax/referals.php",
data: { 'func' : 'ref_show', "num" : num},
beforeSend: function() {progress = true;},
success: function(data) {
$("#ref_show").html(data);
$('span.filterlineactive').removeClass().addClass( "filterline");
$("#limk_"+num).addClass( "filterlineactive");
progress = false;
}
});
}
}
</script>
</div>
<?PHP include('footer.php');?>