<?

session_start();

header( 'Content-type: text/html; charset=windows-1251' );

define( 'ROOT', 'http://' . $_SERVER['HTTP_HOST'] );
define( 'ROOT_DIR', $_SERVER['DOCUMENT_ROOT'] );

if ( $_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest' ) { exit(); }

if ( !isset( $_SESSION['iduser'] ) ) exit();

include_once ROOT_DIR . '/engine/config.php';

$id = ( int ) $_POST['id'];

$pr_task = mysql_query( "SELECT id FROM tasks WHERE id = '" . $id . "' and iduser = '" . $_SESSION['iduser'] . "' LIMIT 1" );
if ( mysql_num_rows( $pr_task ) > 0 ) {
    $pr_task_row = mysql_fetch_array( $pr_task );
    ?>
    <script type="text/javascript">
    function send_revision(tid, uid) {
        $.post('ajax.php?func=task_revision', {
            id: tid,
            user: uid,
            revision: $('#task_revision'+tid).val()
        }, function(data) {
            if (data == 1) {
                $('#task'+tid).remove();
            } else
            if (data == 2) {
                $('#task_revision'+tid).focus();
            }
        });
        return false;
    }
    
    $("#close_box").click(function() {
        Page.Go('/mtasks');
    });
    
    function show_refuse(e) {
        if ($('#refuse'+e).css('display') == 'none') {
            $('#refuse'+e).fadeIn(500);
        } else {
            $('#refuse'+e).fadeOut(500);
        }
        return false;
    }
    
    function show_revision(e) {
        if ($('#revision'+e).css('display') == 'none') {
            $('#revision'+e).fadeIn(500);
        } else {
            $('#revision'+e).fadeOut(500);
        }
        return false;
    }
    
    function send_refuse(tid, uid) {
        $.post('ajax.php?func=send_refuse', {
            id: tid,
            user: uid,
            refuse: $('#task_refuse'+tid).val()
        }, function(data) {
            if (data == 1) {
                $('#task'+tid).remove();
            } else
            if (data == 2) {
                $('#task_refuse'+tid).focus();
            }
        });
        return false;
    }
    function ok_tasks(tid, uid) {
        $.post('ajax.php?func=ok_tasks', {
            id: tid,
            user: uid
        }, function(data) {
            if (data == 1) {
                $('#task'+tid).remove();
            }
        });
        return false;
    }
    function tasks_black_user(tid, uid) {
        $.post('ajax.php?func=tasks_black_user', {
            id: tid,
            user: uid
        }, function(data) {
            if (data == 1) {
                $('body').Qnotify({text:'Испольнитель успешно добавлен в черный список!', sound:'error'});
            } else {
                $('body').Qnotify({text:data, sound:'error'});
            }
        });
        return false;
    }
    </script>
    <?
    $task_wait = mysql_query( "SELECT * FROM tasks_temp WHERE ident = '" . $pr_task_row['id'] . "' and status = 4" );
    while ( $task_wait_row = mysql_fetch_array( $task_wait ) ) {
        $user_send = mysql_fetch_array( mysql_query( "SELECT id, username, avatar, ip FROM user WHERE id = '" . $task_wait_row['iduser'] . "'" ) );
        $timer = $task_wait_row['startdate'] + 86400 * 3 - 7200;
        $t = $timer - time();
        ?>
        <script type="text/javascript">
            var limit<?=$task_wait_row['id']?> = <?=$t?>;
            var settime<?=$task_wait_row['id']?>;
            clearTimeout(settime<?=$task_wait_row['id']?>);
            function processTimer<?=$task_wait_row['id']?>(){
                if (limit<?=$task_wait_row['id']?> > 0) {
                    settime<?=$task_wait_row['id']?> = setTimeout("processTimer<?=$task_wait_row['id']?>()",1000);
                    limit<?=$task_wait_row['id']?>--;
                } else {
                    return;
                }
                
                var limit_hou = Math.floor(limit<?=$task_wait_row['id']?> / 3600);
                if (limit_hou < 10) limit_hou = '0'+limit_hou;
                var limit_div = Math.floor(limit<?=$task_wait_row['id']?> / 60)%60;
                if (limit_div < 10) limit_div = '0'+limit_div;
                var limit_mod = Math.floor(limit<?=$task_wait_row['id']?> % 60);
                if (limit_mod < 10) limit_mod = '0'+limit_mod;
                
                // вывод времени
                el_timer = document.getElementById("timer<?=$task_wait_row['id']?>");
                if (el_timer) el_timer.innerHTML = limit_hou+':'+limit_div+":"+limit_mod;
            }
            processTimer<?=$task_wait_row['id']?>();
            </script>
        <div style="padding: 5px; border-bottom: 1px solid #E4E5E7; vertical-align: top;" id="task<?=$task_wait_row['id']?>">
            <div style="width: 80px; text-align: center; float: left; "><img src="<?=ROOT?>/temp/avatar/<?=$user_send['avatar']?>" width="60" height="60" style="border-radius: 50px; border: 1px solid #333;" /></div>
            <div style="width: 400px; float: left; margin: 5px; 0 0 5px;">
                <div>Исполнитель: <span style="color: #00AD76;"><?=$user_send['username']?></span> [ <span style="color: #999999;">IP: <?=$user_send['ip']?></span> ]</div>
                <div style=" padding: 5px; background: #EDEDED; min-height: 31px; word-wrap: break-word; margin: 5px 0 10px;">Отчет: <span style="color: #000; "><?=$task_wait_row['answer']?></span></div>
                <div style="color: #999999; text-align: right;">До автоматического одобрения осталось: <span id="timer<?=$task_wait_row['id']?>"></span></div>
			</div>
            <div id="refuse<?=$task_wait_row['id']?>" style="margin:5px; padding: 5px 0; text-align: right;; clear: both; display: none;">
				Причина: <input type="text" id="task_refuse<?=$task_wait_row['id']?>" class="val" style="text-align: left; width: 100px;" size="40" /> 
				<a href="#" class="btn_box" onclick="send_refuse(<?=$task_wait_row['id']?>, <?=$user_send['id']?>);return false;">Отправить</a>
			</div>
            <div id="revision<?=$task_wait_row['id']?>" style="margin:5px; padding: 5px 0; text-align: right;; clear: both; display: none;">
				Причина: <input type="text" id="task_revision<?=$task_wait_row['id']?>" class="val" style="text-align: left; width: 100px;" size="40" /> 
				<a href="#" class="btn_box" onclick="send_revision(<?=$task_wait_row['id']?>, <?=$user_send['id']?>);return false;">Отправить</a>
			</div>
            <div style="padding: 5px; text-align: right; clear: both; position: relative;">
				<a style="color: #333333;" href="#" class="a_box" onclick="tasks_black_user(<?=$task_wait_row['id']?>, <?=$user_send['id']?>);return false;">В черный список</a> | 
				<a style="color: #35AA47;" href="#" class="a_box" onclick="ok_tasks(<?=$task_wait_row['id']?>, <?=$user_send['id']?>);return false;">Принять</a> | 
				<a style="color: #B52413;" href="#" class="a_box" onclick="show_refuse(<?=$task_wait_row['id']?>);return false;">Отказать</a> | 
				<a style="color: #B52413;" href="#" class="a_box" onclick="show_revision(<?=$task_wait_row['id']?>);return false;">На доработку</a>
			</div>
        </div>
        <?
    }
}

?>