
   <?php 
   session_start();
   include("header.php"); 
   if(isset($_SESSION["expense_add_error"])){
    echo "<div class='alert m-2 alert-danger alert-dismissible fade show' role='alert'>
  <strong>All Fields Are Required!!</strong>
  <button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='Close'></button>
</div>";
    session_destroy();
   }
   if(isset($_SESSION["expense_val_error"])){
    echo "<div class='alert m-2 alert-danger alert-dismissible fade show' role='alert'>
  <strong>Single quotes (') are not allowed in title.</strong>
  <button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='Close'></button>
</div>";
    session_destroy();
   }
   if(isset($_SESSION["expense_add_alert"])){
    echo "<div class='alert m-2 alert-success alert-dismissible fade show show-cat-update' role='alert'>
  <strong>Expense Added Successfully!!</strong></div>";
    session_destroy();
   }
  
   ?>
   
   
  <h3 class="p-2">All Sales</h1> 
<div class="d-flex py-3 d-flex justify-content-between">
   <form action="">
      <div class="row mx-1">
        <div class="col-lg-4 mb-2">
          <label for="" class="form-label">From:</label>
          <input type="date" class="form-control">
        </div>
        <div class="col-lg-4 mb-2">
          <label for="" class="form-label">To:</label>
          <input type="date" class="form-control">
        </div>
        <div class="col-lg-4 mb-2">
          <button class="btn btn-primary" style="margin-top:33px;">
  <i class="fa-solid fa-eye"></i>
  View
</button>
</div>
</div>
</form>
     <div class="pt-3">
      <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addProductModal"><i class="fa-solid fa-plus" ></i> Add Expense</button>
      <button class="btn btn-outline-dark"><i class="fa-solid fa-download"></i> Export</button>
     </div>
     <div class="modal fade modal-form " id="addProductModal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
  <div class="modal-dialog col-12">
    <div class="modal-content">
      <div class="modal-header">
        <h1 class="modal-title fs-5" id="staticBackdropLabel">Add New Product</h1>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        <form class="form" method="POST" action="./submissions/expensesSubmission.php">
            <div class="row">
                
                <div class="col-6">
                    <div class="mb-3">
                         <label for="" class="form-label">Title:</label>
                        <input type="text" name="add_expense_title" class="form-control" placeholder="Enter Expense">
                    </div>
                </div>
                
                    <div class="col-6">
                        <div class="mb-3">
                        <label for="" class="form-label" >Date:</label>
                         <input type="date" name="add_expense_date" class="form-control">
                    </div>
                </div>
             
                <div class="col-6">
                    <div class="mb-3">
                         <label for="" class="form-label">Cost:</label>
                      <input type="number" name="add_expense_cost" class="form-control" >
                    </div>
                </div>
               
            </div>
            <div class="modal-footer">
               <button type="submit" name="add_expense_btn" class="btn btn-primary">Add</button>
            </div>
        </form>
      </div>
    </div>
  </div>
</div>
</DIV>
  <div>
   
<p class="float-end fw-bold pt-3">Total Expenses: <span class="fw-light">0</span></p>


</div>
<table class='table table-hover rounded-3'>
  <thead>
    <tr class='table-secondary'>
      <th>ID</th>
      <th>Description</th>
      <th>Date</th>
      <th>Entry Date</th>
      <th>Cost</th>
      <th>Action</th>
      
    </th>
  </thead>
<tbody>
    <?php

 $connection = new mysqli($dbhost,$dbusername,$dbpassword,$dbname); 
if($connection->connect_error){
  die("error while connecting database".$connection->connect_error);
}

$sql="SELECT * FROM  expense";
$result=$connection->query($sql);
if(!$result){die("Invalid Query!!".$connection->error);}

while($row=mysqli_fetch_array($result)){
  ?>
  <tr class='table-light'>
  <td><?php echo "$row[ID]";?></td>
  <td><?php echo "$row[Title]";?></Dtd>
  <td><?php echo "$row[Date]";?></td>
  <td><?php echo "$row[Entry_Date]";?></td>
  <td><?php echo "$row[Cost]";?></td>
  <td>
  <button class='btn btn-primary mx-2 editExpensebtn' data-bs-toggle='modal'
  data-id="<?php echo "$row[ID]";?>" 
  data-title="<?php echo "$row[Title]";?>" 
  data-date="<?php echo "$row[Date]";?>" 
  data-entrydate="<?php echo "$row[Entry_Date]";?>" 
  data-cost="<?php echo "$row[Cost]";?>" 
  data-bs-target='#editTableModal'><i class='fa-solid fa-pen-to-square'></i> Edit Expense</button>
  <button class='btn btn-danger mx-2 dlt_expense_btn' data-id="<?php echo $row['ID'] ?>"> <i class='fa-solid fa-trash'></i> Delete Expense</button>
  </td>
  </tr>
  <div class='modal fade modal-form ' id='editTableModal' data-bs-backdrop='static' data-bs-keyboard='false' tabindex='-1' aria-labelledby='staticBackdropLabel' aria-hidden='true'>
  <div class='modal-dialog col-12'>
    <div class='modal-content'>
      <div class='modal-header'>
        <h1 class='modal-title fs-5' id='staticBackdropLabel'>Edit Expense</h1>
        <button type='button' class='btn-close' data-bs-dismiss='modal' aria-label='Close'></button>
      </div>
      <div class='modal-body'>
        <form class='form' method="POST" action="./submissions/expensesSubmission.php">
            <div class='row'>
               
                        <input type='number'
                        name="edit_expense_id"
                         class='form-control' placeholder='Enter User Id' id="editExpenseId"  hidden>
                  
                <div class='col-6'>
                    <div class='mb-3'>
                         <label for='' class='form-label'>Title:</label>
                        <input type='text' class='form-control' placeholder='Enter Name' id="editExpenseTitle" name="edit_expense_title" >
                    </div>
                </div>
               
                <div class='col-6'>
                    <div class='mb-3'>
                         <label for='' class='form-label'>Date:</label>
                        <input type='date' class='form-control' placeholder='Enter Name' id="editExpenseDate" name="edit_expense_date" >
                    </div>
                </div>
               
                <div class='col-6'>
                    <div class='mb-3'>
                         <label for='' class='form-label'>Cost:</label>
                        <input type='number' class='form-control' placeholder='Enter Cost' name="edit_expense_cost"  id="editExpenseCost">
                    </div>
                </div>
               
            </div>
            <div class='modal-footer'>
               <button type='submit' class='btn btn-primary' name="edit_expense_btn">Add</button>
            </div>
        </form>
      </div>
    </div>
  </div>
</div>
 </tr>

  <?php
}
?>
</tbody>  
</table>

<?php include("footer.php"); ?>
  
