<?php
include('includes/header.php');
include('includes/navbar.php');
?>


<?php
if (isset($_POST['update_tranx'])) {
    $trxn_id = $_POST['trxn_id'];
    $trxn_amount = $_POST['trxn_amount'];
    $trxn_date = $_POST['trxn_date'];
    $trnx_status = $_POST['trnx_status'];

    if (empty($trxn_id) || empty($trxn_amount) || empty($trxn_date) || empty($trnx_status)) {
      $error = "empty";
    }else {
      $sql_trnx_update = "UPDATE transaction SET transaction_status  = '$trnx_status', transaction_amount = '$trxn_amount', transaction_date = '$trxn_date' WHERE trnx_no = '$trxn_id'";
      if ($connection->query($sql_trnx_update)===TRUE) {
          $error = "tranx_update_success";
          }else {
            $error = "error";
          }
    }
}
 ?>

<div class="container-fluid">

    <!-- DataTales Example -->
    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">Edit Transaction Details</h6>
        </div>
        <div class="card-body">
        <?php

            if(isset($_GET['trnx_id']))
            {
                $id = $_GET['trnx_id'];
                if(empty($id)) {
                  echo "<script>
                    window.location.href = 'dashboard.php';
                  </script>";
                }else {
                $query = "SELECT * FROM transaction WHERE trnx_no = '$id'";
                $query_run = mysqli_query($connection, $query);

                foreach($query_run as $row)
                {
                    ?>

                        <form action="" method="POST">

                            <input type="hidden" name="trxn_id" value="<?php echo $row['trnx_no'] ?>">

                            <div class="form-group">
                                <label> Amount </label>
                                <input type="text" name="trxn_amount" value="<?php echo $row['transaction_amount'] ?>" class="form-control">
                            </div>
                            <div class="form-group">
                                <label> Transaction Date </label>
                                <input type="datetime" name="trxn_date" value="<?php echo $row['transaction_date'] ?>" class="form-control">
                            </div>

                            <div class="form-group">
                                <label>Transaction Status</label>
                                <select class="form-control" name="trnx_status" style="text-transform:capitalize">
                                    <option value="<?php echo $row['transaction_status'] ?>"><?php echo $row['transaction_status'] ?></option>
                                    <option value="success">Success</option>
                                    <option value="pending">Pending</option>
                                    <option value="decline">Decline</option>
                                </select>
                            </div>

                            <a href="dashboard.php" class="btn btn-danger"> CANCEL </a>
                            <button type="submit" name="update_tranx" class="btn btn-primary"> Update </button>

                        </form>
                        <?php
                }
              }
            }
        ?>
        </div>
    </div>
</div>

</div>

<p id="error" style='display: none'><?php echo $error?></p>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script>
    var error = document.getElementById('error');

    if (error.textContent == 'empty') {
         swal("ERROR!", "Input's cannot be empty!", "warning");
    }else if (error.textContent == "tranx_update_success") {
        swal("SUCCESS!", "Transaction Updated Successfully", "success");
        setTimeout(() => {
            window.location.href = 'dashboard.php'
        }, 2000);
    }else if (error.textContent == "error") {
        swal("ERROR!", "Sorry an error occurred. Please try again later", "warning");
    }

</script>
<?php
include('includes/scripts.php');
include('includes/footer.php');
?>
