<?php

	include "library.php";

	$cxn = start_code( true );

	$cxn_common = db_common_login();

	$_SESSION['sitename'] = 'MPS Client Portal';

	$userid = val( $_SESSION, 'userid' );

    $practice = val( $_SESSION, "logged_in_practice" );
    
    
	if ($userid == "")
	{
		$_SESSION['nextpage'] = url_encode( "/" );

		jump_to_page( "login.php" );		/* must happen before print output */

		exit();
	}


    page_header( "", $cxn );
    
   
	$query = "SELECT * FROM request WHERE practice = '".$practice."' ORDER BY id DESC";

 	$result = run_query( $query, $cxn );

    if (query_num_rows($result) == 0)
    {
        echo black( "<br><br>You have no SOA's to list.<br><br>" );
    }
    else
    {
     	echo "<table width='100%'>";
     	
     	echo "<tr><td width='8%'>".black( "<i>Request ID</i>" )."</td><td width='8%'>".black( "<i>Date received</i>" )."</td><td width='8%'>".black( "<i>Date completed</i>" )."</td><td width='15%'>".black( "<i>Client names</i>" )."</td><td width='8%'>".black( "<i>Status</i>" )."</td><td width='8%'>".black( "<i>Submit</i>" )."</td><td width='8%'>".black( "<i>Edit</i>" )."</td><td width='8%'>".black( "<i>Attachments</i>" )."</td><td width='8%'>".black( "<i>Download</i>" )."</td><td width='25%'>&nbsp;</td></tr>";
    
        echo "<tr><td>&nbsp;</td></tr>";
        
        
    	for ($i=0; $i < query_num_rows($result); $i++)
    	{
    	    $status = "";
    	       
    		$data = get_row( $result );
        
            $query2 = "SELECT * FROM task WHERE request_id = ".$data['id'];
            
           	$result2 = run_query( $query2, $cxn );

            if (query_num_rows($result2) == 0)
            {
                $submitted = false;
            
                $status = "<span class='gred' style='font-weight: bold'>Not submitted yet</span>";
            }
            else
            {
                $data2 = get_row( $result2 );

                if ($data2['status'] == "received") 
                    $status = "<span class='ggreen'>Submitted</span>";
                else
                if ($data2['status'] == "quote_accepted") 
                    $status = "<span class='ggreen'>In progress</span>";
                else
                if ($data2['status'] == "completed") 
                    $status = "<span class='ggreen'>Completed</span>";
                else
                    $status = black( sentence_case( $data2['status'] ) );
                
                $submitted = true;
            }

            echo "<tr><td>".$data['id']."</td>";
            
            echo "<td>".ad_format_date( $data['date_entered'], "dd/mm/yyyy" )."</td>";

            if ($submitted)
                echo "<td>".ad_format_date( $data2['date_completed'], "dd/mm/yyyy" )."</td>";
            else
       		    echo "<td>&nbsp;</td>";
            
            echo "<td>".$data['client_names']."</td><td>".$status."</td>";

       		if ($submitted)
       		    echo "<td>&nbsp;</td>";
       		else
                echo "<td>".show_link( "Submit request", "submit_request_yes_no.php", "requestid", $data['id'] )."</td>";

            echo "<td>&nbsp;".show_link( "Edit", "edit_all_data.php", "formname", "request", "newrecord", "false", "id", $data['id'], "shownextprevious", "false", "readonly", "false" )."&nbsp;</td>";

            echo "<td>&nbsp;".show_link( "Attachments", "attachments.php", "requestid", $data['id'] )."&nbsp;</td>";

            echo "<td>".show_link( "Download Area", "download_area.php", "requestid", $data['id'] )."</td>";


            echo "</tr>";
            
    	}
    	
    	echo "</table>";
    }	
	
?>