<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>Atenção&nbsp;</strong> &nbsp;&nbsp;Deve marcar a opção correta
				</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">Informações de Mutuario</a></li>
              <!--<li class="active"><a href="#tab_2" data-toggle="tab">Informação Financeira</a></li>-->
              <li class="active"><a href="#tab_3" data-toggle="tab">Anexos</a></li>
              </ul>
             <div class="tab-content">
             <div class="tab-pane" id="tab_1">
<?php
$id = $_GET['id'];
$select = mysqli_query($link, "SELECT * FROM borrowers WHERE id = '$id'") or die (mysqli_error($link));
while($row = mysqli_fetch_array($select))
{   
?>              
			 <form class="form-horizontal" method="post" enctype="multipart/form-data" action="">
             <div class="box-body">
				
			<!--<div class="form-group">-->
   <!--         <label for="" class="col-sm-2 control-label">Sua Imagem</label>-->
			<!--<div class="col-sm-10">-->
  	<!--	  			 <input type='file' name="image" onChange="readURL(this);" />-->
   <!--    				 <img id="blah"  src="../<?php echo $row['image']; ?>" alt="Image Here" height="100" width="100"/>-->
			<!--</div>-->
			<!--</div>-->
			
			<div class="form-group">
                  <label for="" class="col-sm-2 control-label" style="color:#009900">Nome</label>
                  <div class="col-sm-10">
                  <input name="fname" type="text" class="form-control" value="<?php echo $row['fname']; ?>" required>
                  </div>
                  </div>
				  
		<div class="form-group">
                  <label for="" class="col-sm-2 control-label" style="color:#009900">Apelido</label>
                  <div class="col-sm-10">
                  <input name="lname" type="text" class="form-control"  value="<?php echo $row['lname']; ?>" required>
                  </div>
                  </div>
                  <div class="form-group">
                          <label for="" class="col-sm-2 control-label" style="color:#009900">Data de Nascimento</label>
                        <div class="col-sm-10">
                          <input name="data_nascimento" type="text" class="form-control" value="<?php echo $row['data_nascimento']; ?>" required>
                        </div>
                    </div>
                    <div class="form-group">
                          <label for="" class="col-sm-2 control-label" style="color:#009900">Sexo</label>
                        <div class="col-sm-10">
                          <input name="sexo" type="text" class="form-control" value="<?php echo $row['sexo']; ?>" required>
                        </div>
                    </div>
				  <div class="form-group">
                          <label for="" class="col-sm-2 control-label" style="color:#009900">Naturalidade</label>
                        <div class="col-sm-10">
                          <input name="naturalidade" type="text" class="form-control" value="<?php echo $row['naturalidade']; ?>" required>
                        </div>
                    </div>
                    <div class="form-group">
                          <label for="" class="col-sm-2 control-label" style="color:#009900">Filiacao</label>
                        <div class="col-sm-10">
                          <input name="filiacao" type="text" class="form-control" value="<?php echo $row['filiacao']; ?>" required>
                        </div>
                    </div>
                    <div class="form-group">
                          <label for="" class="col-sm-2 control-label" style="color:#009900">Estado Civil</label>
                        <div class="col-sm-10">
                          <input name="estado_civil" type="text" class="form-control" value="<?php echo $row['estado_civil']; ?>" required>
                        </div>
                    </div>
                    <div class="form-group">
                          <label for="" class="col-sm-2 control-label" style="color:#009900">Local de Residencia</label>
                        <div class="col-sm-10">
                          <input name="residencia" type="text" class="form-control" value="<?php echo $row['residencia']; ?>" required>
                        </div>
                    </div>
                    <div class="form-group">
                          <label for="" class="col-sm-2 control-label" style="color:#009900">Documento</label>
                        <div class="col-sm-10">
                          <input name="documento" type="text" class="form-control" value="<?php echo $row['documento']; ?>" required>
                        </div>
                    </div>
                    <div class="form-group">
                          <label for="" class="col-sm-2 control-label" style="color:#009900">Numero do Documento</label>
                        <div class="col-sm-10">
                          <input name="numero_documento" type="text" class="form-control" value="<?php echo $row['numero_documento']; ?>" required maxlength="13" min="0">
                        </div>
                    </div>
                    <div class="form-group">
                          <label for="" class="col-sm-2 control-label" style="color:#009900">Nuit</label>
                        <div class="col-sm-10">
                          <input name="nuit" type="text" class="form-control" value="<?php echo $row['nuit']; ?>" required maxlength="9" min="0">
                        </div>
                    </div>
                    <div class="form-group">
                          <label for="" class="col-sm-2 control-label" style="color:#009900">Data da Emissao</label>
                        <div class="col-sm-10">
                          <input name="emissao" type="text" class="form-control" value="<?php echo $row['emissao']; ?>" required>
                        </div>
                    </div>
                    <div class="form-group">
                          <label for="" class="col-sm-2 control-label" style="color:#009900">Validade</label>
                        <div class="col-sm-10">
                          <input name="validade" type="text" class="form-control" value="<?php echo $row['validade']; ?>" required>
                        </div>
                    </div>
                    <div class="form-group">
                          <label for="" class="col-sm-2 control-label" style="color:#009900">Local da Emissao</label>
                        <div class="col-sm-10">
                          <input name="localEmit" type="text" class="form-control" value="<?php echo $row['localEmit']; ?>" required>
                        </div>
                    </div>
            
		<div class="form-group">
                  <label for="" class="col-sm-2 control-label" style="color:#009900">Email</label>
                  <div class="col-sm-10">
                  <input type="email" name="email" type="text" class="form-control"  value="<?php echo $row['email']; ?>" required>
                  </div>
                  </div>
				  
		<div class="form-group">
                  <label for="" class="col-sm-2 control-label" style="color:#009900">Telemovel</label>
                  <div class="col-sm-10">
                  <input name="phone" type="text" class="form-control" value="<?php echo $row['phone']; ?>" required>
                  </div>
                  </div>

                    <div class="form-group">
                          <label for="" class="col-sm-2 control-label" style="color:#009900">Distrito</label>
                        <div class="col-sm-10">
                          <input name="distrito" type="text" class="form-control" value="<?php echo $row['distrito']; ?>" required>
                        </div>
                    </div>
			<div class="form-group">
                  <label for="" class="col-sm-2 control-label" style="color:#009900">Cidade</label>
                  <div class="col-sm-10">
                  <input name="state" type="text" class="form-control"  value="<?php echo $row['state']; ?>" required>
                  </div>
                  </div>
				  
		<div class="form-group">
                  <label for="" class="col-sm-2 control-label" style="color:#009900">Provincia</label>
                  <div class="col-sm-10">
                  <input name="state" type="text" class="form-control"  value="<?php echo $row['state']; ?>" required>
                  </div>
                  </div>
				  
				  <div class="form-group">
                  <label for="" class="col-sm-2 control-label" style="color:#009900">Codigo Postal</label>
                  <div class="col-sm-10">
                  <input name="zip" type="text" class="form-control" value="<?php echo $row['zip']; ?>">
                  </div>
                  </div>
				  
		<div class="form-group">
                          <label for="" class="col-sm-2 control-label" style="color:#009900">Pais</label>
                        <div class="col-sm-10">
                          <input name="country" type="text" class="form-control" value="<?php echo $row['country']; ?>" required>
                        </div>
                    </div>
									 
					<div class="form-group">
                  <label for="" class="col-sm-2 control-label" style="color:#009900">Categoria de Beneficiario</label>
                  <div class="col-sm-10">
                  <input name="beneficiario" type="text" class="form-control"  value="<?php echo $row['beneficiario']; ?>" required>
                  </div>
                  </div>
                  <div class="form-group">
                  <label for="" class="col-sm-2 control-label" style="color:#009900">Membros do Grupo</label>
                  <div class="col-sm-10">
                  <input name="membros" type="text" class="form-control"  value="<?php echo $row['membros']; ?>">
                  </div>
                  </div>
				<div class="form-group">
                  	<label for="" class="col-sm-2 control-label" style="color:#009900">Comentarios</label>
                  	<div class="col-sm-10">
					<input type="text" name="comment"  class="form-control"  value="<?php echo $row['comment']; ?>">
           			 </div>
          	</div>
			
			 

			 </div>
			 
			  <div align="right">
              <div class="box-footer">
                				<button type="reset" class="btn btn-primary btn-flat"><i class="fa fa-times">&nbsp;Reset</i></button>
                				<button name="save" type="submit" class="btn btn-success btn-flat"><i class="fa fa-save">&nbsp;Gravar</i></button>

              </div>
			  </div>
			  
			 </form> 
<?php } ?>
              </div>
              <!-- /.tab-pane -->
              
              <!-- /.tab-pane -->
              	<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 style="text-align:left;padding: 10px 2px;">
                  <label>Descricao</label>
                  <input name="desc" type="text" style="width: 40%;">
                  </div>
			 <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>
              <table id="example1" class="table table-bordered table-striped">
                 <thead>
                     <th>Descricao do ficheiro</th>
                     <th>Ficheiro</th>
                     <th>Download</th>
                     <!--<th>Apagar</th>-->
                 </thead>
                 <tbody>
                     <?php
                     $id = $_GET['id'];
$se = mysqli_query($link, "SELECT * FROM battachment WHERE get_id = '$id'") or die (mysqli_error($link));
while($gete = mysqli_fetch_array($se))
{
    $n = $gete['attached_file'];
    $ns = substr($n, 10);
?>
				<tr>
				    <td><?php echo $gete['descricao'];?></td>
				    <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>
			  </div>
<?php
if(isset($_POST['upload']))
{
$id = $_GET['id'];
$desc = $_POST['desc'];
$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="bdocument/".$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 battachment(get_id,tid,attached_file,descricao,date_time) VALUES('$id','$tid','$newname','$desc',NOW())") or die (mysqli_error($link));
$insert = mysqli_query($link, "UPDATE borrowers SET status = 'Completo' WHERE id = '$id'") or die (mysqli_error($link));
if(!$insert)
{
echo "<script>alert('Ficheiro nao inserido.....Por favor tente novamente!'); </script>";
}
else{
echo "<script>alert('Documentos adcionados com Sucesso!!'); </script>";
echo "<script>window.location='listborrowers.php?id=".$_SESSION['tid']."'; </script>";
}
}
}
}
}
?>
			 </form> 
              </div>
              <!-- /.tab-pane -->
            </div>
            <!-- /.tab-content -->
          </div>
				 
					
					
				
				
				</div>
				

              </div>
			 
<?php 
if(isset($_POST['apagar'])){
    $ficheiro = $_POST['attfile'];
    $id = $_GET['id'];
    $sql = "DELETE FROM battachment 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>";
}
else{
echo "<script>alert('Aenxo apagado com Sucesso!!'); </script>";
echo "<script>window.location='listborrowers.php?id=".$_SESSION['tid']."'; </script>";
unlink('$ficheiro');
}
    
}
?>
	
<?php
if(isset($_POST['save']))
{
$id = $_GET['id'];
$fname =  mysqli_real_escape_string($link, $_POST['fname']);
$lname = mysqli_real_escape_string($link, $_POST['lname']);
$data_nascimento = mysqli_real_escape_string($link, $_POST['data_nascimento']);
$sexo = mysqli_real_escape_string($link, $_POST['sexo']);
$naturalidade = mysqli_real_escape_string($link, $_POST['naturalidade']);
$filiacao = mysqli_real_escape_string($link, $_POST['filiacao']);
$estado_civil = mysqli_real_escape_string($link, $_POST['estado_civil']);
$residencia = mysqli_real_escape_string($link, $_POST['residencia']);
$documento = mysqli_real_escape_string($link, $_POST['documento']);
$numero_documento = mysqli_real_escape_string($link, $_POST['numero_documento']);
$nuit = mysqli_real_escape_string($link, $_POST['nuit']);
$emissao = mysqli_real_escape_string($link, $_POST['emissao']);
$validade = mysqli_real_escape_string($link, $_POST['validade']);
$localEmit = mysqli_real_escape_string($link, $_POST['localEmit']);
$email = mysqli_real_escape_string($link, $_POST['email']);
$phone = mysqli_real_escape_string($link, $_POST['phone']);
$distrito = mysqli_real_escape_string($link, $_POST['distrito']);
$state = mysqli_real_escape_string($link, $_POST['state']);
$zip = mysqli_real_escape_string($link, $_POST['zip']);
$country = mysqli_real_escape_string($link, $_POST['country']);
$beneficiario = mysqli_real_escape_string($link, $_POST['beneficiario']);
$membros = mysqli_real_escape_string($link, $_POST['membros']);
$comment = mysqli_real_escape_string($link, $_POST['comment']);




$update = mysqli_query($link, "UPDATE `borrowers` SET `fname`= '$fname',`lname`= '$lname',`data_nascimento`= '$data_nascimento',`sexo`= '$sexo',`naturalidade`= '$naturalidade',`filiacao`= '$filiacao',`estado_civil`= '$estado_civil',`residencia`= '$residencia',`documento`= '$documento',`numero_documento`= '$numero_documento',`nuit`= '$nuit',`emissao`=  '$emissao',`validade`= '$validade',`localEmit`= '$localEmit',`email`= '$email',`phone`= '$phone',`distrito`= '$distrito',`state`= '$state',`zip`= '$zip',`country`= '$country',`beneficiario`= '$beneficiario',`membros`= '$membros',`comment`= '$comment' WHERE id = '$id'") or die (mysqli_error($link));
if(!$update)
{
echo "<script>alert('Registo nao atualizado...Contacte o seu administrador!'); </script>";
}
else{
echo "<script>alert('Mutuario atualizado com sucesso!'); </script>";
echo "<script>window.location='listborrowers.php?id=".$_SESSION['tid']."'; </script>";
}
}
?>
</div>	
</div>
</div>	
</div>