<?php
$type = $_GET['type'];

switch ($type) {
    case 'emailreceiving':
        $query = "SELECT `NIK`, `NM`, `EMAIL`, `DIV`, `LOG_RECEIVE` FROM `tbl_securitymail` WHERE `LOG_RECEIVE` IS NOT NULL";
        $logColumn = 'LOG_RECEIVE';
        break;
    case 'openmail':
        $query = "SELECT `NIK`, `NM`, `EMAIL`, `DIV`, `LOG_OPENEMAIL` FROM `tbl_securitymail` WHERE `LOG_OPENEMAIL` IS NOT NULL";
        $logColumn = 'LOG_OPENEMAIL';
        break;
    case 'clicklink':
        $query = "SELECT `NIK`, `NM`, `EMAIL`, `DIV`, `LOG_OPENLINK` FROM `tbl_securitymail` WHERE `LOG_OPENLINK` IS NOT NULL";
        $logColumn = 'LOG_OPENLINK';
        break;
    case 'openweb':
        $query = "SELECT `NIK`, `NM`, `EMAIL`, `DIV`, `LOG_OPENWEB` FROM `tbl_securitymail` WHERE `LOG_OPENWEB` IS NOT NULL";
        $logColumn = 'LOG_OPENWEB';
        break;
    default:
        $query = "";
        $logColumn = '';
        break;
}

if ($query !== "") {
// Koneksi ke database
    $servername = "localhost";
    $username = "zonb9946_weliington";
    $password = "#Bismillah16";
    $dbname = "zonb9946_email";

$conn = new mysqli($servername, $username, $password, $dbname);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

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

    if ($result->num_rows > 0) {
        echo '<table class="table table-hover">';
        echo '<thead><tr><th>No</th><th>NIK</th><th>Name</th><th>Email</th><th>Division</th><th>Log</th></tr></thead>';
        echo '<tbody>';
        $no = 1;
        while($row = $result->fetch_assoc()) {
            echo '<tr>';
            echo '<td>' . $no . '</td>';
            echo '<td>' . $row["NIK"] . '</td>';
            echo '<td>' . $row["NM"] . '</td>';
            echo '<td>' . $row["EMAIL"] . '</td>';
            echo '<td>' . $row["DIV"] . '</td>';
            echo '<td>' . $row[$logColumn] . '</td>';
            echo '</tr>';
            $no++;
        }
        echo '</tbody>';
        echo '</table>';
    } else {
        echo "No results found.";
    }
    $conn->close();
} else {
    echo "Invalid type.";
}
?>
