<?php

if(isset($_GET['id'])) {
	
		$database->setBareQuery("SELECT name, parent, project_num, project_year, offerno FROM project WHERE id = '".$_GET['id']."'");
	
		$name = $database->loadResultArray(0);
		
		$parent = $database->loadResultArray(1);
		
		$projectnum = $database->loadResultArray(2);
		
		$projectyr = $database->loadResultArray(3);
		
		$offerno = $database->loadResultArray(4);
		
		if($projectnum[0] < 100){
			
			if($projectnum[0] < 10){ $visiblenum = '00'.$projectnum[0];}
			
			else { $visiblenum = '0'.$projectnum[0];}
			
			}
			
			else { $visiblenum = $projectnum[0];}
	

				
				?>

<h1>Izmena projekta</h1>
<div id="uvbody-content">
    <form id="edit-project" class="" method="post" action="">
    <input type="hidden" name="id" id="id" value="<?=$_GET['id']?>" />
        <div id="edit">
            <div id="edit-content">
                <input type="text" name="projectname" id="projectname" value="<?=$name[0]?>" />
                <label>Broj projekta</label>
                <input type="text" name="projectnum" id="projectnum" value="<?=$projectyr[0].$visiblenum?>" />
                <label>Klijent</label>
                <select id="clientset" name="clientset">

            <?php 
				
				$database->setBareQuery("SELECT id, name FROM client ORDER BY name");

				$clientid = $database->loadResultArray(0);
		
				$clientname = $database->loadResultArray(1);
		
				for($z = 0; $z < count($clientid); $z++): 
			
			?>
            <option value="<?=$clientid[$z]?>"<?php if($clientid[$z] == $parent[0]) { echo " selected = 'selected'";}?>><?=$clientname[$z]?></option>
            
            <?php endfor; ?>
            
		</select>
        
        <label>Ponuda</label>
                <input type="text" name="offerno" id="offerno" value="<?=$offerno[0]?>" />
                 <input type="submit" id="user-edit-submit-2" value="Snimi" />
            </div>
        </div>      
        <div class="clear"></div>
    </form>
</div>

<?php

}

else {
	
	if(isset($_GET['delete'])){

	
		$database->query("DELETE FROM task WHERE project = '".$_GET['delete']."'");
	

	
	$database->query("DELETE FROM project WHERE id='".$_GET['delete']."' LIMIT 1");
	
}


	
$database->setBareQuery("SELECT id, name FROM client WHERE id = '".$_GET['clientid']."' LIMIT 1");

				$clientid = $database->loadResultArray(0);
		
				$clientname = $database->loadResultArray(1);
	


$database->setBareQuery("SELECT id, name, project_num, project_year, offerno FROM project WHERE parent = '".$_GET['clientid']."' ORDER BY project_year DESC, project_num DESC");

				$projectid = $database->loadResultArray(0);
		
				$projectname = $database->loadResultArray(1);
				
				$projectnum = $database->loadResultArray(2);
				
				$projectyr = $database->loadResultArray(3);
				
				$offerno = $database->loadResultArray(4);
				
				?>

<h1>Projekti <?=$clientname[0]?></h1>
<div id="uvbody-content">
	<table id="table" cellspacing="0">
		<thead>
			<tr>
				<th scope="col" class="manage-title">Ime</th>
                <th scope="col" class="manage-offer">Ponuda</th>
				<th scope="col" class="manage-action">Akcija</th>
                
			</tr>
		</thead>
		<tbody>
		<?php 
		for($i = 0; $i < count($projectid); $i++):
		
		if($projectnum[$i] < 100){
			
			if($projectnum[$i] < 10){ $visiblenum = '00'.$projectnum[$i];}
			
			else { $visiblenum = '0'.$projectnum[$i];}
			
			}
			
			else { $visiblenum = $projectnum[$i];}
    		?>
    			<tr id="page-<?=$projectid[$i]?>">
    				<td><a href="admin.php?page=tools&action=project-edit&clientid=<?=$_GET['clientid']?>&id=<?=$projectid[$i]?>"><?=$projectyr[$i].$visiblenum.' '.$projectname[$i]?></a></td>
                    <td><?=$offerno[$i]?></td>
    				<td><a href="admin.php?page=tools&action=project-edit&clientid=<?=$_GET['clientid']?>&id=<?=$projectid[$i]?>" class="edit">Edit</a> | <a href="admin.php?page=tools&action=project-edit&clientid=<?=$_GET['clientid']?>&delete=<?=$projectid[$i]?>" class="delete">Delete</a></td>
                    
    			</tr>
    		<?php

		endfor;
		?>		
		</tbody>
		<tfoot>
			<tr>
				<th scope="col" class="manage-title">Ime</th>
                <th scope="col" class="manage-offer">Ponuda</th>	
				<th scope="col" class="manage-action">Akcija</th>
                
			</tr>
		</tfoot>
	</table>
</div>
<?php

}


 ?>