<div class="row">	
		
	 <section class="content">
     
	<?php echo '<div class="alert alert-warning fade in" >
			  <a href = "#" class = "close" data-dismiss= "alert"> &times;</a>
  				<strong>Nota&nbsp;</strong> &nbsp;&nbsp;Preencha todos os campos antes de atualizar os dados!
				</div>'?>
	        <div class="box box-success">
            <div class="box-body">
              <div class="table-responsive">
             <div class="box-body">

			 <div class="col-md-14">
             <div class="nav-tabs-custom">
             <ul class="nav nav-tabs">
              <li><a href="#tab_1" data-toggle="tab">Informacoes de Emprestimo</a></li>
              <!--<li ><a href="#tab_2" data-toggle="tab">Additional Fees</a></li>-->
              <li class="active"><a href="#tab_3" data-toggle="tab">Anexos</a></li>
              <!--<li><a href="#tab_4" data-toggle="tab">Collateral</a></li>-->
              <!--<li><a href="#tab_5" data-toggle="tab">Payment Schedule</a></li>-->
              </ul>
             <div class="tab-content">
             <div class="tab-pane" id="tab_1">
<?php
$id = $_GET['id'];
$select = mysqli_query($link, "SELECT * FROM loan_info WHERE id = '$id'") or die (mysqli_error($link));
while($row = mysqli_fetch_array($select))
{
$borrower = $row['borrower'];   
?>           
			 <form class="form-horizontal" method="post" action="">
			  <?php echo '<div class="alert alert-info fade in" >
			  <a href = "#" class = "close" data-dismiss= "alert"> &times;</a>
  				<strong>Note that&nbsp;</strong> &nbsp;&nbsp;Some Fields are Rquired.
				</div>'?>
             <div class="box-body">
				
				 
		<div class="form-group">
                  <label for="" class="col-sm-2 control-label" style="color:#009900">Valor</label>
                  <div class="col-sm-10">
                  <input name="amount" type="number" class="form-control" value="<?php echo $row['amount']; ?>" step="0.01" required>
                  </div>
                  </div>
		
		 <div class="form-group">
                  	<label for="" class="col-sm-2 control-label" style="color:#009900">Descricao</label>
                  	<div class="col-sm-10">
					<textarea name="desc"  class="form-control" rows="4" cols="80"><?php echo $row['desci']; ?></textarea>
           			 </div>
					 </div>
		
		 <div class="form-group">
                <label for="" class="col-sm-2 control-label" style="color:#009900">Data de Aprovacao</label>
			 <div class="col-sm-10">
              <div class="input-group date">
                  <div class="input-group-addon">
                    <i class="fa fa-calendar"></i>
                  </div>
                  <input name="date_release" type="text" class="form-control pull-right" value="<?php echo $row['date_release']; ?>" required>
                </div>
              </div>
			  </div>
	
				  					
			<div class="form-group">
                  	<label for="" class="col-sm-2 control-label" style="color:#009900">Comentarios</label>
                  	<div class="col-sm-10">
					<textarea name="remarks"  class="form-control" rows="4" cols="80"><?php echo $row['remarks']; ?></textarea>
           			 </div>
          	</div>



<hr>	
<div class="alert-danger">&nbsp;INFORMACOES DE PAGAMENTO</div>
<hr>	
				
				  
				   <div class="form-group">
                <label for="" class="col-sm-2 control-label" style="color:#009900">Data de Pagamento</label>
			 <div class="col-sm-10">
              <div class="input-group date">
                  <div class="input-group-addon">
                    <i class="fa fa-calendar"></i>
                  </div>
                  <input name="pay_date" type="text" class="form-control pull-right" value="<?php echo $row['pay_date']; ?>" required>
                </div>
              </div>
			  </div>
				  
				  <div class="form-group">
                  <label for="" class="col-sm-2 control-label" style="color:#009900">Valor a pagar</label>
                  <div class="col-sm-10">
                  <input name="amount_topay" type="number" class="form-control" value="<?php echo $row['amount_topay']; ?>" required>
                  </div>
                  </div>
				  

				<div class="form-group">
                  	<label for="" class="col-sm-2 control-label" style="color:#009900">Comentarios</label>
                  	<div class="col-sm-10">
					<textarea name="remark"  class="form-control" rows="4" cols="80"><?php echo $row['remark']; ?></textarea>
           			 </div>
          	</div>
          	<div align="right">
              <div class="box-footer">
                				<button type="reset" class="btn btn-primary btn-flat"><i class="fa fa-times">&nbsp;Limpar</i></button>
                				<button name="saveUp" type="submit" class="btn btn-success btn-flat"><i class="fa fa-save">&nbsp;Gravar</i></button>

              </div>
			  </div>
				  
			 </div>
			 
			  </form>


<?php
if(isset($_POST['saveUp'])){
$idm = $_GET['id'];
$desc = mysqli_real_escape_string($link, $_POST['desc']);
$amount = mysqli_real_escape_string($link, $_POST['amount']);
$date_release = mysqli_real_escape_string($link, $_POST['date_release']);
$remarks = mysqli_real_escape_string($link, $_POST['remarks']);
$pay_date = mysqli_real_escape_string($link, $_POST['pay_date']);
$amount_topay = mysqli_real_escape_string($link, $_POST['amount_topay']);
$remark = mysqli_real_escape_string($link, $_POST['remark']);

$sql = "UPDATE loan_info SET `desci` = '$desc', `amount` = '$amount', `date_release` = '$date_release', `remarks` = '$remarks', `pay_date` = '$pay_date', `amount_topay` = '$amount_topay', `remark` = '$remark' WHERE `id` = '$idm'";

$update = mysqli_query($link, $sql) or die (mysqli_error($link));
if(!$update)
{
echo "<script>alert('Erro ao atualizar emprestimo!'); </script>";
echo "<script>window.location='listloans.php?id=".$idm."&&mid=".base64_encode("405")."'; </script>";
}
else{
echo "<script>alert('Emprestimo atualizado com Sucesso!!'); </script>";
echo "<script>window.location='listloans.php?id=".$idm."&&mid=".base64_encode("405")."'; </script>";
}
}
?>







<?php } ?>
              </div>

              <div class="tab-pane active" id="tab_3">
                <form class="form-horizontal" method="post" enctype="multipart/form-data">

                Anexos
Ficheiros aceites <span style="color:#FF0000">jpg, gif, png, xls, xlsx, csv, doc, docx, pdf</span>
			 <input name="uploaded_file" type="file" class="btn btn-info">
			 <div align="left">
              <div class="box-footer">
                				<button type="submit" class="btn btn-success btn-flat" name="upload"><i class="fa fa-upload">&nbsp;Upload</i></button>
              </div>
			  </div>
			  <table id="example1" class="table table-bordered table-striped">
                 <thead>
                     <th>Descricao do ficheiro</th>
                     <th>Download</th>
                     <!--<th>Apagar</th>-->
                 </thead>
                 <tbody>
                     <?php
                     $id = $_GET['id'];
$se = mysqli_query($link, "SELECT * FROM attachment WHERE get_id = '$id'") or die (mysqli_error($link));
while($gete = mysqli_fetch_array($se))
{
    $n = $gete['attached_file'];
    $ns = substr($n, 9);
?>
				<tr>
				    <td><?php echo $ns;?></td>
				     <td><?php echo ($pread == '1') ? '<a href="'.$gete['attached_file'].'"><button type="button" class="btn btn-flat btn-success"><i class="fa fa-download"></i>&nbsp;Baixar</button></a>' : ''; ?></td>
				     <td>
				         <form action="" method="POST">
				             <input type="hidden" name="attfile" value="<?php echo $gete['attached_file'];?>">
				             <button type="submit" style="cursor: pointer;padding: 7px 10px;border: none;border-radius:2px;background-color: #428BCA;color: #fff;" name="apagar">
				                 <i class="fa fa-trash" aria-hidden="true"></i>&nbsp;<span>Apagar</span>
				             </button>
				         </form>
				     </td>
				     
				</tr>
<?php } ?>  
                 </tbody>
              </table>
              
<?php 
if(isset($_POST['apagar'])){
    $ficheiro = $_POST['attfile'];
    $id = $_GET['id'];
    $sql = "DELETE FROM attachment WHERE get_id = '$id' AND attached_file = '$ficheiro'";
    $delet = mysqli_query($link, $sql) or die (mysqli_error($link));
    if(!$delet)
{
echo "<script>alert('Erro ao apagar anexo!'); </script>";
echo "<script>window.location='updateloans.php?id=".$id."&&mid=".base64_encode("405")."'; </script>";
}
else{
echo "<script>alert('Aenxo apagado com Sucesso!!'); </script>";
echo "<script>window.location='updateloans.php?id=".$id."&&mid=".base64_encode("405")."'; </script>";
unlink('$ficheiro');
}
    
}
?>


<?php
if(isset($_POST['upload']))
{
$id = $_GET['id'];
$tid = $_SESSION['tid'];

//upload random name/number
	 $rd2 = mt_rand(1000,9999)."_File"; 
	 
	 //Check that we have a file
	if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
  //Check if the file is JPEG image and it's size is less than 350Kb
  $filename = basename($_FILES['uploaded_file']['name']);
  
  $ext = substr($filename, strrpos($filename, '.') + 1);
  
  if (($ext != "exe") && ($_FILES["uploaded_file"]["type"] != "application/x-msdownload"))  {
    //Determine the path to which we want to save this file      
	  //$newname = dirname(__FILE__).'/upload/'.$filename;
	  $newname="document/".$rd2."_".$filename;      
	  //Check if the file with the same name is already exists on the server
 
        //Attempt to move the uploaded file to it's new place
        if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
			//successful upload
          // echo "It's done! The file has been saved as: ".$newname;		   

$insert = mysqli_query($link, "INSERT INTO attachment(get_id,tid,attached_file,date_time) VALUES('$id','$tid','$newname',NOW())") or die (mysqli_error($link));
if(!$insert)
{
echo "<script>alert('Documentos nao inseridos.....Por favor tente novamente!'); </script>";
echo "<script>window.location='updateloans.php?id=".$id."&&mid=".base64_encode("405")."'; </script>";
}
else{
echo "<script>alert('Documentos inseridos com sucesso!!'); </script>";
echo "<script>window.location='updateloans.php?id=".$id."&&mid=".base64_encode("405")."'; </script>";
}
}
}
}
}
?>
			 </form> 
              </div>
			              
              <!-- /.tab-pane -->
            </div>
            <!-- /.tab-content -->
          </div>
				 
					
					
				
				
				</div>
				

              </div>
			 

	
</div>	
</div>
</div>	
</div>	