<?
### Версия ПО (ДМ) 1.0.0.0 ( (c)2018 MISOFT Продукт защищен регистрацией патента и авторских прав! www.misoft.studio )
######################################################################################################################

if ( $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' ) {
    define( 'ROOT_DIR', $_SERVER['DOCUMENT_ROOT'] );
    session_start();
    header( 'Content-type: text/html; charset=windows-1251' );
    include_once ROOT_DIR . '/engine/config.php';
    include_once ROOT_DIR . '/engine/function.php';
} else {
    if ( !defined( 'MISOFT' ) ) { exit(); }
}

if ( !isset( $_SESSION['iduser'] ) ) {
    echo '<div style="margin-left: -10px;" class="alert alert-info alert-dismissible fade in" role="alert">
			<button type="button" class="close" data-dismiss="alert" aria-label="Close">
				<span aria-hidden="true">&times;</span>
			</button>
			<h5 class="alert-heading"><i class="fa fa-exclamation-triangle" aria-hidden="true"></i> Внимание!</h5>
			<p style="font-size: 14px;">Вы не авторизованы. Доступ к этой странице разрешен только авторизованным пользователям. <br>Пожалуйста, воспользуйтесь формой авторизации.</p>
			<p style="font-size: 14px;" class="m-b-0">Для перехода в форму авторизации нажмите <a class="a_link" href="/login" >login</a>.</p>
		  </div>';
} else
if ( !$config_user['activate'] ) {
    echo '<div style="margin-left: -10px;" class="alert alert-info alert-dismissible fade in" role="alert">
			<button type="button" class="close" data-dismiss="alert" aria-label="Close">
				<span aria-hidden="true">&times;</span>
			</button>
			<h5 class="alert-heading"><i class="fa fa-exclamation-triangle" aria-hidden="true"></i> Внимание!</h5>
			<p style="font-size: 14px;">Для продолжения, Вам необходимо активировать свой аккаунт, перейдя в раздел <a class="a_link" href="/profile" >настройки аккаунта</a>. </p>
			
		  </div>';
} else {

#Определяем страну 
$regcode = find_country();

#Выборка при не активном фильтре
if ( $_POST['type'] == 'kategories' ) {
    switch ( $_POST['zapros'] ) {
        case 1:
            $zapros = " and t1.ShownKategories = 1";
            break;
        case 2:
            $zapros = " and t1.ShownKategories = 2";
            break;
        case 3:
            $zapros = " and t1.ShownKategories = 3";
            break;
        case 4:
            $zapros = " and t1.ShownKategories = 4";
            break;
        case 5:
            $zapros = " and t1.ShownKategories = 5";
            break;
        case 6:
            $zapros = " and t1.ShownKategories = 6";
            break;
        case 7:
            $zapros = " and t1.ShownKategories = 7";
            break;
        case 8:
            $zapros = " and t1.ShownKategories = 8";
            break;
        case 9:
            $zapros = " and t1.ShownKategories = 9";
            break;
        case 10:
            $zapros = " and t1.ShownKategories = 10";
            break;
        case 11:
            $zapros = " and t1.ShownKategories = 11";
            break;
        case 12:
            $zapros = " and t1.ShownKategories = 12";
            break;
        case 13:
            $zapros = " and t1.ShownKategories = 13";
            break;
        case 14:
            $zapros = " and t1.ShownKategories = 14";
            break;
        case 15:
            $zapros = " and t1.ShownKategories = 15";
            break;
        case 16:
            $zapros = " and t1.ShownKategories = 16";
            break;
        
		default:
            $zapros = '';
			$zapros2 = 'ORDER BY t1.date DESC';
            break;
    }
} else
	
if ( $_POST['type'] == 'status' ) {
    switch ( $_POST['zapros'] ) {
        case 1:
            $zapros = " and t1.vip = 1";
            break;
        case 2:
            $zapros = " and t1.many = 1";
            break;
        default:
            $zapros = '';
			$zapros2 = 'ORDER BY t1.date DESC';
            break;
    }
} else
	
if ( $_POST['type'] == 'id' ) {
    if ( !empty( $_POST['zapros'] ) )
    $zapros = " and t1.id = " . ( int ) $_POST['zapros'];
    else
    $zapros = '';
	$zapros2 = 'ORDER BY t1.date DESC';
} else
	
if ( $_POST['type'] == 'url' ) {
    if ( !empty( $_POST['zapros'] ) )
    $zapros = " and t1.url LIKE '%" . check_text( $_POST['zapros'] ) . "%'";
    else
    $zapros = '';
	$zapros2 = 'ORDER BY t1.date DESC';
} else
	
if ( $_POST['type'] == 'autor' ) {
    if ( !empty( $_POST['zapros'] ) ) {
        $zapros = " and t1.iduser = " . ( int ) $_POST['zapros'];;
    } else {
		$zapros = '';
		$zapros2 = 'ORDER BY t1.date DESC';
	}
} else
	
if ( $_POST['type'] == 'money' ) {
    if ( !empty( $_POST['zapros'] ) ) {
		if ( ( int ) $_POST['zapros'] == 1 ) { $mon_zapros = 'DESC'; } else { $mon_zapros = 'ASC'; }	
		$zapros2 = "ORDER BY t1.price " . $mon_zapros . "";
	} 
	if ( ( int ) $_POST['zapros'] == 1 AND ( int ) $_POST['zapros'] == 2 ) {
		$zapros2 = 'ORDER BY t1.date DESC';
	}
} else 
	
if ( $_POST['type'] == 'coment' ) {
    if ( !empty( $_POST['zapros'] ) ) {
		if ( ( int ) $_POST['zapros'] == 1 ) { $coment_zapros = 't1.t_goods'; } else 	
		if ( ( int ) $_POST['zapros'] == 2 ) { $coment_zapros = 't1.t_bad'; } else 	
		if ( ( int ) $_POST['zapros'] == 3 ) { $coment_zapros = 't1.t_wait'; } 	
		$zapros2 = "ORDER BY " . $coment_zapros . " DESC";
	} 
	if ( ( int ) $_POST['zapros'] == 1 AND ( int ) $_POST['zapros'] == 2 AND ( int ) $_POST['zapros'] == 3 ) {
		$zapros2 = 'ORDER BY t1.date DESC';
	}
} else {
	$zapros2 = 'ORDER BY t1.date DESC';
}

#Выборка при активном фильтре
$cokie_task_status = ( int ) $_COOKIE['task_status'];
if($cokie_task_status > 0) {
	if(check_text( $_COOKIE['type'] ) == 'kategories') {
		$cokie_task_value = ( int ) $_COOKIE['zapros'];
		$zapros = " and t1.ShownKategories = $cokie_task_value";
	} else 
	
	if(check_text( $_COOKIE['type'] ) == 'status') {
		$cokie_task_value = ( int ) $_COOKIE['zapros'];
		if ($cokie_task_value == 1) {
			$zapros = " and t1.vip = 1";
		} else 
		if ($cokie_task_value == 2) {
			$zapros = " and t1.many = 1";
		}  
	} else 
	
	if(check_text( $_COOKIE['type'] ) == 'url') {
		$cokie_task_value = check_text( $_COOKIE['zapros'] );
		$zapros = " and t1.url LIKE '%" . $cokie_task_value . "%'";
	} else 
	
	if(check_text( $_COOKIE['type'] ) == 'autor') {
		$cokie_task_value = ( int ) $_COOKIE['zapros'];
		$zapros = " and t1.iduser = $cokie_task_value";
	} else 
	
	if(check_text( $_COOKIE['type'] ) == 'money') {
		$cokie_task_value = ( int ) $_COOKIE['zapros'];
		if ( $cokie_task_value == 1 ) { $mon_zapros = 'DESC'; } else { $mon_zapros = 'ASC'; }	
		$zapros2 = "ORDER BY t1.price " . $mon_zapros . "";
	} else 
	
	if(check_text( $_COOKIE['type'] ) == 'coment') {
		$cokie_task_value = ( int ) $_COOKIE['zapros'];
		if ( $cokie_task_value == 1 ) { $coment_zapros = 't1.t_goods'; } else 	
		if ( $cokie_task_value == 2 ) { $coment_zapros = 't1.t_bad'; } else 	
		if ( $cokie_task_value == 3 ) { $coment_zapros = 't1.t_wait'; } 	
		$zapros2 = "ORDER BY " . $coment_zapros . " DESC";
	}
}

#Запрос подсчета количества страниц (являеться аналогом центрального циклового запроса, за исключением параметров сортировки)
$tasks_page = mysql_fetch_array( mysql_query( "SELECT COUNT(id) as count FROM tasks t1 
	WHERE 
		t1.paused = 1 and t1.balance >= (t1.t_wait + t1.t_work ) * t1.price  / '" . $config['factor_tasks'] . "' + t1.price  / '" . $config['factor_tasks'] . "' - 0.02 
		
		and NOT EXISTS(SELECT ident, iduser, status, startdate FROM tasks_temp t2 WHERE 
			(t2.ident = t1.id and t2.status in (1,4) and t1.many in (1) and t2.startdate + executionTime > '" . time() . "' and t2.iduser = '" . $_SESSION['iduser'] . "') or 
			(t2.ident = t1.id and t2.status in (1) and t1.many in (0) and t2.iduser = '" . $_SESSION['iduser'] . "')
		)
		
		$zapros 
		
		and id IN (SELECT id FROM tasks 
			WHERE geo_ru = '".$regcode."' OR geo_ua = '".$regcode."' OR geo_by = '".$regcode."' OR geo_kz = '".$regcode."' OR geo_ge = '".$regcode."' 
			   OR geo_md = '".$regcode."' OR geo_am = '".$regcode."' OR geo_tj = '".$regcode."' OR geo_az = '".$regcode."' OR geo_uz = '".$regcode."' 
			   OR geo_pt = '".$regcode."' OR geo_es = '".$regcode."' OR geo_ee = '".$regcode."' OR geo_lv = '".$regcode."' OR geo_lt = '".$regcode."' 
			   OR geo_us = '".$regcode."' OR geo_gb = '".$regcode."' OR geo_fr = '".$regcode."' OR geo_de = '".$regcode."' OR geo_cn = '".$regcode."' 
			   OR geo_it = '".$regcode."' OR geo_all = '".$regcode."') 
		
		and id in (SELECT id FROM tasks WHERE (TimeoutInExecution = '0') or (TimeoutInExecution > '0' and TimeoutResidue > '0')) 
		
		and id in (SELECT id FROM tasks WHERE (nextExecutionOn = '0') or (nextExecutionOn > '0' and nextExecution + nextExecutionOn < '".time()."')) 
		
		and id in (SELECT id FROM tasks WHERE (openReyting = '0') or (openReyting > '0' and openReyting <= '".$config_user['reyting']."')) 
		
		and id in (SELECT id FROM tasks WHERE (openWmid = '0') or (openWmid = '1' and openWmid < '".$config_user['wmid']."'))" ) 
	);

$kolvo = 15;
$all = $tasks_page['count'];
$allpage = ceil( $all / $kolvo );
$this_page = ( isset( $_GET['page'] ) ) ? intval( $_GET['page'] ) : 1 ;
if ( $this_page < 1 ) $this_page = 1;
if ( $this_page > $allpage ) $this_page = $allpage;
$start = $this_page * $kolvo - $kolvo;
if ( $start == -$kolvo ) { $start = 1; }
?>
    <? include_once ROOT_DIR . '/modules/panel_manager/section_earnings.php'; ?>
		
	<div class="content_bar">
		<article class="postBox">
			
			<? include_once ROOT_DIR . '/modules/tasks/menu.php'; ?>
			<? include_once ROOT_DIR . '/modules/tasks/search.php'; ?>
			
			<script type="text/javascript">
			  $(document).ready(function(){
			    $('.workcomp').tipTip({defaultPosition:'right'});
			    $('.cursor-help').tipTip({defaultPosition:'right'});
			    $('.advertiser-task-show').tipTip({defaultPosition:'right'});
			   });
		    </script>
			
			<table class="work-serf">
				<tbody>
					<?
						$sh_tasks = mysql_query( "SELECT * FROM tasks t1 
							WHERE 
							    t1.paused = 1 and t1.balance >= (t1.t_wait + t1.t_work ) * t1.price  / '" . $config['factor_tasks'] . "' + t1.price  / '" . $config['factor_tasks'] . "' - 0.02 
							
								and NOT EXISTS( SELECT ident, iduser, status, startdate FROM tasks_temp t2 WHERE 
									(t2.ident = t1.id and t2.status in (1,4) and t1.many in (1) and t2.startdate + executionTime > '" . time() . "' and t2.iduser = '" . $_SESSION['iduser'] . "') or 
									(t2.ident = t1.id and t2.status in (1) and t1.many in (0) and t2.iduser = '" . $_SESSION['iduser'] . "' ) 
								)
								
								$zapros 
								
								and id IN (SELECT id FROM tasks 
									WHERE geo_ru = '".$regcode."' OR geo_ua = '".$regcode."' OR geo_by = '".$regcode."' OR geo_kz = '".$regcode."' OR geo_ge = '".$regcode."' 
									   OR geo_md = '".$regcode."' OR geo_am = '".$regcode."' OR geo_tj = '".$regcode."' OR geo_az = '".$regcode."' OR geo_uz = '".$regcode."' 
									   OR geo_pt = '".$regcode."' OR geo_es = '".$regcode."' OR geo_ee = '".$regcode."' OR geo_lv = '".$regcode."' OR geo_lt = '".$regcode."' 
									   OR geo_us = '".$regcode."' OR geo_gb = '".$regcode."' OR geo_fr = '".$regcode."' OR geo_de = '".$regcode."' OR geo_cn = '".$regcode."' 
									   OR geo_it = '".$regcode."' OR geo_all = '".$regcode."') 
								
								and id in (SELECT id FROM tasks WHERE (TimeoutInExecution = '0') or (TimeoutInExecution > '0' and TimeoutResidue > '0')) 
								
								and id in (SELECT id FROM tasks WHERE (nextExecutionOn = '0') or (nextExecutionOn > '0' and nextExecution + nextExecutionOn < '".time()."')) 
								
								and id in (SELECT id FROM tasks WHERE (openReyting = '0') or (openReyting > '0' and openReyting <= '".$config_user['reyting']."')) 
								
								and id in (SELECT id FROM tasks WHERE (openWmid = '0') or (openWmid = '1' and openWmid < '".$config_user['wmid']."')) 
								
								$zapros2 LIMIT $start,$kolvo" 
						);
						
						if ( mysql_num_rows( $sh_tasks ) > 0 ) {
						
							while ( $sh_tasks_row = mysql_fetch_array( $sh_tasks ) ) {
								#Иконка задания на которое нажали
								$img_tasks_footstep = '<img src="/theme/images/footstep_adv.png" width="32" height="32" alt="" class="icon-vip-task" title="Просмотренное задание" id="icon_24h_task' . $sh_tasks_row['id'] . '" />';
								
								#Иконка задания
								$img_tasks = '<div title="Обычное задание" id="icon_standart_task' . $sh_tasks_row['id'] . '" ><i class="fa fa-briefcase color-green" ></i></div>';
								
								#Проверка на многоразовое задание
								if ( $sh_tasks_row['many'] == 1 ) {
									$img_tasks = '<img src="/theme/images/24-hours.png" width="32" height="32" alt="" class="icon-vip-task" title="Задание многоразовое" id="icon_24h_task' . $sh_tasks_row['id'] . '" />';
								} else
								
								#Проверка V.I.P
								if ( $sh_tasks_row['vip'] == 1 ) {
									$img_tasks = '<img src="/theme/images/crown.png" width="32" height="32" alt="" class="icon-vip-task" title="Задание V.I.P категории" id="icon_vip_task' . $sh_tasks_row['id'] . '" />';
								} 
								
								#Автор задани?
								$task_user_avtor = mysql_fetch_array( mysql_query( "SELECT username, id FROM user WHERE id = '" . $sh_tasks_row['iduser'] . "' LIMIT 1" ) );
								
								#Категория задания
								if ($sh_tasks_row['ShownKategories'] == 1 ) { $kategories_task_text = 'Только регистрация'; } else
								if ($sh_tasks_row['ShownKategories'] == 2 ) { $kategories_task_text = 'Только клики'; } else
								if ($sh_tasks_row['ShownKategories'] == 3 ) { $kategories_task_text = 'Регистрация с активностью'; } else
								if ($sh_tasks_row['ShownKategories'] == 4 ) { $kategories_task_text = 'Приглашение в рефералы'; } else
								if ($sh_tasks_row['ShownKategories'] == 5 ) { $kategories_task_text = 'Социальные сети'; } else
								if ($sh_tasks_row['ShownKategories'] == 6 ) { $kategories_task_text = 'YouTube'; } else
								if ($sh_tasks_row['ShownKategories'] == 7 ) { $kategories_task_text = 'Постинг в форумы и/или блоги'; } else
								if ($sh_tasks_row['ShownKategories'] == 8 ) { $kategories_task_text = 'Написать статью или пресс-релиз'; } else
								if ($sh_tasks_row['ShownKategories'] == 9 ) { $kategories_task_text = 'Отзыв/проголосовать'; } else
								if ($sh_tasks_row['ShownKategories'] == 10 ) { $kategories_task_text = 'Forex'; } else
								if ($sh_tasks_row['ShownKategories'] == 11 ) { $kategories_task_text = 'Bitcoin/Litecoin/Dogecoin'; } else
								if ($sh_tasks_row['ShownKategories'] == 12 ) { $kategories_task_text = 'Играть в игры'; } else
								if ($sh_tasks_row['ShownKategories'] == 13 ) { $kategories_task_text = 'Инвестировать'; } else
								if ($sh_tasks_row['ShownKategories'] == 14 ) { $kategories_task_text = 'Мобильные приложения'; } else
								if ($sh_tasks_row['ShownKategories'] == 15 ) { $kategories_task_text = 'Бонусы'; } else
								if ($sh_tasks_row['ShownKategories'] == 16 ) { $kategories_task_text = 'Прочее'; }
								
								#Обрезаем ссылку
								$task_cropped_link = crop_the_link($sh_tasks_row['url'], '50');
								
								?>	
								  <script type="text/javascript">
								    $(document).ready(function(){
									  $('#icon_standart_task<?=$sh_tasks_row['id']?>').tipTip({defaultPosition:'right'});
									  $('#icon_24h_task<?=$sh_tasks_row['id']?>').tipTip({defaultPosition:'right'});
									  $('#icon_vip_task<?=$sh_tasks_row['id']?>').tipTip({defaultPosition:'right'});
								    });
									function footstep(a) {
									  $('#footstep_'+ a +'_off').fadeOut(5);
									  $('#footstep_'+ a +'_on').fadeIn(3);
									}
								  </script>
								  <tr id="d<?=$sh_tasks_row['id']?>">
									<td class="normal" width="40" valign="top" >
									  <div class="img-icon-task" id="footstep_<?=$sh_tasks_row['id']?>_off">
										<?=$img_tasks?>
									  </div>
									  <div class="img-icon-task" id="footstep_<?=$sh_tasks_row['id']?>_on" style="display: none;">
										<?=$img_tasks_footstep?>
									  </div>
									</td>
									<td class="normalm" valign="top" >
									  <div class="info-show-task">
										<a href="/tasksview=<?=$sh_tasks_row['id']?>" target="_blank" onclick="footstep(<?=$sh_tasks_row['id']?>);"><?=$sh_tasks_row['title']?></a><br>
										<span class="desctext"><?=$task_cropped_link?></span> 
										<span class="desctext">ID:<?=$sh_tasks_row['id']?> - <?=$kategories_task_text?></span>
									  </div>
									</td>
									<td class="normal" style="text-align: center;" valign="center" nowrap="nowrap" >
									  <div class="advertiser-task-show" title="Рекламодатель: <?=$task_user_avtor['username']?>">
									    <a href="/id<?=$task_user_avtor['id']?>"><i class="fa fa-bullhorn advertiser-task-show_link" aria-hidden="true"></i></a>
									  </div>
									  <span class="clickprice color-grey"><?=moneyraund( $sh_tasks_row['price'], 3 )?> <?=$config['valutaname']?> </span><br>
									</td>
									<td class="normal" style="text-align: center; width: 15%;">
									  <span title="Одобрено заданий" class="color-green cursor-help"><?=$sh_tasks_row['t_goods']?></span> - 
									  <span title="Отклонено заданий" class="color-red cursor-help"><?=$sh_tasks_row['t_bad']?></span> - 
									  <span title="На проверке заданий" class="color-grey cursor-help"><?=$sh_tasks_row['t_wait']?></span>
									  
									</td>
									<td class="normal" style="width: 6.6%;" >
									  <div class="doing-show-task">
										<a onclick="adv_ell.complaints(<?=$sh_tasks_row['id']?>, 'task');" class="complaints_adv"  target="_blank">
										  <i class="glyphicon glyphicon-ban-circle workcomp" aria-hidden="true" title="Пожаловаться на рекламу" ></i>
										</a>
									  </div>
									</td>
								  </tr>
								<?
							}
						} else {
						 ?>
						  <tr>
							<td colspan="2">
							  <div class="inquiry-search-task"><i class="fa fa-info-circle" aria-hidden="true"></i> Bаш запрос не дал результатов.</div>
						    </td>
						  </tr>
						 <?
						}
					?>
				</tbody>
			</table>
			<div class="navigation-page-task"><? echo navigation( 15, ( int ) $_GET['page'], $tasks_page['count'], 'tasks&p=' ); ?></div>
		</article>
	</div>
  <?
}
?>