<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once '../auth/auth.php';
require_once '../models/db_connection.php';
if (isset($_GET['id'])) {
    $sql = mysqli_query($conn, "SELECT * FROM cash_deposits WHERE cash_id ='$_GET[id]'");
    $fetch = mysqli_fetch_array($sql);
}
if (isset($_POST['edit'])) {
    $updatecash = mysqli_query($conn, " UPDATE cash_deposits SET ammount = '$_POST[amount]' , deposit_date = '$_POST[depositedDate]' , narration = '$_POST[narration]' WHERE cash_id = '$_GET[id]'");
    if ($updatecash) {
        echo "<script>
        alert('Deposit editted successfully');
        window.location.href = 'view_deposits.php';
    </script>";
    }
}

?>
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Accounts | Deposit Cash </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>AVAILABLE CASH: <span style="color:green">
                                            <?php
                                            $amount = mysqli_query($conn, " SELECT * FROM cash_deposits");
                                            $total = 0;
                                            while ($tot = mysqli_fetch_array($amount)) {
                                                $total += $tot['ammount'];
                                            }
                                            echo number_format($total);
                                            ?>
                                        </span></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">Edit Deposit Cash</h4>

                                <form action="" method="POST">
                                    <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="depositedDate" value="<?php echo $fetch['deposit_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['narration']; ?>" required>
                                    </div>

                                    <div class="col-sm-9">
                                        <button type="submit" name="edit" class="btn btn-dark btn-lg ">Deposit Cash</button>
                                    </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>