<?php
include("../db/cn.php");
ob_start();
if(isset($_SESSION['user_name']) && isset($_SESSION['user_role']) && isset($_SESSION['outlet_name']) && isset($_SESSION['outlet_address'])) {
    $userName = $_SESSION['user_name'];
    $userRole = $_SESSION['user_role'];
    $outlet_address = $_SESSION['outlet_address'];
    $outlet_name = $_SESSION['outlet_name'];
}
@$expName = $_GET['exp_name'];

        // Display the user's name and role
// if($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['expence_add'])){
// 	$userName1 = $_SESSION['user_name'];
//      $userRole1 = $_SESSION['user_role'];
// 	@$name_detail = $_POST['desc'];
// 	@$amount = $_POST["amount"];
// 	$paid_by = $_POST["paid_by"];
// 	$date_time122 = $_POST["setdate"];
// 	$date_time122 = $_POST["description"];

// 	$date_time122 = date("Y-m-d");
// 	mysqli_query($connection,"insert into 
// 	incash(name_detail,amount,cr_id,paid_by,login_user,user_role,outlet_name,outlet_address,date_time)
// 	values('".$name_detail."','".$amount."','','".$paid_by."','".$userName1."','".$userRole1."','".$outlet_name."','".$outlet_address."','".$date_time122."')");
	
// 	}
if($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['expence_add'])) {
    $userName1 = $_SESSION['user_name'];
    $userRole1 = $_SESSION['user_role'];
    $name_detail = $_POST['desc'] ?? '';
    $amount = $_POST['amount'] ?? 0;
    $paid_by = $_POST['paid_by'] ?? '';

    // Agar user date select kiya hai use use karo, warna today date
    $date_time122 = !empty($_POST['setdate']) ? $_POST['setdate'] : date("Y-m-d");

    // Optional: extra description
    $description = $_POST['description'] ?? '';

    // Insert query
    $insert_query = "
        INSERT INTO incash(name_detail, amount, cr_id, paid_by, login_user, user_role, outlet_name, outlet_address, date_time, description)
        VALUES('$name_detail', '$amount', '', '$paid_by', '$userName1', '$userRole1', '$outlet_name', '$outlet_address', '$date_time122', '$description')
    ";
    
    if(mysqli_query($connection, $insert_query)) {
        echo "";
    } else {
        echo "Error: " . mysqli_error($connection);
    }
}


if (isset($_POST['delete_id'])) {
    $id = intval($_POST['delete_id']);
    $del_query = "DELETE FROM incash WHERE id = $id LIMIT 1";
    if (mysqli_query($connection, $del_query)) {
        echo "<script>
                Swal.fire('Deleted!','Record has been deleted.','success');
              </script>";
    } else {
        echo "<script>
                Swal.fire('Error!','Unable to delete record.','error');
              </script>";
    }
}

ob_end_flush();
?>

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

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title> Expense Management</title>

    <!-- Custom fonts for this template-->
    <link href="../vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
   <link href="https://fonts.googleapis.com/css2?family=Quicksand:wght@200;300;400;500;600;700&display=swap" rel="stylesheet">

    <!-- Custom styles for this template-->
    <link href="../css/sb-admin-2.min.css" rel="stylesheet">
        <link href="../css/sb-admin-2.css" rel="stylesheet">
    
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<style>
body {
    font-family: 'Quicksand', sans-serif;
    
}
		    .chart-container {
      position: relative;
      background: rgba(255, 255, 255, 0.9);
      border-radius: 10px;
      padding: 15px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
    }

    canvas {
      max-width: 100%;
      height: auto;
    }
.bg-gradient-primary{
    
	background:#0A4657;
	color:#fff;	
	
	}

.btn-primary{
	background:#0A4657;
	color:#fff;
	 border:1px #0A4657 solid;
	
	}  
.btn-primary:hover{
  
  background:#fff;
  border:1px #0A4657 solid;
	color:#0A4657;
	
	}	 
    .chart-container {
      position: relative;
      background: rgba(255, 255, 255, 0.9);
      border-radius: 10px;
      padding: 15px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
    }

    canvas {
      max-width: 100%;
      height: auto;
    }
	
	.fa-download:hover{
		color:#953E39;
		}
		
	/* Basic styling */
    .tab {
        display: none;
        padding: 20px;
    }
    .active {
        display: block;
    }
    .tab-buttons {
        display: flex;
    }
    .tab-button {
        cursor: pointer;
        padding: 10px 10px;
        background-color: #ffff;
        border: 1px solid #0A5064;
        border-radius: 5px 5px 0 0;
        margin-right: 5px;
		color:#212529;
    }
    .tab-button.active {
        background-color: #212529;
		color:#fff;
    }

    /* Background colors for tab content */
    #tab1 {
        background-color: #fff;
		color:#212529;
		padding:20px;
    }
    #tab2 {
        color:#212529;
		background-color: ;
    }
    #tab3 {
		color:#212529;
        background-color: ;
    }
    
    /* Custom styles for search bar */
    .search-container {
        display: flex;
        align-items: center;
       width:100%;
        margin: 2px auto;

        float:left;
	}

/* Updated hide class to apply to the overlay */
.sidebar.hide, .overlay.hide {
    transform: translateX(-100%);
}

    .search-input {
        width: 100%;
        padding: 10px 30px 10px 10px;
        /* Adjust padding to accommodate the icon */
        border: 1px solid #ccc;
        border-radius: 20px;
        font-size: 16px;
    }

    .search-icon {
        position: relative;
        left: -25px;
        /* Adjust the position of the icon */
        color: #aaa;
    }

    .search-icon i {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
    }
.form-control, option{
    font-weight:700;
    color:#000;
}   
.btn{
    font-weight:700;
}
  </style>

<body id="page-top">

    <!-- Page Wrapper -->
    <div id="wrapper">

        <!-- Sidebar -->
        <?php
        include("common/sd.php");
		?>
        <!-- End of Sidebar -->

        <!-- Content Wrapper -->
        <div id="content-wrapper" class="d-flex flex-column">

            <!-- Main Content -->
            <div id="content">
                <nav class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow">

                    <button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3">
                        <i class="fa fa-bars"></i>
                    </button>

                     <?php include('common/log.php');  ?>
                </nav>
<br />

<div class="container">
        <div class="row">
            <div class="col-sm-5">
                <h3 style="color:#212529; font-weight:700;">Expense </h3>
                
            </div>
            <div class="col-sm-1"></div>
            <div class="col-sm-4 d-flex flex-row-reverse align-items-center justify-content-evenly">
                <div class="row">
                
            <div class="col-sm-12">
            <!--<button type="button" class="btn btn-dark btn-lg font-weight-bold" -->
            <!--data-toggle="modal" data-target="#addPurchaseModal" style="float:right;">Add Cash Expense</button>-->
            </div>
            
            </div>
            <div class="col-sm-2">
                <a href="view_expbyhead.php" class="btn btn-primary btn-lg"><i class="fas fa-arrow-left fa-1x"></i></a>
            </div>
        </div>
    </div>
  
    <div class="container">
        <div class="row">
        
            <div class="col-sm-12" style="" id="table-container-wrapper">
                <div class="row text-center">
                    <!-- Search container -->
                    <div class="search-container">
                       <input type="text" id="searchInput"
                       class="form-control" style="width:405px;" placeholder="Search...">

                        <div class="search-icon">
                            <i class="fas fa-search"></i>
                        </div>
                    </div>
                    <br />
                    <table class="table table-striped" style="margin-top:20px;">
    <thead>
        <tr style="background:#212529;font-weight:700;" class="text-white text-capitalize">
            <th>Date</th>
            <th>Expense</th>
            <th>Paid By</th>
            <th>Amount(Rs)</th>
            <th>Description</th>
            <th>By</th>
            <th>Delete</th>
        </tr>
    </thead>
    <tbody id="dataRows">
        <?php
        $rec = mysqli_query($connection, "SELECT * FROM incash where name_detail = '$expName' ORDER BY date_time DESC");
        while ($row = mysqli_fetch_array($rec)) {
        ?>
            <tr class="bg-white font-weight-bold">
                <td><?php echo $row["date_time"]; ?></td>
                <td class="text-capitalize" style="font-weight:700;"><?php echo $row["name_detail"]; ?></td>
                <td class="text-capitalize" style="font-weight:700;"><?php echo $row["paid_by"]; ?></td>
                <td style="font-weight:700;"><?php echo $row["amount"]; ?></td>
                <td style="font-weight:700;"><?php echo $row["desc_name"]; ?></td>
                <td class="text-capitalize"><?php echo $row["login_user"]; ?></td>
                <td>
                    <form method="post" onsubmit="return confirmDelete(this, event);">
                        <input type="hidden" name="delete_id" value="<?php echo $row['id']; ?>">
                        <button type="submit" class="btn btn-danger btn-sm">
                            <i class="fas fa-trash fa-1x"></i>
                        </button>
                    </form>
                </td>
            </tr>
        <?php } ?>
    </tbody>
</table>
                </div>
            </div>
         
            
        </div>
          
                <!-- Modal for adding purchases -->
            <div class="modal fade" id="addPurchaseModal" tabindex="-1" role="dialog" 
            aria-labelledby="addPurchaseModalLabel" aria-hidden="true" style="margin-top:80px;">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header" style="background:#0A4657;">
                            <h5 class="modal-title text-capitalize text-white font-weight-bold" id="addPurchaseModalLabel">Add Cash Expense</h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <div class="modal-body" >
                            <!-- Form for adding purchase -->
                            <form method="post" action="expence.php?exp_name=<?php echo $expName; ?>" enctype="multipart/form-data">
                            
                                <input type="text" name="desc" placeholder="Description / Name" class="form-control" value="<?php echo $expName;?>" readonly />
                                <br />
                                <input type="text" name="amount" placeholder="Amount" class="form-control" required/>
                                <br />
                                <select name="paid_by" class="form-control">
                                <option value="0">Select</option>
                                <option value="cash">Cash</option>
                                <option value="bank1">Bank 1</option>
                                    <option value="bank2">Bank 2 </option>
                                    <option value="bank3">Bank 3</option>
                                    <option>Others</option>
                                </select>
                                <br />
                                <input type="date" name="setdate" class="form-control"  value="<?php echo date('Y-m-d'); ?>" />
                                <br />
                                <textarea name="description" class="form-control" placeholder="Enter Description here"></textarea>
                                <br />
                                <button type="submit"  class="btn btn-dark" name="expence_add">Enter</button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
            
    </div>
</div>


            </div>
            <br><br>
            <!-- End of Main Content -->

            <!-- Footer -->
            <?php include("common/main_ft.php");  ?>
            <!-- End of Footer -->

        </div>
        <!-- End of Content Wrapper -->

    </div>
    <a class="scroll-to-top rounded" href="#page-top">
        <i class="fas fa-angle-up"></i>
    </a>

    <!-- Logout Modal-->
    <div class="modal fade" id="logoutModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Ready to Leave?</h5>
                    <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                </div>
                <div class="modal-body">Select "Logout" below if you are ready to end your current session.</div>
                <div class="modal-footer">
                    <button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
                    <a class="btn btn-primary" href="">Logout</a>
                </div>
            </div>
        </div>
    </div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
   <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js"></script>
    <!-- Bootstrap core JavaScript-->
    <script src="../vendor/jquery/jquery.min.js"></script>
    <script src="../vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

    <!-- Core plugin JavaScript-->
    <script src="../vendor/jquery-easing/jquery.easing.min.js"></script>

    <!-- Custom scripts for all pages-->
    <script src="../js/sb-admin-2.min.js"></script>
    <!-- SweetAlert -->
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <script>
    function confirmDelete(form, event) {
        event.preventDefault();
        Swal.fire({
            title: 'Are you sure?',
            text: "This record will be deleted permanently!",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#d33',
            cancelButtonColor: '#3085d6',
            confirmButtonText: 'Yes, delete it!'
        }).then((result) => {
            if (result.isConfirmed) {
                form.submit();
            }
        });
        return false;
    }
    </script>
    <script>
$(document).ready(function () {
    $("#searchInput").on("keyup", function () {
        var value = $(this).val().toLowerCase();
        $("#dataRows tr").filter(function () {
            $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1);
        });
    });
});


    </script>
</body>

</html>