<?php
if ($_SERVER['HTTP_HOST'] != 'errands-form.dev') {
    $auth = 0;
    if (!isset($_SERVER['PHP_AUTH_USER'])) {
        header('WWW-Authenticate: Basic realm="ASB PING Data Records"');
        header('HTTP/1.0 401 Unauthorized');
        print "you need to login";
        exit;
    } else {
        if ($_SERVER['PHP_AUTH_USER'] == "admin" and $_SERVER['PHP_AUTH_PW'] == "asbping") {
            $auth = 1;
        } else {
            header('WWW-Authenticate: Basic realm="ASB PING Data Records"');
            header('HTTP/1.0 401 Unauthorized');
            print "you need to login";
            exit;
        }
    }

    if ($auth == 0) {
        print "you need to login";
        exit;
    }
}

include "MySqliDb.php";
if ($_SERVER['HTTP_HOST'] == 'mcc-forms.dev') {
    // DB credentials.
    define('DB_HOST', '127.0.0.1');
    define('DB_USER', 'root');
    define('DB_PASS', 'ServBay.dev');
    define('DB_NAME', 'masterco_forms_data');
} else {
    // DB credentials.
    define('DB_HOST', 'localhost');
    define('DB_USER', 'masterco_forms_data');
    define('DB_PASS', 'masterco_forms_data');
    define('DB_NAME', 'masterco_forms_data');
}
$db = new db(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$stmt = $db->query("SELECT * FROM `asb_ping`  ORDER BY `uid` DESC LIMIT 5000");
$records = $db->fetchAll($stmt);
//print_r($records); exit;

?>
<title>ACA RTB - Data Records</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.dataTables.min.js"></script>
<link rel="stylesheet" href="https://getbootstrap.com/docs/4.3/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.4/css/jquery.dataTables.min.css">


<div style="width: 80%; margin: 50px auto 100px;">
    <h3>ASB Ping - Data Records</h3><br>

    <table id="example" class="display" style="width: 100%;">
        <thead>
            <tr>
                <th>Sr. No</th>
                <th>Date/Time</th>
                <th>Requested Phone</th>
                <th>Status</th>
            </tr>
        </thead>
        <tbody><?php
                if (count($records) > 0) {
                    $cnt = 1;
                    foreach ($records as $record) {
                        echo '
                        <tr ' . (($record['status'] == "Ping Accepted") ? 'style="background-color:#d4edda; color: #155724;"' : '') . '>
                            <td>' . $cnt . '</td>
                            <td>' . date("d M, Y h:i A", strtotime($record['system_date'])) . '</td>
                            <td>' . $record['phone'] . '</td>
                            <td>' . $record['status'] . '</td>
                        </tr>';
                        $cnt++;
                    }
                } ?>
        </tbody>
        <tfoot>
            <tr>
                <th>Sr. No</th>
                <th>Date/Time</th>
                <th>Requested Phone</th>
                <th>Status</th>
            </tr>
        </tfoot>
    </table>
</div>
<script>
    $(document).ready(function() {
        $('#example').DataTable({
            lengthMenu: [
                [100, 250, 500, -1],
                [100, 250, 500, 'All'],
            ],
        });
    });
</script>