<?php

if (!$user->cdp_is_Admin())
    cdp_redirect_to("login.php");

$userData = $user->cdp_getUserData();


include "config.php";

if (isset($_POST['update'])) {

    $var_1 = $_POST['expense_title'];

    $exp_id = $_POST['id'];

    $var_2 = $_POST['expense_type'];

    $var_3 = $_POST['created_date'];

    $var_4 = $_POST['expense_amount'];

    $var_5 = $_POST['pre_user'];
    
    $var_6 = $_POST['currency'];
    
    $var_7 = $_POST['name_off'];

    $sql = "UPDATE `expenses_master` SET `expense_title`='$var_1',`expense_type`='$var_2',`created_date`='$var_3',`expense_amount`='$var_4',`pre_user`='$var_5', `currency`='$var_6', `name_off`='$var_7'  WHERE `id`='$exp_id'";

    $result = $conn->query($sql);

    if ($result == TRUE) {

        echo "Record updated successfully.";
        
        header("Location: expenses_list.php");

    } else {

        echo "Error:" . $sql . "<br>" . $conn->error;

    }

}

if (isset($_GET['id'])) {

    $exp_id = $_GET['id'];

    $sql = "SELECT * FROM `expenses_master` WHERE `id`='$exp_id'";

    $result = $conn->query($sql);

    if ($result->num_rows > 0) {

        while ($row = $result->fetch_assoc()) {

            $var_1 = $row['expense_title'];

            $var_2 = $row['expense_type'];

            $var_3 = $row['created_date'];

            $var_4 = $row['expense_amount'];

            $var_5 = $row['pre_user'];
            
            $var_6 = $row['currency'];
            
            $var_7 = $row['name_off'];

            $id = $row['id'];

        }

?>
<!DOCTYPE html>
<html dir="ltr" lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- Tell the browser to be responsive to screen width -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <!-- Favicon icon -->
    <link rel="icon" type="image/png" sizes="16x16" href="assets/<?php echo $core->favicon ?>">
    <title>Edit Expenses | <?php echo $core->site_name ?></title>
    <!-- This Page CSS -->
    <!-- Custom CSS -->
    <link rel="stylesheet" href="assets/intl-tel-input/css/intlTelInput.css">

    <link href="assets/css/style.min.css" rel="stylesheet">

    <link href="assets/css_log/front.css" rel="stylesheet" type="text/css">
    <script type="text/javascript" src="assets/js/jquery.js"></script>
    <script type="text/javascript" src="assets/js/jquery-ui.js"></script>
    <script src="assets/js/jquery.ui.touch-punch.js"></script>
    <script src="assets/js/jquery.wysiwyg.js"></script>
    <script src="assets/js/global.js"></script>
    <script src="assets/js/custom.js"></script>
    <link href="assets/customClassPagination.css" rel="stylesheet">


    <script>
        $(document).ready(function () {
            $('[data-toggle="tooltip"]').tooltip();
        });
    </script>

</head>

<body>
<!-- ============================================================== -->
<!-- Preloader - style you can find in spinners.css -->
<!-- ============================================================== -->


<?php include 'views/inc/preloader.php'; ?>
<!-- ============================================================== -->
<!-- Main wrapper - style you can find in pages.scss -->
<!-- ============================================================== -->
<div id="main-wrapper">
    <!-- ============================================================== -->
    <!-- Topbar header - style you can find in pages.scss -->
    <!-- ============================================================== -->

    <!-- ============================================================== -->
    <!-- Preloader - style you can find in spinners.css -->
    <!-- ============================================================== -->

    <?php include 'views/inc/topbar.php'; ?>

    <!-- End Topbar header -->


    <!-- Left Sidebar - style you can find in sidebar.scss  -->

    <?php include 'views/inc/left_sidebar.php'; ?>


    <!-- End Left Sidebar - style you can find in sidebar.scss  -->

    <!-- Page wrapper  -->
    <!-- ============================================================== -->
    <div class="page-wrapper">

        <div class="page-breadcrumb ">
            <div class="row">
                <div class="col-8 align-self-center offset-1">
                    <h4 class="page-title">
                        Edit Expenses</h4>
                </div>
            </div>
        </div>


        <div class="container-fluid">


            <div class="row justify-content-center">
                <!-- Column -->
                <div class="col-lg-12 col-xlg-12 col-md-12">
                    <div class="card">

                        <div class="card-body">
                            <form action="" method="post">

                                <div class="row">
                                    <div class="col-md-2">
                                        <div class="form-group">
                                            <label>Expense Title</label>
                                            <input type="text" class="form-control" name="expense_title" value="<?php echo $var_1; ?>">
                                            <input type="hidden" name="id" value="<?php echo $id; ?>">
                                        </div>
                                    </div>
                                    <div class="col-md-2">
                                        <div class="form-group">
                                            <label>Expense Type</label>
                                            <select class="form-control" name="expense_type">
                                                <option value="<?php echo $var_2; ?>"><?php echo $var_2; ?></option>
                                                <option value="Meals">Meals</option>
                                                <option value="Utility">Utility</option>
                                                <option value="Others">Others</option>
                                                <option value="Salary">Salary</option>
                                                <option value="Transport">Transportation</option>
                                                <option value="Cargo">Cargo Charges</option>
                                            </select>

                                        </div>
                                    </div>
                                    <div class="col-md-2">
                                        <div class="form-group">
                                            <label>Expense Date</label>
                                            <input type="date" class="form-control" name="created_date" value="<?php echo $var_3; ?>">
                                        </div>
                                    </div>
                                    <div class="col-md-2">
                                        <div class="form-group">
                                            <label>Currency</label>
                                            <select class="form-control" name="currency">
                                                <option value="<?php echo $var_6; ?>"><?php echo $var_6; ?></option>
                                                <option value="Ksh.">KES</option>
                                                <option value="$">USD</option>
                                            </select>

                                        </div>
                                    </div>
                                    <div class="col-md-2">
                                        <div class="form-group">
                                            <label>Agency</label>
                                            <select class="form-control" name="name_off">
                                                <option value="<?php echo $var_6; ?>"><?php echo $var_7; ?></option>
                                                <option value="Galeyr NBO">Galeyr NBO</option>
                                                <option value="Galeyr MGQ">Galeyr MGQ</option>
                                            </select>

                                        </div>
                                    </div>
                                    <div class="col-md-2">
                                        <div class="form-group">
                                            <label>Expense Amount</label>
                                            <input type="text" class="form-control" name="expense_amount" value="<?php echo $var_4; ?>">
                                        </div>
                                    </div>
                                    <div class="col-md-2">
                                        <div class="form-group">
                                            <label>User</label>
                                            <input type="text" class="form-control" name="pre_user" value="<?php echo $var_5; ?>">
                                        </div>
                                    </div>

                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <button class="btn btn-primary" type="submit" name="update" value="Update">Update</button>
                                        </div>
                                         <a href="expenses_list.php" class="btn btn-outline-secondary btn-confirmation"><span><i class="ti-share-alt"></i></span> Return to Expenses List</a>
                                    </div>
                                </div>



                            </form>
                        </div>
                    </div>

                </div>
                <!-- Column -->
            </div>


        </div>


    </div>
    <!-- ============================================================== -->
    <!-- End Page wrapper  -->
    <!-- ============================================================== -->
</div>
<!-- ============================================================== -->
<!-- End Wrapper -->
<!-- ============================================================== -->


<!-- ============================================================== -->
<!-- All Jquery -->
<!-- ============================================================== -->
<!-- Bootstrap tether Core JavaScript -->
<script src="assets/libs/popper.js/dist/umd/popper.min.js"></script>
<script src="assets/libs/bootstrap/dist/js/bootstrap.min.js"></script>
<!-- apps -->
<script src="assets/js/app.min.js"></script>
<script src="assets/js/app.init.js"></script>
<script src="assets/js/app-style-switcher.js"></script>
<!-- slimscrollbar scrollbar JavaScript -->
<script src="assets/libs/perfect-scrollbar/dist/perfect-scrollbar.jquery.min.js"></script>
<script src="assets/extra-libs/sparkline/sparkline.js"></script>
<!--Wave Effects -->
<script src="assets/js/waves.js"></script>
<!--Menu sidebar -->
<script src="assets/js/sidebarmenu.js"></script>
<!--Custom JavaScript -->
<script src="assets/js/custom.min.js"></script>

<script src="assets/intl-tel-input/js/intlTelInput.js"></script>


</body>

</html>

<?php

} else{

    header('Location: view.php');

}

}

?>
