<?php
$pageHead = 'All Leads Records';

$Clients = array();
$Clients['m57'] = 'M5/M7-SQ';
$Clients['m2'] = 'M2-SVG';
$Clients['m3'] = 'M3-GH';
$Clients['m4'] = 'M4-GH';
$Clients['m34'] = 'M3/M4-GH';
$Clients['justdata'] = 'MH1/M1/M14';
?>

<link href="<?= TEMPLATE ?>vendor/datatables/dataTables.bootstrap4.min.css" rel="stylesheet">

<!-- Content Wrapper -->
<div id="content-wrapper" class="d-flex flex-column">

    <!-- Main Content -->
    <div id="content">

        <!-- Topbar -->
        <nav class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow">
            <button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3">
                <i class="fa fa-bars"></i>
            </button>
            <img src="https://errandsservices.com/wp-content/uploads/2018/03/errands-services-logo-02.png"
                 style="height: 45px;">
        </nav>
        <!-- End of Topbar -->

        <!-- Begin Page Content -->
        <div class="container-fluid">
            <!-- Page Heading -->
            <div class="d-sm-flex align-items-center justify-content-between mb-4">
                <h1 class="h3 mb-0 text-gray-800"><?= $pageHead ?></h1>
            </div>
            <div class="card shadow mb-4">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">Leads Records</h6>
                </div>
                <div class="card-body">
                    <div class="table-responsive text-nowrap" style="overflow:auto">
                        <table class="table table-bordered dataTable" width="100%" cellspacing="0">
                            <thead>
                            <tr>
                                <th>Action</th>
                                <th>Status</th>
                                <th>Client</th>
                                <th>Submit DateTime</th>
                                <th>Full Name</th>
                                <th>Phone</th>
                                <th>Email</th>
                                <th>DOB</th>
                                <th>Zip Code</th>
                                <th>State</th>
                                <th>Agent Name</th>
                                <th>LeadID</th>
                                <th>API LeadID</th>

                            </tr>
                            </thead>
                            <tfoot>
                            <tr>
                                <th>Action</th>
                                <th>Status</th>
                                <th>Client</th>
                                <th>Submit DateTime</th>
                                <th>Full Name</th>
                                <th>Phone</th>
                                <th>Email</th>
                                <th>DOB</th>
                                <th>Zip Code</th>
                                <th>State</th>
                                <th>Agent Name</th>
                                <th>LeadID</th>
                                <th>API LeadID</th>

                            </tr>
                            </tfoot>
                            <tbody>
                            <?php
                            if (count($records) > 0) {
                                foreach ($records as $record) { ?>
                                <tr id="row_<?= $record['id'] ?>"
                                    <?= (($record['status'] == 'submit_for_411') ? 'class="alert-warning"' : '') ?>
                                    <?= (($record['status'] == 'final') ? 'class="alert-success"' : '') ?>
                                >
                                    <td>
                                        <button onclick="LoadLead(<?= $record['id'] ?>)"
                                                class="btn btn-outline-primary btn-sm">View
                                        </button>
                                    </td>
                                    <td><?= ucwords(str_replace("_", ' ', $record['status'])) ?></td>
                                    <td><?= ucwords($Clients[$record['clients']]) ?></td>
                                    <td><?= date("d M, Y - h:i A", strtotime($record['system_datetime'])) ?></td>
                                    <td><?= $record['first_name'] ?>&nbsp;&nbsp;<?= $record['last_name'] ?></td>
                                    <td><?= $record['phone'] ?></td>
                                    <td><?= $record['email'] ?></td>
                                    <td><?= (($record['dob'] == '1970-01-01') ? '' : date("d M, Y - h:i A", strtotime($record['dob']))) ?></td>
                                    <td><?= $record['zip'] ?></td>
                                    <td><?= $record['state'] ?></td>
                                    <td><?= $record['agent_name'] ?></td>
                                    <td><?= $record['lead_id'] ?></td>
                                    <td><?= $record['api_lead_id'] ?></td>

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

        </div>
        <!-- /.container-fluid -->

    </div>
    <!-- End of Main Content -->

    <!-- Footer -->
    <footer class="sticky-footer bg-white">
        <div class="container my-auto">
            <div class="copyright text-center my-auto">
                <span>Copyright &copy; Errands Services</span>
            </div>
        </div>
    </footer>
    <!-- End of Footer -->

</div>
<!-- End of Content Wrapper -->
<?php echo view("load_lead"); ?>

<!-- Page level plugins -->
<script src="<?= TEMPLATE ?>vendor/datatables/jquery.dataTables.min.js"></script>
<script src="<?= TEMPLATE ?>vendor/datatables/dataTables.bootstrap4.min.js"></script>
<script src="<?= TEMPLATE ?>vendor/datatables/dataTables.buttons.min.js"></script>
<script src="<?= TEMPLATE ?>vendor/datatables/jszip.min.js"></script>
<script src="<?= TEMPLATE ?>vendor/datatables/pdfmake.min.js"></script>
<script src="<?= TEMPLATE ?>vendor/datatables/vfs_fonts.js"></script>
<script src="<?= TEMPLATE ?>vendor/datatables/buttons.html5.min.js"></script>

<script>
    // Call the dataTables jQuery plugin
    $(document).ready(function () {
        $('table.dataTable').DataTable({
            dom: 'Bfrtip',
            pageLength: 50,
            buttons: [
                'copyHtml5',
                'excelHtml5',
                'csvHtml5',
                'pdfHtml5'
            ]
        });
    });

    function LoadLead(uid) {
        AjaxRequest("html_load_lead", "uid=" + uid, 'LoadLeadModal .modal-content');
        $('#LoadLeadModal').modal({
            show: true,
            backdrop: false,
            keyboard: false
        });
    }

    function ChangeLeadStatus(id, status) {
        AjaxRequest("html_update_lead_status", "uid=" + id + "&status=" + status, 'LoadLeadModal .modal-content #LeadStatusAjaxResponse');
        setTimeout(function () {
            location.reload();
        }, 1000);

    }

</script>