<?php
  if (isset($_GET['source'])) {
        highlight_file($_SERVER['SCRIPT_FILENAME']);
        exit;
  }
?>
<html>
<head>
<title>PHP and Web Forms</title>
    
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="theme-color" content="#B5C7DD" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="../css/reset.css">
        <link rel="stylesheet" href="../css/mandy.css">
        <link rel="stylesheet" href="../css/form.css">

<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon">
</head>
<body>
    
<h2 class="pad-top">    
    
<?php   # need this to be able to view source in the below anchor tag
  $urlPath=dirname($_SERVER['SCRIPT_URL']).'/'; 
  $labsPath=$urlPath.'labs/'; 
?>     

    <a href="../index.php">CS 130A</a> → Lab 5
<a href="<?=$urlPath?>payments.php?source" target="labs">Source code</a>

</h2>    

<h4>Car Payments Formula</h4>
    

<?php      

$loan = $_POST['loan'];
$months = $_POST['months'];
$rate = $_POST['rate'];

       $monthly_rate = ( $rate / 1200 );
       $plus = ( $monthly_rate + 1 );
       $power = $plus ** $months;
       
       $raw = ( $loan * $monthly_rate * $power / ( $power - 1) );

$payment = number_format($raw, 2, '.', '');
                                                            
// using $base**$exp instead of pow (  $base ,  $exp ) for power function
       
//  original attempts keep outputting logic errors, so I created new variables to make order of operations errors easier to spot as well as to make the final output formula simpler

       //   inside the form below, php variables are interpolated inline in the html
    ?>    
  
<form action="index.php" method=post name="calculate" id="calculate"> 

<div class="pad-top">  

<label for="loan">Loan Amount: $</label>    
<input type="text" name="loan" id="loan" value="<?=$loan?>" readonly /> 
 
</div>   

<label for="months">Number of Months:</label>    
<input type="text" name="months" id="months" value="<?=$months?>" class="space" readonly /> months
 
 <br />      

<label for="rate">Yearly Rate:</label>
<input type="text" name="rate" id="rate" value="<?=$rate?>" class="space" readonly  />   %      
     
  
<div class="pad-top">
<label for="Payment:">Payment: $</label>
<input type="text" name="payment" id="payment" value="<?=$payment?>" class="space bold" readonly />  per month       
</div>    
    
<!--Reset button here doesn't actually reset but brings you back to the original index page with the form-->     
<div class="pad-top">     
<input type="submit" formaction="index.php" value="Reset" class="button"> 
    
<!-- edit button submits the form and sends you to edit.php -->
<input type="submit" formaction="edit.php" name="submit" value="Edit" class="space button">
</div>     
      
</form>     
  
    <p class="hide">
    this is a blank paragraph to do nothing but take up space and ensure the formatting works
    </p>
    <div class="stretch"></div>
</body>
</html>