<?php

	include "library.php";

	$cxn = start_code();
	
	$cxn_common = db_common_login();


	$pathroot = "/home/academi1/public_html/melbourneparaplanning/clients";


	$task_id = val( $_POST, "task_id" );


		/* start the page  */

	page_header( "Request Attachments", $cxn );

    echo "<br>";

    if ($task_id == "")
    {
        echo "<br>".black( "Task ID is blank" )."<br><br><br>";

        echo show_home_link();
    }
    else
    {
    	$query = "SELECT * FROM task WHERE id = ".$task_id;
        
    	$result = run_query( $query, $cxn );
    
        if (query_num_rows( $result ) == 0)
        {
            echo "<br>".black( "Task ID ".$task_id." not found" )."<br><br><br>";
    
            show_home_link();
        }
        else
        {
        	$data = get_row( $result );
        
            $request_id = $data['request_id'];
        
        
        	echo "<table><tr><td>".green( "Client name(s):&nbsp;&nbsp;" )."</td><td>".green( $data['client_names'] )."</td></tr>";
        
        //	echo "<tr><td>".black( "Practice" )."</td><td>".black( $data['practice'] )."</td></tr>";
        
        //	echo "<tr><td>".black( "Adviser" )."</td><td>".black( $data['adviser1'] )."</td></tr>";
        
        	echo "</table><br><br>";	
        
        		
        	$random_file_number = $data['random_file_number'];
        
        
        		/* retrieve existing files */
        
        	$query = "SELECT * FROM attachments WHERE request_id = (SELECT request_id FROM task WHERE id = ".$task_id.")";
        
        	$result = run_query( $query, $cxn );
        
        	$data = get_row( $result );
        
        
            
        	if (query_num_rows($result) == 0)
        		echo red( "No files currently stored" );
        
        	echo "<br><table><tr>";
        
        
        	$result = run_query( $query, $cxn );
        
        	for ($i=0;$i <  query_num_rows($result);$i++)
        	{
        		$data = get_row( $result );
        
        		$file_path = "/attachments/request_".$request_id."_".$random_file_number."/".$data['filename'];
        
        		$modified_datetime = date( "d M Y H:i:s", filemtime( $pathroot.$file_path ) );
        
        		echo "<tr><td align='left'><a href='https://melbourneparaplanning.com/clients".$file_path."'>".black( $data['filename'] )."</a></td><td>&nbsp;".black( $modified_datetime );
        
        //		echo "&nbsp;&nbsp;&nbsp;".show_link( "rename", "get_new_task_filename.php", "taskid", $id, "taskfileid", $data['id'] );		
        	    
        	    echo "</td></tr>";
        	}
        
        	echo "</tr><tr>";
        
        	echo "</tr></table><br><br><br>";
        }
    }
    
    
	echo "<br>";
	echo "<br>";
	echo "<br>";
	echo "<br>";
	echo "<br>";
	echo "<br>";
	echo "<br>";
	

?>