<?php
session_start();
include '../config.php';
$f_date = $_POST['f_date'];
$t_date = $_POST['t_date'];
// $company_id = $_POST['company_id'];
// $user_id = $_POST['user_id'];
// $module = $_POST['module'];

$fields = array('company_id', 'user_id', 'module');
$conditions = array();
foreach($fields as $field){
    // if the field is set and not empty
    if(isset($_POST[$field]) && $_POST[$field] != '') {
        // create a new condition while escaping the value inputed by the user (SQL Injection)
        $conditions[] = "`$field` = $_POST[$field]" ;
    }
}
 // builds the query
 $query = "SELECT * FROM `activity_log` WHERE `login_date_time` BETWEEN '$f_date' AND '$t_date'";
 // if there are conditions defined
 if(count($conditions) > 0)
{
     // append the conditions
     $query .= "AND" . implode (' AND ', $conditions); // you can change to 'OR', but I suggest to apply the filters cumulative
}
// var_dump($query);die;

$search_query = mysqli_query($con,$query);
$rows = mysqli_num_rows($search_query); 
if($rows > 0)
{
    $i = 1;
    $total_csv = 0;
    $total_excel = 0;
    while($get_result = mysqli_fetch_array($search_query))
    {
        $s = "";
        $t = "";
        $v = "";
        $sql_stmt1 = "SELECT * FROM user_master "; 
        $result1 = mysqli_query($con,$sql_stmt1);
        while($row1 = mysqli_fetch_array($result1))
        { 
            if($row1['id'] == $get_result['user_id'])
            { 
                $s = $row1['username']; 
            }
        }   
        $sql_stmt3 = "SELECT * FROM company_master"; 
        $result3 = mysqli_query($con,$sql_stmt3);
        while($row1 = mysqli_fetch_array($result3))
        { 
            if($row1['id'] == $get_result['company_id'])
            { 
                $t = $row1['company_name']; 
            }
        }
        if($get_result['module'] == 1)
        { 
            $v = "Import"; 
        }        
        else
        {
            $v = "Export"; 
        }    
        $output .= '<tr>
                        <td>'.$i .'</td>
                        <td>'. $s .'</td>
                        <td>'. $t .'</td>
                        <td>'. $v .'</td>
                        <td>'.date('d-m-Y',strtotime($get_result['login_date_time'])).'</td>
                        <td>'.$get_result['public_ip'].'</td>
                        <td>'.$get_result['csv_generated'].'</td>
                        <td>'.$get_result['no_of_shipments'].'</td>';
        $output .= '</tr>';
        $i = $i + 1;
        $total_csv += $get_result['csv_generated'];
        $total_excel += $get_result['no_of_shipments']; 
    }
    $output .= '<tr>
                <td colspan=6>Total CSV and Manifests :</td>
                <td>'.$total_csv.'</td>
                <td>'.$total_excel.'</td>';
    $output .= '</tr>';    
}
else
{
    $output .= '<tr>
                    <td colspan = 8 style="text-align-last: center;">No Result Found</td>
                </tr>';
}
echo $output;
?>