<?php
require_once '../auth/auth.php';
require_once '../models/db_connection.php';
//require_once '../models/expense_model.php';
if(isset($_GET['id'])){
    $sql = mysqli_query($conn,"SELECT * FROM expenses WHERE id='$_GET[id]'");
    $fetch = mysqli_fetch_array($sql);
}
if (isset($_POST["editexp"])) {

    $expense = $_POST["expense"];
    $amount = $_POST["amount"];
    $issuedDate = $_POST["issuedDate"];
    $userID = $_SESSION["user"];
    $narration = $_POST["narration"];
    $update = mysqli_query($conn, "UPDATE expenses SET expense = '$expense', ammount = '$amount', 
    issued_date = '$issuedDate', naration = '$narration' WHERE id = '$_GET[id]'");
    if ($update) {
        echo "<script>
        alert('Expense editted successfully');
        window.location.href = 'view_expenses.php';
    </script>";
    }
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Expenses | Add Expenses </title>
    <?php include("../templates/header.php"); ?>
</head>

<body>
    <div class="container-scroller">
        <!-- partial:partials/_navbar.html -->
        <?php include("../templates/nav.php"); ?>
        <!-- partial -->
        <div class="container-fluid page-body-wrapper">
            <!-- partial:partials/_sidebar.html -->
            <?php include("../templates/sidenav.php"); ?>
            <!-- partial -->
            <div class="main-panel">
                <div class="content-wrapper">
                    <div class="row">


                        <div class="col-md-6 grid-margin stretch-card">
                            <div class="card">
                                <div class="card-body">
                                    <h1>Edit Expenses: </h1>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6 grid-margin stretch-card">
                        <div class="card">
                            <div class="card-body">
                                <h4 class="card-title">Add Expenses</h4>

                                <form action="" method="POST">
                                    <div class="form-group">
                                        <label for="exampleFormControlSelect2">Expense</label>
                                        <input type="text" class="form-control" name="expense" value="<?php echo $fetch['expense']; ?>" required>
                                    </div>
                                    <div class="form-group">
                                        <label for="exampleFormControlSelect2">Ammount</label>
                                        <input type="number" class="form-control" name="amount" value="<?php echo $fetch['ammount']; ?>" required>
                                    </div>

                                    <div class="form-group">
                                        <label for="exampleFormControlSelect2">Date</label>
                                        <input required type="date" class="form-control" name="issuedDate" value="<?php echo $fetch['issued_date']; ?>" required>
                                    </div>

                                    <div class="form-group">
                                        <label for="exampleFormControlSelect2">Narration</label>
                                        <input required type="text" class="form-control" name="narration" value="<?php echo $fetch['naration']; ?>" required>
                                    </div>

                                    <div class="col-sm-9">
                                        <input type="submit" name="editexp" value="Update" class="btn btn-dark btn-lg ">
                                    </div>
                            </div>
                            </form>
                        </div>
                    </div>

                </div>
                <!-- content-wrapper ends -->
                <!-- partial:partials/_footer.html -->
                <?php include("../templates/footer.php"); ?>

                <!-- partial -->
            </div>
            <!-- main-panel ends -->
        </div>
        <!-- page-body-wrapper ends -->
    </div>
    <?php include("../templates/inc_scripts.php"); ?>
</body>

</html>