<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css" />
<!-- Begin Page Content -->
<style>
video {
    width: 100%
}

table {
    width: 100%;
    border-collapse: collapse;
}

td,
th {
    padding: 8px;
    text-align: left;
}

.fw-bold {
    font-weight: 700;
    color: #000;

}

.dt-buttons {
    margin: 1rem;
}

.dataTables_filter {
    margin: 1rem;
}

@media (max-width: 768px) {

    /* Change table layout to block on small screens */
    table,
    thead,
    tbody,
    th,
    td,
    tr {
        display: block;
    }

    /* Hide the table headers */
    thead tr {
        display: none;
    }

    /* Display each row as a card */
    tr {
        margin-bottom: 8px;
        border-bottom: 2px solid #bbb;
    }

    /* Label each cell for clarity */

}
</style>
<div class="container-fluid">
    <div class="row column_title">
        <div class="col-md-12">
            <div class="page_title">
                <h2>Riders</h2>
            </div>
        </div>
    </div>

    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <!-- <h6 class="m-0 font-weight-bold text-primary">DataTables Example</h6> -->
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-striped" id="songTable">
                    <thead class="thead-dark">
                        <tr>
                            <th>Image</th>
                            <th>Name</th>
                            <th>Email</th>
                            <th>License Number</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>
                                <img class="img-thumbnail" src="http://placehold.jp/100x100.png">
                            </td>
                            <td>John Doe</td>
                            <td>john@gmail.com</td>
                            <td>GaG-1123-HeJ</td>
                            <td>
                                <button class="btn btn-secondary btn-block mt-2" onclick="window.location.href='<?=base_url('dashboard/riderprofile')?>'">Profile</button>
                                <button class="btn btn-info btn-block mt-2" onclick="window.location.href='<?=base_url('dashboard/vehicles')?>'">Vehicles</button>
                                <button class="btn btn-warning btn-block mt-2" data-toggle="modal" data-target="#exampleModal" onclick="suspend('1','John Doe');">Suspend</button>
                                <button class="btn btn-primary btn-block mt-2" onclick="window.location.href='<?=base_url('dashboard/usersh')?>'">Suspension History</button>

                                <button class="btn btn-danger btn-block mt-2">Delete</button>
                            </td>
                        </tr>


                        <?php
                        if (!empty($customerlist)) {



                            $count = 1;
                            foreach ($customerlist["user"] as $id => $customerlists) {
                                if ($customerlists['email'] == 'admin@example.com') {
                                    continue;
                                }

                        ?>
                        <tr>

                            <td><?php echo $customerlists['name']; ?></td>


                            <td>
                                <?php echo output($customerlists['email']); ?><br><br>
                                <?php echo output($customerlists['loginMethod']); ?>
                            </td>

                            <td><?php echo output($customerlists['language']); ?></td>
                            <td><?php echo output($customerlists['platform']); ?></td>
                            <td><?php echo output($customerlists['appVersion']); ?></td>
                            <td><?php echo date('Y-m-d', $customerlists['lastActiveDate']/1000); ?></td>
                            <!-- <td>
                                <?php if ($customerlists['image'] != "") { ?>
                                <a href="<?= $customerlists['image'] ?>" target="_blank">
                                    <img class="img-thumbnail" width="100" height="100" src="<?php echo output($customerlists['image']); ?>">
                                </a>
                                <?php } else { ?>
                                <img class="img-thumbnail" src="http://placehold.jp/100x100.png">
                                <?php } ?>
                            </td> -->
                            <td>
                                <!-- <a class="icon" style="text-decoration: none;" href="<?php echo base_url(); ?>customer/userLogs/<?php echo output($id); ?>">
                                    <button class="btn btn-primary btn-block mt-2">User Details</button>
                                </a> -->

                                <!-- <?php if ($customerlists['isActive'] === false) { ?>
                                <a class="icon" style="text-decoration: none;" href="<?php echo base_url(); ?>customer/approve/<?php echo output($id); ?>/user">
                                    <button class="btn btn-success btn-block mt-2" disabledstyle="color:black">Approve</button>
                                </a>
                                <?php } else { ?>
                                <a class="icon" style="text-decoration: none;" href="<?php echo base_url(); ?>customer/suspend/<?php echo output($id); ?>/user">
                                    <button class="btn btn-warning btn-block mt-2">Suspend</button>
                                </a>
                                <?php } ?> -->

                                <button class="btn btn-primary btn-block mt-2" onclick="showDetails('<?php echo output($id); ?>')">Details</button>
                                <button class="btn btn-danger btn-block mt-2" data-toggle="modal" data-target="#exampleModa2" onclick="showConfirmation('<?php echo output($id); ?>','<?= $customerlists['name'] ?>', 'deleteCustomer');">Delete</button>

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

</div>
<!-- /.container-fluid -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <form method="post" action="<?= base_url('customer/suspend') ?>">
        <input id="userId" type="hidden" name="userId" value="" />
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 id="exampleModalLabel">Suspend user "<span id="user-name">"</span>
                    </h4>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label for="message-text" class="col-form-label">Reason:</label>
                        <input type="text" class="form-control" id="reason" name="reason" required>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary" data-dismiss="modal">Suspend</button>
                    <!-- <button type="submit" class="btn btn-primary">Suspend</button> -->
                </div>
            </div>
        </div>
    </form>
</div>
<div class="modal fade" id="exampleModa2" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <form method="get" name="cform" action="">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel2"></h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary" id="submitBtn"></button>
                </div>
            </div>
        </div>
    </form>
</div>
<script>
function showDetails(userId) {
    window.location.href = '<?=base_url('dashboard/userdetails/')?>' + userId;
}

function suspend(id, username) {
    console.log(id, username)
    document.getElementById('user-name').innerText = username;
    document.getElementById('userId').value = id;
}

function showConfirmation(userid, username, action) {
    let url = "";
    let message = "";
    let btnTitle = "";
    switch (action) {
        case 'deleteBanner':
            url = "<?php echo base_url(); ?>customer/deleteBanner/" + userid;
            message = "Are you sure you want to remove banner of user '" + username + "'?";
            btnTitle = "Remove Banner";
            break;
        case 'deleteImage':
            url = "<?php echo base_url(); ?>customer/deleteImage/" + userid;
            message = "Are you sure you want to remove profile phot of user '" + username + "'?";
            btnTitle = "Remove Profile Photo";
            break;
        case 'banCustomer':
            url = "<?php echo base_url(); ?>customer/banCustomer/" + userid;
            message = "Are you sure you want to ban user '" + username + "'?";
            btnTitle = "Ban User";
            break;
        case 'unBanCustomer':
            url = "<?php echo base_url(); ?>customer/unBanCustomer/" + userid;
            message = "Are you sure you want to Unbanned user '" + username + "'?";
            btnTitle = "Unban User";
            break;
        case 'suspendCustomer':
            url = "<?php echo base_url(); ?>customer/suspendCustomer/" + userid;
            message = "Are you sure you want to deactivate user '" + username + "'?";
            btnTitle = "Deactivate User";
            break;
        case 'unSuspendCustomer':
            url = "<?php echo base_url(); ?>customer/unSuspendCustomer/" + userid;
            message = "Are you sure you want to activate user '" + username + "'?";
            btnTitle = "Activate User";
            break;
        case 'deleteCustomer':
            url = "<?php echo base_url(); ?>customer/remove/" + userid + "/user";
            message = "Are you sure you want to delete user '" + username + "'?";
            btnTitle = "Delete User";
            break;
        default:
            return false;
    }

    document.getElementById('exampleModalLabel2').innerText = message;
    document.getElementById('submitBtn').innerText = btnTitle;
    document.cform.action = url;
}



$(document).ready(function() {
    $('#songTable').DataTable();
});

// Custom sorting function
function sortTable(columnIndex) {
    var table = $('#songTable').DataTable();
    var currentOrder = table.order();

    // Toggle sorting order based on current state
    if (currentOrder[0][0] === columnIndex) {
        // Reverse the order if already sorted on this column
        var newOrder = currentOrder[0][1] === 'asc' ? 'desc' : 'asc';
        table.order([columnIndex, newOrder]).draw();
    } else {
        // Sort in ascending order if sorting for the first time
        table.order([columnIndex, 'asc']).draw();
    }
}
</script>