<?= message_box('success'); ?>
<?= message_box('error'); ?>
<?php
$created = can_action('149', 'created');
$edited = can_action('149', 'edited');
$deleted = can_action('149', 'deleted');
?>
<div class="rounded-2xl border border-border bg-card shadow-sm">
    <div class="border-b border-border bg-muted/50 px-6 py-4">
        <div class="flex items-center gap-2">
            <i class="fa fa-shield-alt text-primary"></i>
            <h3 class="text-lg font-semibold text-foreground"><?= lang('allowed_ip') ?></h3>
        </div>
    </div>
    <div class="p-6">
        <div class="overflow-hidden rounded-lg border border-border">
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-border">
                    <thead class="bg-muted/50">
                        <tr>
                            <th class="px-6 py-3 text-left text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('allowed_ip') ?></th>
                            <th class="px-6 py-3 text-left text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('status') ?></th>
                            <?php if (!empty($edited) || !empty($deleted)) { ?>
                                <th class="col-options no-sort px-6 py-3 text-right text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('action') ?></th>
                            <?php } ?>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-border bg-background">
                    <?php
                    $all_allowed_ip = $this->db->get('tbl_allowed_ip')->result();
                    if (!empty($all_allowed_ip)) {
                        foreach ($all_allowed_ip as $allowed_ip) {
                            $is_editing = !empty($this->uri->segment(5)) && $this->uri->segment(5) == $allowed_ip->allowed_ip_id;
                            $status_value = !empty($is_editing) && !empty($allowed_ip_info) ? $allowed_ip_info->status : $allowed_ip->status;
                            if (empty($status_value)) {
                                $status_value = 'active';
                            }
                            $status_styles = array(
                                'active' => 'bg-emerald-500/10 text-emerald-600 ring-1 ring-emerald-500/20',
                                'reject' => 'bg-rose-500/10 text-rose-600 ring-1 ring-rose-500/20',
                                'pending' => 'bg-amber-500/10 text-amber-600 ring-1 ring-amber-500/20',
                            );
                            $status_labels = array(
                                'active' => lang('active'),
                                'reject' => lang('reject'),
                                'pending' => lang('pending'),
                            );
                            $status_class = isset($status_styles[$status_value]) ? $status_styles[$status_value] : $status_styles['active'];
                            $status_label = isset($status_labels[$status_value]) ? $status_labels[$status_value] : ucfirst($status_value);
                            ?>
                            <tr id="allowed_ip_<?= $allowed_ip->allowed_ip_id ?>" class="hover:bg-muted/30 transition-colors">
                                <td class="px-6 py-4">
                                    <?php if ($is_editing) { ?>
                                    <form method="post"
                                          action="<?= base_url() ?>admin/settings/allowed_ip/update_allowed_ip/<?php
                                          if (!empty($allowed_ip_info)) {
                                              echo $allowed_ip_info->allowed_ip_id;
                                          }
                                          ?>" class="inline-block w-full">
                                        <input type="text" name="allowed_ip" value="<?php
                                        if (!empty($allowed_ip_info)) {
                                            echo $allowed_ip_info->allowed_ip;
                                        }
                                        ?>" class="w-full rounded-lg border border-border bg-background px-3 py-2 text-sm text-foreground focus:border-primary focus:outline-none focus:ring-2 focus:ring-primary/20" placeholder="<?= lang('allowed_ip') ?>" required>
                                    <?php } else { ?>
                                        <span class="text-sm font-mono text-foreground"><?= $allowed_ip->allowed_ip ?></span>
                                    <?php } ?>
                                </td>
                                <td class="px-6 py-4">
                                    <?php if ($is_editing) { ?>
                                        <select name="status" class="w-full max-w-xs rounded-lg border border-border bg-background px-3 py-2 text-sm text-foreground focus:border-primary focus:outline-none focus:ring-2 focus:ring-primary/20">
                                            <?php
                                            if (!empty($allowed_ip_info) && $allowed_ip_info->status == 'pending') {
                                                ?>
                                                <option value="pending" <?= !empty($allowed_ip_info) && $allowed_ip_info->status == 'pending' ? 'selected' : '' ?>><?= lang('pending') ?></option>
                                            <?php } ?>
                                            <option value="active" <?= !empty($allowed_ip_info) && $allowed_ip_info->status == 'active' ? 'selected' : '' ?>><?= lang('active') ?></option>
                                            <option value="reject" <?= !empty($allowed_ip_info) && $allowed_ip_info->status == 'reject' ? 'selected' : '' ?>><?= lang('reject') ?></option>
                                        </select>
                                    <?php } else { ?>
                                        <span class="inline-flex items-center rounded-full px-3 py-1 text-xs font-semibold <?= $status_class ?>">
                                            <?= $status_label ?>
                                        </span>
                                    <?php } ?>
                                </td>
                                <?php if (!empty($edited) || !empty($deleted)) { ?>
                                    <td class="px-6 py-4">
                                        <div class="flex items-center justify-end gap-0">
                                            <?php if ($is_editing) { ?>
                                                <?= btn_update('update','xs', 'h-7') ?>
                                                </form>
                                                <?= btn_cancel('admin/settings/allowed_ip/') ?>
                                            <?php } else { ?>
                                                <?php if (!empty($edited)) { ?>
                                                    <?= btn_edit('admin/settings/allowed_ip/edit_allowed_ip/' . $allowed_ip->allowed_ip_id) ?>
                                                <?php } ?>
                                                <?php if (!empty($deleted)) { ?>
                                                    <?php echo ajax_anchor(base_url("admin/settings/allowed_ip/delete_allowed_ip/" . $allowed_ip->allowed_ip_id), "", array("class" => "inline-flex items-center justify-center gap-1 rounded-lg bg-destructive px-2 py-2 text-xs font-medium text-white hover:bg-destructive/90 transition-colors", "title" => lang('delete'), "data-fade-out-on-success" => "#allowed_ip_" . $allowed_ip->allowed_ip_id)); ?>
                                                <?php }
                                            }
                                            ?>
                                        </div>
                                    </td>
                                <?php } ?>
                            </tr>
                            <?php
                        }
                    }
                    ?>
                    <?php if (!empty($created) || !empty($edited)) { ?>
                        <form method="post" action="<?= base_url() ?>admin/settings/allowed_ip/update_allowed_ip" data-parsley-validate="" novalidate="">
                            <tr class="bg-muted/20">
                                <td class="px-6 py-4">
                                    <input type="text" name="allowed_ip" class="w-full rounded-lg border border-border bg-background px-3 py-2 text-sm text-foreground focus:border-primary focus:outline-none focus:ring-2 focus:ring-primary/20" placeholder="<?= lang('allowed_ip') ?>" required>
                                </td>
                                <td class="px-6 py-4">
                                    <select name="status" class="w-full max-w-xs rounded-lg border border-border bg-background px-3 py-2 text-sm text-foreground focus:border-primary focus:outline-none focus:ring-2 focus:ring-primary/20">
                                        <option value="active"><?= lang('active') ?></option>
                                        <option value="reject"><?= lang('reject') ?></option>
                                    </select>
                                </td>
                                <td class="px-6 py-4">
                                    <div class="flex justify-end">
                                        <?= btn_add() ?>
                                    </div>
                                </td>
                            </tr>
                        </form>
                    <?php } ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
