<!DOCTYPE HTML>
<?PHP 
require 'functions.php';
checkLogin();
$db_link = connect();
getGroupLoanID($db_link);

//Retrieve loan_id and loan_no of newly created loan from group_loans.
$sql_loan = "SELECT loan_id, loan_no FROM group_loans WHERE loan_id = '$_SESSION[loan_id]'";
$query_loan = mysqli_query($db_link, $sql_loan);
checkSQL($db_link, $query_loan);
$result_loan = mysqli_fetch_assoc($query_loan);

// Generate timestamp
$timestamp = time();

//SKIP-Button
if (isset($_POST['skip'])){
    echo "Loan ID: ".$_SESSION['loan_id'];
    header('Location: group_loan.php?loan_id='.$_SESSION['loan_id']);
    exit;
}



//UPLOAD-Button
if (isset($_POST['upload'])){
  $target_path = "uploads/letters/";
  
  // Check if directory exists, if not create it
  if (!is_dir($target_path)) {
    mkdir($target_path, 0777, true);
  }
  
  //Check if a file was uploaded
  if ($_FILES['letter']['size'] != 0){
    //Determine where file is going to be stored and create file name
    $path_part = pathinfo($_FILES['letter']['name']);
    $extension = $path_part['extension'];
    $file_name = 'Group_Loan_'.$result_loan['loan_no'].'.'.$extension;
    
    //Add original filename to target path
    $target_path_file = $target_path.$file_name;
    
    //Move uploaded file from temporary storage to final location
    move_uploaded_file($_FILES['letter']['tmp_name'], $target_path_file);
    
    //UPDATE file path for letter in group_loans
    $sql_update_letter_path = "UPDATE group_loans SET letter_path = '$target_path_file' WHERE loan_id = '$result_loan[loan_id]'";
    $query_update_letter_path = mysqli_query($db_link, $sql_update_letter_path);
    checkSQL($db_link, $query_update_letter_path);
  }
  
  //Unset session variables and refer to LOAN.PHP
  header('Location: group_loan.php?loan_id='.$_SESSION['loan_id']);
  exit;
}
?>

<html>
<?PHP includeHead('New Loan',1) ?>
<body>
  <!-- MENU -->
  <?PHP includeMenu(2); ?>
  <div id="menu_main">
    <a href="customer.php?cust=<?PHP echo $_SESSION['cust_id'] ?>">Back</a>
    <a href="cust_search.php">Search</a>
    <a href="loan_new.php?cust=<?PHP echo $_SESSION['cust_id'] ?>">loan</a>
    <a href="loan_new.php?cust=<?PHP echo $_SESSION['cust_id'] ?>" id="item_selected">New Loan</a>
    <a href="cust_new.php">New Customer</a>
    <a href="cust_act.php">Active Cust.</a>
    <a href="cust_inact.php">Inactive Cust.</a>
  </div>
  <div class="content_center">
    <p class="heading">Upload Letter for Group Loan <?PHP echo $result_loan['loan_no']; ?></p>
    <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
      <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
      <label for="letter" class="file-upload">
        <i class="fa fa-file-text-o"></i> Upload Letter
      </label>
      <input type="file" name="letter" id="letter" accept=".pdf,.jpg,.jpeg,.png,.tif,.tiff,.doc,.docx,.xls,.xlsx,.odt,.ods,.txt" />
      <br/>
      <input type="submit" name="upload" value="Upload" />
      <input type="submit" name="skip" value="Skip" />
    </form>
  </div>
</body>
</html>