<?php echo message_box('success') ?>
<?php echo message_box('error') ?>

<?php
$department_info = $this->db->get('tbl_departments')->result();
$department_options = array();
if (!empty($department_info)) {
    foreach ($department_info as $v_department) {
        $department_options[$v_department->departments_id] = $v_department->deptname;
    }
}
$status_info = $this->db->get('tbl_status')->result();
$status_options = array();
if (!empty($status_info)) {
    foreach ($status_info as $v_status) {
        $status_options[$v_status->status] = lang($v_status->status);
    }
}
$all_priority = $this->db->get('tbl_priority')->result();
$priority_options = array();
if (!empty($all_priority)) {
    foreach ($all_priority as $priority) {
        $priority_options[$priority->priority] = $priority->priority;
    }
}
$all_lead_source = $this->db->get('tbl_lead_source')->result();
$lead_source_options = array();
if (!empty($all_lead_source)) {
    foreach ($all_lead_source as $lead_source) {
        $lead_source_options[$lead_source->lead_source_id] = $lead_source->lead_source;
    }
}
$all_lead_status = $this->db->get('tbl_lead_status')->result();
$lead_status_options = array();
if (!empty($all_lead_status)) {
    foreach ($all_lead_status as $lead_status) {
        $lead_type = '';
        if (!empty($lead_status->lead_type)) {
            $lead_type = '(' . lang($lead_status->lead_type) . ')';
        }
        $lead_status_options[$lead_status->lead_status_id] = $lead_status->lead_status . ' ' . $lead_type;
    }
}
$lead_permission = config_item('default_lead_permission');
?>

<div class="space-y-6">
    <form role="form" id="form" action="<?php echo base_url(); ?>admin/settings/save_tickets" method="post" class="space-y-6">
        <div class="rounded-2xl border border-border bg-card shadow-sm">
            <div class="flex items-center justify-between border-b border-border px-6 py-4">
                <div>
                    <h3 class="text-lg font-semibold text-foreground"><?= lang('tickets_settings') ?></h3>
                    <p class="text-sm text-muted-foreground"><?= lang('manage') . ' ' . lang('tickets_settings') ?></p>
                </div>
                <button type="submit" class="inline-flex items-center justify-center gap-2 rounded-lg bg-primary px-6 py-2.5 text-sm font-medium text-primary-foreground shadow-sm hover:bg-primary/90 transition-colors">
                    <i data-lucide="save" class="h-4 w-4"></i>
                    <?= lang('save_changes') ?>
                </button>
            </div>
            <div class="p-6 space-y-6">
                <?php echo validation_errors(); ?>
                <input type="hidden" name="settings" value="<?= $load_setting ?>">

                <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
                    <div class="rounded-xl border border-border bg-card p-5 space-y-4">
                        <div class="flex items-center gap-2 border-b border-border pb-3">
                            <i data-lucide="life-buoy" class="h-5 w-5"></i>
                            <h4 class="text-base font-semibold text-foreground"><?= lang('tickets_settings') ?></h4>
                        </div>
                        <?= form_select_group('default_department', lang('default_department'), $department_options, config_item('default_department'), false, array('class' => 'select_box', 'style' => 'width:100%')) ?>
                        <?= form_select_group('default_status', lang('default_status'), $status_options, config_item('default_status')) ?>
                        <div class="flex items-end gap-2">
                            <div class="flex-1">
                                <?= form_select_group('default_priority', lang('default_priority'), $priority_options, config_item('default_priority'), false, array('style' => 'width:100%')) ?>
                            </div>
                            <?= btn_modal(
                                'admin/settings/manage_status/priority',
                                lang('new') . ' ' . lang('priority'),
                                lang('new') . ' ' . lang('priority'),
                                'md',
                                '',
                                array('class' => 'inline-flex items-center rounded-lg border border-border px-3 py-2.5 text-xs font-semibold text-foreground hover:bg-muted')
                            ) ?>
                        </div>
                        <?= form_checkbox('notify_ticket_reopened', lang('notify_ticket_reopened'), config_item('notify_ticket_reopened') == 'TRUE', false, array('value' => 'TRUE')) ?>
                    </div>

                    <div class="rounded-xl border border-border bg-card p-5 space-y-4">
                        <div class="flex items-center gap-2 border-b border-border pb-3">
                            <i data-lucide="users" class="h-5 w-5"></i>
                            <h4 class="text-base font-semibold text-foreground"><?= lang('leads_settings') ?></h4>
                        </div>
                        <div class="flex items-end gap-2">
                            <div class="flex-1">
                                <?= form_select_group('default_leads_source', lang('default') . ' ' . lang('source'), $lead_source_options, config_item('default_leads_source'), false, array('class' => 'select_box', 'style' => 'width:100%')) ?>
                            </div>
                            <a href="<?= base_url() ?>admin/settings/lead_source" class="inline-flex items-center rounded-lg border border-border px-3 py-2.5 text-xs font-semibold text-foreground hover:bg-muted">
                                <?= lang('new') . ' ' . lang('source') ?>
                            </a>
                        </div>
                        <div class="flex items-end gap-2">
                            <div class="flex-1">
                                <?= form_select_group('default_lead_status', lang('default') . ' ' . lang('status'), $lead_status_options, config_item('default_lead_status'), false, array('class' => 'select_box', 'style' => 'width:100%')) ?>
                            </div>
                            <a href="<?= base_url() ?>admin/settings/lead_status" class="inline-flex items-center rounded-lg border border-border px-3 py-2.5 text-xs font-semibold text-foreground hover:bg-muted">
                                <?= lang('new') . ' ' . lang('status') ?>
                            </a>
                        </div>

                        <div class="rounded-lg border border-border bg-muted/20 p-4 space-y-3">
                            <p class="text-sm font-medium text-foreground"><?= lang('permission_for_new_leads') ?></p>
                            <div class="flex flex-col gap-2">
                                <label class="flex items-center gap-2 text-sm text-foreground">
                                    <input type="radio" name="default_lead_permission" value="everyone" <?= (isset($lead_permission) && $lead_permission == 'all') ? 'checked' : '' ?>>
                                    <?= lang('everyone') ?>
                                    <i title="<?= lang('permission_for_all') ?>" data-lucide="circle-help" data-toggle="tooltip" data-placement="top"></i>
                                </label>
                                <label class="flex items-center gap-2 text-sm text-foreground">
                                    <input type="radio" name="default_lead_permission" value="custom_permission" <?= (isset($lead_permission) && $lead_permission != 'all') ? 'checked' : '' ?>>
                                    <?= lang('custom_permission') ?>
                                    <i title="<?= lang('permission_for_customization') ?>" data-lucide="circle-help" data-toggle="tooltip" data-placement="top"></i>
                                </label>
                            </div>
                        </div>

                        <div class="rounded-lg border border-border bg-card p-4" id="permission_user" style="display: <?= (!empty($lead_permission) && $lead_permission != 'all') ? 'block' : 'none' ?>;">
                            <p class="text-sm font-medium text-foreground mb-3"><?= lang('select') . ' ' . lang('users') ?> <span class="text-red-500">*</span></p>
                            <div class="space-y-4">
                                <?php
                                if (!empty($assign_user)) {
                                    foreach ($assign_user as $key => $v_user) {
                                        if ($v_user->role_id == 1) {
                                            $disable = true;
                                            $role = '<strong class="badge btn-danger">' . lang('admin') . '</strong>';
                                        } else {
                                            $disable = false;
                                            $role = '<strong class="badge btn-primary">' . lang('staff') . '</strong>';
                                        }
                                        ?>
                                        <div class="rounded-lg border border-border p-3">
                                            <label class="flex items-center gap-2 text-sm text-foreground">
                                                <input type="checkbox"
                                                    <?php
                                                    if (!empty($lead_permission) && $lead_permission != 'all') {
                                                        $get_permission = json_decode(config_item('default_lead_permission'));
                                                        foreach ($get_permission as $user_id => $v_permission) {
                                                            if ($user_id == $v_user->user_id) {
                                                                echo 'checked';
                                                            }
                                                        }
                                                    }
                                                    ?>
                                                       value="<?= $v_user->user_id ?>" name="assigned_to[]">
                                                <?= $v_user->username . ' ' . $role ?>
                                            </label>

                                            <div class="mt-2 flex flex-wrap gap-4 action_1 <?php
                                            if (!empty($lead_permission) && $lead_permission != 'all') {
                                                $get_permission = json_decode(config_item('default_lead_permission'));
                                                foreach ($get_permission as $user_id => $v_permission) {
                                                    if ($user_id == $v_user->user_id) {
                                                        echo 'show';
                                                    }
                                                }
                                            }
                                            ?>" id="action_1<?= $v_user->user_id ?>">
                                                <label class="flex items-center gap-2 text-sm text-foreground">
                                                    <input id="<?= $v_user->user_id ?>" checked type="checkbox" name="action_1<?= $v_user->user_id ?>[]" disabled value="view">
                                                    <?= lang('can') . ' ' . lang('view') ?>
                                                </label>
                                                <label class="flex items-center gap-2 text-sm text-foreground">
                                                    <input <?php if (!empty($disable)) { echo 'disabled checked'; } ?> id="<?= $v_user->user_id ?>"
                                                        <?php
                                                        if (!empty($lead_permission) && $lead_permission != 'all') {
                                                            $get_permission = json_decode(config_item('default_lead_permission'));
                                                            foreach ($get_permission as $user_id => $v_permission) {
                                                                if ($user_id == $v_user->user_id) {
                                                                    if (in_array('edit', $v_permission)) {
                                                                        echo 'checked';
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        ?>
                                                         type="checkbox" value="edit" name="action_<?= $v_user->user_id ?>[]">
                                                    <?= lang('can') . ' ' . lang('edit') ?>
                                                </label>
                                                <label class="flex items-center gap-2 text-sm text-foreground">
                                                    <input <?php if (!empty($disable)) { echo 'disabled checked'; } ?> id="<?= $v_user->user_id ?>"
                                                        <?php
                                                        if (!empty($lead_permission) && $lead_permission != 'all') {
                                                            $get_permission = json_decode(config_item('default_lead_permission'));
                                                            foreach ($get_permission as $user_id => $v_permission) {
                                                                if ($user_id == $v_user->user_id) {
                                                                    if (in_array('delete', $v_permission)) {
                                                                        echo 'checked';
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        ?>
                                                         name="action_<?= $v_user->user_id ?>[]" type="checkbox" value="delete">
                                                    <?= lang('can') . ' ' . lang('delete') ?>
                                                </label>
                                                <input id="<?= $v_user->user_id ?>" type="hidden" name="action_<?= $v_user->user_id ?>[]" value="view">
                                            </div>
                                        </div>
                                    <?php }
                                }
                                ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>

