<?php
include '../includes/connection.php';
session_start();
$id_user=$_SESSION['id'];
$branch=$_SESSION['branch_id'];

$category=$_GET['category']; 
$user=$_GET['user']; 
$from=$_GET['dfrom']; 
$to=$_GET['dto']; 

if(strlen($category)>0 && strlen($user)==0 && strlen($from)==0 && strlen($to)==0){
    $sql = "SELECT id, description, (select categoryName from expense_categories where id=expenses.category) as category, amount, date, date_created, (select name from users where id=expenses.expense_by)as name FROM expenses where category='$category' order by id DESC";

}elseif(strlen($category)>0 && strlen($user)>0 && strlen($from)==0 && strlen($to)==0){
    $sql = "SELECT id, description, (select categoryName from expense_categories where id=expenses.category) as category, amount, date, date_created, (select name from users where id=expenses.expense_by)as name FROM expenses where category='$category' && expense_by='$user' order by id DESC";

}elseif(strlen($category)>0 && strlen($user)>0 && strlen($from)>0 && strlen($to)==0){
    $sql = "SELECT id, description, (select categoryName from expense_categories where id=expenses.category) as category, amount, date, date_created, (select name from users where id=expenses.expense_by)as name FROM expenses where category='$category' && expense_by='$user' && date>='$from' order by id DESC";

}elseif(strlen($category)>0 && strlen($user)>0 && strlen($from)>0 && strlen($to)>0){
    $sql = "SELECT id, description, (select categoryName from expense_categories where id=expenses.category) as category, amount, date, date_created, (select name from users where id=expenses.expense_by)as name FROM expenses where category='$category' && expense_by='$user' && date>='$from' && date<='$to' order by id DESC";

}elseif(strlen($category)>0 && strlen($user)==0 && strlen($from)>0 && strlen($to)==0){
    $sql = "SELECT id, description, (select categoryName from expense_categories where id=expenses.category) as category, amount, date, date_created, (select name from users where id=expenses.expense_by)as name FROM expenses where category='$category' && date>='$from'  order by id DESC";

}elseif(strlen($category)>0 && strlen($user)==0 && strlen($from)>0 && strlen($to)>0){
    $sql = "SELECT id, description, (select categoryName from expense_categories where id=expenses.category) as category, amount, date, date_created, (select name from users where id=expenses.expense_by)as name FROM expenses where category='$category' && date>='$from' && date<='$to'  order by id DESC";

}elseif(strlen($category)==0 && strlen($user)==0 && strlen($from)>0 && strlen($to)>0){
    $sql = "SELECT id, description, (select categoryName from expense_categories where id=expenses.category) as category, amount, date, date_created, (select name from users where id=expenses.expense_by)as name FROM expenses where  date>='$from' && date<='$to' && branch_id='$branch' order by id DESC";

}elseif(strlen($category)==0 && strlen($user)>0 && strlen($from)==0 && strlen($to)==0){
    $sql = "SELECT id, description, (select categoryName from expense_categories where id=expenses.category) as category, amount, date, date_created, (select name from users where id=expenses.expense_by)as name FROM expenses where  expense_by='$user'  order by id DESC";

}elseif(strlen($category)==0 && strlen($user)>0 && strlen($from)>0 && strlen($to)==0){
    $sql = "SELECT id, description, (select categoryName from expense_categories where id=expenses.category) as category, amount, date, date_created, (select name from users where id=expenses.expense_by)as name FROM expenses where   expense_by='$user' && date>='$from' order by id DESC";

}elseif(strlen($category)==0 && strlen($user)==0 && strlen($from)>0 && strlen($to)==0){
    $sql = "SELECT id, description, (select categoryName from expense_categories where id=expenses.category) as category, amount, date, date_created, (select name from users where id=expenses.expense_by)as name FROM expenses where  branch_id='$branch' && date>='$from' order by id DESC";

}elseif(strlen($category)==0 && strlen($user)>0 && strlen($from)>0 && strlen($to)>0){
    $sql = "SELECT id, description, (select categoryName from expense_categories where id=expenses.category) as category, amount, date, date_created, (select name from users where id=expenses.expense_by)as name FROM expenses where  expense_by='$user' && date='$from' && date<='$to' order by id DESC";

}
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    $exn=0;
    $total=0;
    while($row = mysqli_fetch_assoc($result)) {
        $total=$total+$row['amount'];
        $exn++;
        ?>
<tr>
    <td><?php echo $exn?></td>
    <td><?php echo $row['category']; ?></td>
    <td><?php echo $row['amount']; ?></td>
    <td><?php echo $row['description']; ?></td>
    <td><?php echo $row['date']; ?></td>
    <td><?php echo $row['date_created']; ?></td>
    <td><?php echo $row['name']; ?></td>
   
</tr>
        <?php
    }
    ?><tr>
      <td colspan="2"><strong>TOTAL</strong></td>
      <td colspan="4"><strong><?php echo $total; ?></strong></td>
    </tr>
    <?php
} else {
    echo "0 results".mysqli_error($conn);
}


?>