<?php

include_once "../inc/db.inc";
include_once "../inc/util.inc";
include_once "../inc/db_ops.inc";
include_once "../inc/util_ops.inc";
include_once "../inc/prefs.inc";
include_once "../inc/queue.inc";
$timestr = time_str(time(0));
db_init();
$workunitid = get_int('workunitid');
$workunit = mysql_fetch_object(mysql_query("SELECT * FROM workunit WHERE id=" . $workunitid));
$job = mysql_fetch_object(mysql_query("SELECT * FROM q_list WHERE workunit=" . $workunit->id));
$user = mysql_fetch_object(mysql_query("SELECT * FROM user WHERE id=" . $job->user));
$title = "Job '" . workunit_name($workunit) . "' (" . $workunitid . ") of " . $user->name . " at " . $timestr;
$jobname = workunit_name($workunit);
$config = get_config();
$jobapplication = mysql_fetch_object(mysql_query("SELECT * FROM app WHERE id=" . $workunit->appid));
$jobapplicationname = $jobapplication->name;
$jobapplicationfriendlyname = $jobapplication->user_friendly_name;
$jobfops = $workunit->rsc_fpops_est;
$jobmem = $workunit->rsc_memory_bound;
$jobdisk = $workunit->rsc_disk_bound;
$jobstatusstring = workunit_status_string($workunit);
$coloredjobstatusstring = $jobstatusstring;
if ($jobstatusstring == "running") {
    $coloredjobstatusstring = "<font color='green'><b>" . $jobstatusstring . "</b></font>";
}
if ($jobstatusstring == "queued") {
    $coloredjobstatusstring = "<font color='blue'><b>" . $jobstatusstring . "</b></font>";
}
 if ($njobs > 1) {
     row1("You have " . $njobs . " jobs listed !<br>");
 } else {
     row1("You have " . $njobs . " job listed !<br>");
 }
 end_table();
 start_table();
 row5("<b>Job #</b>", "<b>Job submit time</b>", "<b>Job status</b>", "<b>Job name</b>", "<b>Job ID</b>");
 for ($jobindex = 0; $jobindex < $njobs; ++$jobindex) {
     $job = mysql_fetch_object($alljobs);
     $workunitquery = mysql_query("SELECT * FROM workunit WHERE id=" . $job->workunit);
     if ($workunitquery) {
         $workunit = mysql_fetch_object($workunitquery);
         if ($workunit) {
             $prefix = '<a href="queue_show_job.php?workunitid=' . $job->workunit . '">';
             $workunitname = $prefix . workunit_name($workunit) . '</a>';
             $status = workunit_status_string($workunit);
             if ($status == "running") {
                 $status = "<font color='green'><b>" . $status . "</b></font>";
             }
             if ($status == "queued") {
                 $status = "<font color='blue'><b>" . $status . "</b></font>";
             }
             if ($status == "ERROR") {
                 $status = "<font color='red'><b>" . $status . "</b></font>";
             }
             $jobsubmittime = time_str($workunit->create_time);
         } else {
             $workunitname = "<font color='red'>WORKUNIT NOT FOUND IN DATABASE</font>";
             $status = "<font color='red'>UNKNOWN</font>";
             $jobsubmittime = "<font color='red'>UNKNOWN</font>";
    start_table();
    row6("<b>Job #</b>", "<b>User</b>", "<b>Job submit time</b>", "<b>Job status</b>", "<b>Job name</b>", "<b>Job ID</b>");
    for ($jobindex = 0; $jobindex < $njobs; ++$jobindex) {
        $workunit = mysql_fetch_object($alljobs);
        $prefix = '<a href="queue_show_job.php?workunitid=' . $workunit->id . '">';
        $workunitname = $prefix . workunit_name($workunit) . '</a>';
        $workunitidstr = "<a href='db_action.php?table=workunit&id=" . $workunit->id . "'>" . $workunit->id . "</a>";
        $status = workunit_status_string($workunit);
        $jobsubmittime = time_str($workunit->create_time);
        if ($status != "CANCELED") {
            if ($status == "running") {
                $status = "<font color='green'><b>" . $status . "</b></font>";
            }
            if ($status == "queued") {
                $status = "<font color='blue'><b>" . $status . "</b></font>";
            }
            $job = mysql_fetch_object(mysql_query("SELECT * FROM q_list WHERE workunit=" . $workunit->id));
            $user = mysql_fetch_object(mysql_query("SELECT * FROM user WHERE id=" . $job->user));
            $jobusername = "******" . $user->id . "'>" . $user->name . "</a>";
        } else {
            $jobusername = "******";
            $status = "<font color='red'><b>CANCELED</b></font>";
            $workunitname = workunit_name($workunit);
        }
        row6($jobindex + 1, $jobusername, $jobsubmittime, $status, $workunitname, $workunitidstr);
    }
} else {
    row1("There are NO jobs listed !<br>");
}
end_table();
admin_page_tail();