<?php
 include('include/headscript.php'); ?>
<?php
if(!empty($_FILES['doc'])){
    // File upload configuration
	if (isset($_POST["idtrf"])) {
	$idtrf=$_POST["idtrf"]; }
	if (isset($_POST["filedescription"])) {
	$filedescription=$_POST["filedescription"]; }
	else { $filedescription=""; }
    $targetDir = "uploaddocuments/";
    $allowTypes = array('pdf','doc','xls','xlsx','docx','jpg');
    
    $docss_arr = array();
    foreach($_FILES['doc']['name'] as $key=>$val){
        $doc_name = $_FILES['doc']['name'][$key];
        $tmp_name   = $_FILES['doc']['tmp_name'][$key];
        $size       = $_FILES['doc']['size'][$key];
        $type       = $_FILES['doc']['type'][$key];
        $error      = $_FILES['doc']['error'][$key];
        
        // File upload path
		$code=time();
        $fileName = basename($_FILES['doc']['name'][$key]);
		$fileName=$idtrf.'-'.$code.$fileName;
        $targetFilePath = $targetDir . $fileName;
        
		
		
        // Check whether file type is valid
        $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
        if(in_array($fileType, $allowTypes)){    
            // Store images on the server
            if(move_uploaded_file($_FILES['doc']['tmp_name'][$key],$targetFilePath)){
                $docs_arr[] = $targetFilePath;
				
  $InsertQuery = new WA_MySQLi_Query($cmctrfdb);
  $InsertQuery->Action = "insert";
  $InsertQuery->Table = "fileattached";
  $InsertQuery->bindColumn("idtrfdetails", "i", "$idtrf", "WA_DEFAULT");
  $InsertQuery->bindColumn("filename_fileattached", "s", "$fileName", "WA_DEFAULT");
  $InsertQuery->bindColumn("description_fileattached", "s", "$filedescription", "WA_DEFAULT");
  $InsertQuery->saveInSession("");
  $InsertQuery->execute();
  $InsertGoTo = "";
  $InsertQuery->redirect($InsertGoTo);
            }
        }
    }
    
    // Generate gallery view of the images
    /*if(!empty($docs_arr)){ ?>
        <ul>
        <?php foreach($docs_arr as $doc_src){ ?>
                <!-- SHOW LINK -->
                <a href="<?php echo $doc_src; ?>" target="_blank"><?php echo $doc_src; ?></a>
        <?php } ?>
        </ul>
    <?php }*/
    // LIST ALL UPLOADED FILES INSIDE TABLE
    $filenamelist = new WA_MySQLi_RS("filenamelist",$cmctrfdb,0);
    
    $filenamelist->setQuery("SELECT * FROM fileattached WHERE fileattached.idtrfdetails='$idtrf'");
    $filenamelist->execute();
    while(!$filenamelist->atEnd()) {
        $wa_startindex = $filenamelist->Index;
?>
        <tr>
            <td>
                <a href="uploaddocuments/<?php echo($filenamelist->getColumnVal("filename_fileattached")); ?>" target="_blank"><?php echo $filesent; ?></a>
            </td>
            <td>
                
                <?php echo($filenamelist->getColumnVal("description_fileattached")); ?>
            </td>
            <td>
                
                <a href="deleteaddedoc.php?idtrf=<?php echo $idtrf; ?>&idadddoc=<?php echo($filenamelist->getColumnVal("idfileattached")); ?>"><i class="fas fa-trash-alt text-danger font-16"></i></a>
    
            </td>
        </tr>
<?php
        $filenamelist->moveNext();
    }
}
?>