<div class="row">
		
	       
<section class="content">  
	        <div class="box box-success">
            <div class="box-body">
              <div class="table-responsive">
             	<div class="box-body">
<form method="post">
	<a class="btn btn-primary btn-flat" onclick="window.print();"><i class="fa fa-print"></i>&nbsp;Imprimir</a>
	<hr>		
			  
			 <table id="example1" class="table table-bordered table-striped">
                <thead>
                <tr>
                  <th>Conta</th>
                  <th>Mutuario</th>
                  <th>Provincia</th>
                  <th>Distrito</th>
                  <th>Tipo de Credito</th>
                  <th>Cadeia de Valores</th>
                  <th>Emprestimo</th>
                  <th>Saldo em divida</th>
                 </tr>
                </thead>
                <tbody>
<?php
$provincia = $_GET['provincia'];
$credito = $_GET['credito'];
$distrito = $_GET['distrito'];
$cadeia = $_GET['cadeia'];

//var_dump($provincia." ".$distrito." ".$credito." ".$cadeia);

if($provincia != NULL && $distrito == NULL && $credito == NULL && $cadeia == NULL){
    $select = mysqli_query($link, "SELECT id, state, distrito FROM borrowers WHERE state = '$provincia'") or die (mysqli_error($link));
    while($dados = mysqli_fetch_array($select)){
        $id = $dados['id'];
        $provincia1 = $dados['state'];
        $distrito1 = $dados['distrito'];
        $select2 = mysqli_query($link, "SELECT (SELECT CONCAT(fname,' ', lname) as nome FROM borrowers as b WHERE b.id = l.borrower) as nomecompleto,baccount, (SELECT tipo_credito FROM tipo_credito as t WHERE t.id = l.credito) as credito, cadeia, amount, amount_topay FROM loan_info as l WHERE borrower = '$id'") or die (mysqli_error($link));
        while($dados2 = mysqli_fetch_array($select2)){
            $nome = $dados2['nomecompleto'];
            $conta = $dados2['baccount'];
            $credito1 = $dados2['credito'];
            $cadeia1 = $dados2['cadeia'];
            $valor = $dados2['amount'];
            $valor_pagar = $dados2['amount_topay'];
            echo "<tr>";
            echo "<td>$conta</td>";
            echo "<td>$nome</td>";
            echo "<td>$provincia1</td>";
            echo "<td>$distrito1</td>";
            echo "<td>$credito1</td>";
            echo "<td>$cadeia1</td>";
            echo "<td>$valor</td>";
            echo "<td>$valor_pagar</td>";
            echo "</tr>";
        }
    }
}elseif($provincia != NULL && $distrito != NULL && $credito == NULL && $cadeia == NULL){
    $select = mysqli_query($link, "SELECT id, state, distrito FROM borrowers WHERE state = '$provincia' AND distrito = '$distrito'") or die (mysqli_error($link));
    while($dados = mysqli_fetch_array($select)){
        $id = $dados['id'];
        $provincia1 = $dados['state'];
        $distrito1 = $dados['distrito'];
        $select2 = mysqli_query($link, "SELECT (SELECT CONCAT(fname,' ', lname) as nome FROM borrowers as b WHERE b.id = l.borrower) as nomecompleto,baccount, (SELECT tipo_credito FROM tipo_credito as t WHERE t.id = l.credito) as credito, cadeia, amount, amount_topay FROM loan_info as l WHERE borrower = '$id'") or die (mysqli_error($link));
        while($dados2 = mysqli_fetch_array($select2)){
            $nome = $dados2['nomecompleto'];
            $conta = $dados2['baccount'];
            $credito1 = $dados2['credito'];
            $cadeia1 = $dados2['cadeia'];
            $valor = $dados2['amount'];
            $valor_pagar = $dados2['amount_topay'];
            echo "<tr>";
            echo "<td>$conta</td>";
            echo "<td>$nome</td>";
            echo "<td>$provincia1</td>";
            echo "<td>$distrito1</td>";
            echo "<td>$credito1</td>";
            echo "<td>$cadeia1</td>";
            echo "<td>$valor</td>";
            echo "<td>$valor_pagar</td>";
            echo "</tr>";
        }
    }
}elseif($provincia != NULL && $distrito != NULL && $credito != NULL && $cadeia != NULL){
    $select = mysqli_query($link, "SELECT id, state, distrito FROM borrowers WHERE state = '$provincia' AND distrito = '$distrito'") or die (mysqli_error($link));
    while($dados = mysqli_fetch_array($select)){
        $id = $dados['id'];
        $provincia1 = $dados['state'];
        $distrito1 = $dados['distrito'];
        $select2 = mysqli_query($link, "SELECT (SELECT CONCAT(fname,' ', lname) as nome FROM borrowers as b WHERE b.id = l.borrower) as nomecompleto,baccount, (SELECT tipo_credito FROM tipo_credito as t WHERE t.id = l.credito) as credito, cadeia, amount, amount_topay FROM loan_info as l WHERE borrower = '$id' AND l.credito = '$credito' AND l.cadeia = '$cadeia'") or die (mysqli_error($link));
        while($dados2 = mysqli_fetch_array($select2)){
            $nome = $dados2['nomecompleto'];
            $conta = $dados2['baccount'];
            $credito1 = $dados2['credito'];
            $cadeia1 = $dados2['cadeia'];
            $valor = $dados2['amount'];
            $valor_pagar = $dados2['amount_topay'];
            echo "<tr>";
            echo "<td>$conta</td>";
            echo "<td>$nome</td>";
            echo "<td>$provincia1</td>";
            echo "<td>$distrito1</td>";
            echo "<td>$credito1</td>";
            echo "<td>$cadeia1</td>";
            echo "<td>$valor</td>";
            echo "<td>$valor_pagar</td>";
            echo "</tr>";
        }
    }
}

?>
    </tbody>
</table>  
</form>
</div>
</div>	
</div>
</div>	
</div>