<?php
include('../includes/functions.php');
session_start();
make_connection();
if(is_loggedin());
else
  redirect_to('index.php');
$user=get_user();
if(!$user)
 redirect_to('index.php');
$user_id=$user['regno'];

if(isset($_POST['submit'])){
  if(!isset($_POST['cost']))
    $cost=0;
  else
    $cost=$_POST['cost'];
  if(!isset($_POST['due_return_date']))
    $due_return_date=0;
  else
    $due_return_date=$_POST['due_return_date'];
  if(!isset($_POST['act_return_date']))
    $act_return_date=0;
  else
    $act_return_date=$_POST['act_return_date'];

  $success=insert_transaction($_POST['book_id'],$_POST['buyer_id'],$_POST['sold_borrowed'],$cost,$due_return_date,$act_return_date);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <?php include('../includes/head.php');?>
  <title>Utopia- Add Transactions</title>
  <link rel="stylesheet" type="text/css" href="font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" type="text/css" href="materialize/css/materialize.css">
  <link rel="stylesheet" type="text/css" href="css/home.css">
  <style type="text/css">
    .color-red{
      color: red;
    }
    .color-green{
      color: green;
    }
    #createnew{
      cursor: pointer;
    }
  </style>
</head>
<body>
  <div class="body row">
    <?php
    global $navactive;
    $navactive="transactions";
    include('../includes/nav.php');
    ?>

    <div class="main">
      <div class="container">
        <header>
          <h3 class="center" id="introo_heading21">Add a transactions record</h3>
          <h3 class="center"><?php 
            if(isset($success) and $success){
              echo "<span class='color-green'>Successfully inserted Transaction.</span>";
            }
            if(isset($success) and !$success){
              echo "<span class='color-red'>Transaction insertion failed.</span>";
            }
            ?>
          </h3>

          <br>
        </header>
        <div class="requestssdiv">
         <form action="#" method="POST">
          <div class="container">
            <div class="row">
              <div class="input-field">
                <input type="number" required name="book_id" class="validate" id="book_id">
                <label for="book_id">Book ID</label>
              </div>
            </div>
            <div class="row">
              <div class="input-field">
                <input type="text" required name="buyer_id" class="validate" id="buyer_id">
                <label for="buyer_id">Buyer ID</label>
              </div>
            </div>
             <div class="row">
            <div class="input-field">
              <input type="number" name="cost" class="validate" value="0" id="cost">
              <label for="cost">Cost</label>
            </div>
          </div>
            <div class="row">
              <div class="input-field">
               <select name="sold_borrowed" required>
                 <option value="1" id="op1">Sold permanantly</option>
                 <option value="2" id="op2">Borrowed temporarily</option>
               </select>
             </div>
           </div>
          <div class="row">
         <label> Due return Date:</label>
            <div class="input-field">
              <input type="date" name="due_return_date" class="validate borrowed_prop" id="due_return_date">
            </div>
          </div>
          <div class="row">
          <label>Actual return date:</label>
            <div class="input-field">
              <input type="date" name="act_return_date" class="validate borrowed_prop" id="act_return_date">
            </div>
          </div>
          <div class="row">
            <input type="submit" name="submit" class="btn right">
            <a href="transactions.php"> 
            <button class="btn left red">Go back</button>
            </a>
          </div>
        </div>
      </form>
    </div>
  </div>
</div>
</div>
<script src="js/jquery.js"></script>
<script src="materialize/js/materialize.js"></script>
<script>
  $(".button-collapse").sideNav();
  $('.button-collapse').sideNav({
      menuWidth: 300, // Default is 240
      edge: 'left', // Choose the horizontal origin
      closeOnClick: false // Closes side-nav on <a> clicks, useful for Angular/Meteor
    }
    );
  </script>
  <script>
    $(document).ready(function() {
      $('select').material_select();
    });
  </script>
  <script type="text/javascript">
    function check_val(){
      var brr=document.getElementsByClassName('.borrowed_prop');
      if($("select").val()=='1'){
        $(".borrowed_prop").prop('disabled',true);
      }
      else{
        $(".borrowed_prop").prop('disabled',false);
      }
    }
    check_val();
    $("select").change(function(){
      check_val();
    });
  </script>
</body>
</html>