<?php
$frontend = $this->uri->segment(1);
$mid = my_id();
if (!empty($mid) && $frontend != 'frontend') { ?>
    <div class="chat_frame">
        <button type="button" class="fixed bottom-6 right-6 z-50 inline-flex h-12 w-12 items-center justify-center rounded-full bg-indigo-600 text-white shadow-lg hover:bg-indigo-500" id="open_chat_list">
            <i data-lucide="message-circle" class="h-5 w-5"></i>
        </button>
        <div class="fixed bottom-6 right-6 z-50 w-80 translate-y-0 rounded-2xl border border-border bg-card shadow-xl hidden" id="chat_list">
            <div class="flex items-center justify-between rounded-t-2xl border-b border-border bg-muted/40 px-4 py-3">
                <div class="text-sm font-semibold text-foreground"><?= lang('users') . ' ' . lang('list') ?></div>
                <button type="button" id="close_chat_list" class="text-muted-foreground hover:text-foreground" aria-label="<?= lang('close') ?>">
                    <i data-lucide="x" class="h-4 w-4"></i>
                </button>
            </div>
            <div class="max-h-[420px] overflow-y-auto">
                <?php
                $users = $this->admin_model->get_online_users();
                if (!empty($users)) {
                    foreach ($users as $key => $v_users) {
                        if (!empty($v_users)) {
                            foreach ($v_users as $v_user) {
                                ?>
                                <button type="button" data-user-id="<?= $v_user->user_id ?>" data-chat-start
                                        class="flex w-full items-center gap-3 border-b border-border px-4 py-3 text-left hover:bg-muted/40 start_chat">
                                    <div class="relative">
                                        <img src="<?= base_url($v_user->avatar) ?>" alt="Image" class="h-10 w-10 rounded-full object-cover">
                                        <?php if ($key == 'online') { ?>
                                            <span class="absolute -right-0.5 -bottom-0.5 h-3 w-3 rounded-full border-2 border-card bg-emerald-500"></span>
                                        <?php } else { ?>
                                            <span class="absolute -right-0.5 -bottom-0.5 h-3 w-3 rounded-full border-2 border-card bg-amber-400"></span>
                                        <?php } ?>
                                    </div>
                                    <div class="min-w-0 flex-1">
                                        <div class="text-sm font-semibold text-foreground truncate"><?= $v_user->fullname ?></div>
                                        <div class="flex items-center justify-between text-xs text-muted-foreground">
                                            <span class="truncate"><?= $v_user->designations ?></span>
                                            <span class="ml-2"><?php
                                                if (!empty($v_user->online_time)) {
                                                    echo time_ago($v_user->online_time);
                                                } else {
                                                    echo lang('never');
                                                } ?></span>
                                        </div>
                                    </div>
                                </button>
                                <?php
                            }
                        }
                    }
                } ?>
            </div>
        </div>
        <div id="chat_box" class="fixed bottom-24 right-6 z-40 space-y-3"></div>
        <audio id="chat-tune" class="hidden" controls="">
            <source src="<?= base_url() ?>assets/plugins/chat/chat_tune.mp3" type="audio/mpeg">
        </audio>
        <script src="<?= base_url() ?>assets/plugins/chat/chat.js"></script>
    </div><!--End live_chat_section-->
<?php } ?>
