<div class="box">
	      <div class="box-body">
			<div class="panel panel-success">
            <div class="panel-heading">
            <h3 class="panel-title"><i class="fa fa-dollar"></i>Novo Pagamento</h3>
            </div>
             <div class="box-body">
<?php
if(isset($_POST['save']))
{
$tid = $_SESSION['tid'];
$name = mysqli_real_escape_string($link, $_POST['teller']);
$desc =  mysqli_real_escape_string($link, $_POST['desc']);
$account_no = mysqli_real_escape_string($link, $_POST['account']);
$customer = mysqli_real_escape_string($link, $_POST['customer']);
$loan = mysqli_real_escape_string($link, $_POST['loan']);
$pay_date = mysqli_real_escape_string($link, $_POST['pay_date']);
$amount_to_pay = mysqli_real_escape_string($link, $_POST['amount_to_pay']);
$referencia = mysqli_real_escape_string($link, $_POST['referencia']);
$conta = mysqli_real_escape_string($link, $_POST['conta']);
$remarks = mysqli_real_escape_string($link, $_POST['remarks']);
$balance = $loan - $amount_to_pay;

$look = mysqli_query($link, "SELECT * FROM loan_info WHERE borrower = '$customer' AND status = 'Approved' AND id = '$desc'") or die (mysqli_error($link));

if(mysqli_num_rows($look) > 0){

$insert = mysqli_query($link, "INSERT INTO payments(tid,account_no,customer,id_emprestimo,loan, pay_date,amount_to_pay, referencia, conta, remarks) VALUES('$tid','$account_no','$customer','$desc','$loan','$pay_date','$amount_to_pay','$referencia','$conta','$remarks')") or die (mysqli_error($link));
if(!$insert)
{
echo '<meta http-equiv="refresh" content="2;url=newpayments.php?tid='.$_SESSION['tid'].'">';
echo '<br>';
echo'<span class="itext" style="color: #FF0000">Impossivel efetuar pagamento.....Por favor tente novamente!</span>';
   
}
else{
    mysqli_query($link, "UPDATE loan_info SET amount_topay = amount_topay - '$amount_to_pay', pay_date = date_add(pay_date, interval 1 month) WHERE borrower = '$customer' AND id = '$desc'") or die (mysqli_error($link));
    $sql = mysqli_query($link, "INSERT INTO transacoes(mutuario, descricao,referencia, credito, saldo, data) VALUES('$customer', '$remarks', '$referencia', '$amount_to_pay', '$balance', '$pay_date')") or die (mysqli_error($link));
echo '<meta http-equiv="refresh" content="2;url=listpayment.php?tid='.$_SESSION['tid'].'">';
echo '<br>';
echo'<span class="itext" style="color: #FF0000">Salvando Pagamento.....Por favor espere!</span>';
}
}else{
    echo '<meta http-equiv="refresh" content="2;url=newpayments.php?tid='.$_SESSION['tid'].'">';
echo '<br>';
echo'<span class="itext" style="color: #FF0000">Impossivel efetuar pagamento.....Este emprestimo não esta aprovado!</span>';
}
}
?>
			 <form class="form-horizontal" method="post" enctype="multipart/form-data" action="">
			  <?php echo '<div class="alert alert-info fade in" >
			  <a href = "#" class = "close" data-dismiss= "alert"> &times;</a>
  				<strong>Note que&nbsp;</strong> &nbsp;&nbsp;Os campos obrigatorios estao assinalados por <span style="color:red"> *</span>.
				</div>'?>
             <div class="box-body">
				
				  
				   <div class="form-group">
                <label for="" class="col-sm-2 control-label" style="color:#009900"><b style="color:red">*</b> Cliente</label>
				 <div class="col-sm-10">
                <select class="customer select2" name="customer" style="width: 100%;" required>
				<option selected="selected">--Selecionar Cliente--</option>
                 <?php
				$get = mysqli_query($link, "SELECT * FROM borrowers order by id") or die (mysqli_error($link));
				while($rows = mysqli_fetch_array($get))
				{
				echo '<option value="'.$rows['id'].'">'.$rows['fname'].'&nbsp;'.$rows['lname'].'</option>';
				}
				?>
                </select>
              </div>
			  </div>
	    <script type="text/javascript">
	$(function() {
	    $(".customer").on('change', function(){
	        preecher_select2();
	        preecher_select4();
	    });

	    $(".desc").on('change', function(){
	        preecher_select3();

	    });

		function preecher_select2() {
		    var ida = $(".customer").val();
			$.ajax({
				url: 'numeroConta.php',
				type: 'GET',
				data:{
				    id: ida
				},
				success: function(data) {
					$(".account").html(data);
				},
				error: function() {
					$(".account").html("Ocorreu um erro na tentativa de preencher o select!");
				}
			});
		}
		function preecher_select3() {
		    var idaa = $(".desc").val();
		    var customer = $(".customer").val();
			$.ajax({
				url: 'loan.php',
				type: 'GET',
				data:{
				    desc: idaa,
				    customer: customer
				},
				success: function(data) {
					$(".loan").html(data);
				},
				error: function() {
					$(".loan").html("Ocorreu um erro na tentativa de preencher o select!");
				}
			});
		}
		function preecher_select4() {
		    var ida = $(".customer").val();
			$.ajax({
				url: 'loan_desc.php',
				type: 'GET',
				data:{
				    id: ida
				},
				success: function(data) {
					$(".desc").html(data);
				},
				error: function() {
					$(".desc").html("Ocorreu um erro na tentativa de preencher o select!");
				}
			});
		}

	});
</script>			  
		 <div class="form-group">
                <label for="" class="col-sm-2 control-label" style="color:#009900"><b style="color:red">*</b>Nº de Conta</label>
				 <div class="col-sm-10">
                <select class="account select2" name="account" style="width: 100%;" required>
             
                </select>
              </div>
			  </div>
			  <div class="form-group">
                <label for="" class="col-sm-2 control-label" style="color:#009900"><b style="color:red">*</b>Descrição do Emprestimo</label>
				 <div class="col-sm-10">
                <select class="desc select2" name="desc" style="width: 100%;" required>
              
                </select>
              </div>
			  </div>
			  <div class="form-group">
                <label for="" class="col-sm-2 control-label" style="color:#009900"><b style="color:red">*</b>Emprestimo</label>
				 <div class="col-sm-10">
                <select class="loan select2" name="loan" style="width: 100%;" required>
              
                </select>
              </div>
			  </div>
			 
			<div class="form-group">
                <label for="" class="col-sm-2 control-label" style="color:#009900"><b style="color:red">*</b> Data</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="date" class="form-control pull-right"  required>
                </div>
              </div>
			  </div>
				  
		<div class="form-group">
                  <label for="" class="col-sm-2 control-label" style="color:#009900"><b style="color:red">*</b> A Pagar</label>
                  <div class="col-sm-10">
                  <input name="amount_to_pay" type="number" class="form-control" placeholder="Valor a Pagar" required step="0.01">
                  </div>
                  </div>
        <div class="form-group">
                  <label for="" class="col-sm-2 control-label" style="color:#009900"><b style="color:red">*</b> Referência do Deposito</label>
                  <div class="col-sm-10">
                  <input name="referencia" type="text" class="form-control" placeholder="Referencia" required>
                  </div>
                  </div>
        <div class="form-group">
                  <label for="" class="col-sm-2 control-label" style="color:#009900"><b style="color:red">*</b> Conta de Deposito</label>
                  <div class="col-sm-10">
                  <input name="conta" type="number" class="form-control" placeholder="Numero de conta" value="477351510001" required readonly>
                  </div>
                  </div>
				  
		<div class="form-group">
                  <label for="" class="col-sm-2 control-label" style="color:#009900">Processado Por</label>
                  <div class="col-sm-10">
                 <?php
$tid = $_SESSION['tid'];
$sele = mysqli_query($link, "SELECT * from user WHERE id = '$tid'") or die (mysqli_error($link));
while($row = mysqli_fetch_array($sele))
{
?>
                  <input name="teller" type="text" class="form-control" value="<?php echo $row['name']; ?>" readonly>
<?php } ?>
                  </div>
                  </div>
				  
				  
		 <div class="form-group">
                  	<label for="" class="col-sm-2 control-label" style="color:#009900">Observaçao</label>
                  	<div class="col-sm-10">
					<textarea name="remarks"  class="form-control" rows="4" cols="80"></textarea>
           			 </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;Limpar</i></button>
                				<button name="save" type="submit" class="btn btn-success btn-flat"><i class="fa fa-save">&nbsp;Processar</i></button>

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

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