<?php
/*
 * *************************************************
 * Copyright (C) 2021 Earworks.                    *
 * Codeigniter Project Template Modification       *
 * Please contact our email for more information : *
 * contact@earworks.co.id                          *
 * *************************************************
 */

defined('BASEPATH') OR exit('No direct script access allowed');
$permission_saved = $model->permissions;
$permission_group = Permission_model::grouped($permission_list);
$permission_ids = [];
$permission_group_saved = [];
if ($permission_saved) {
    $permission_ids = $permission_saved->pluck('id')->toArray();
    $permission_group_saved = array_unique($permission_saved->pluck('group')->toArray());
}
?>

<?= form_open(current_url() . '?redir=' . $redir, ['class' => 'form-horizontal']) ?>
<div class="row">
    <div class="col-sm-6">
        <div class="form-group">
            <?= lang('name', [], 'name', ['class' => 'col-sm-3 control-label']) ?>
            <div class="col-sm-9">
                <?= form_input('name', $data['name'], ['class' => 'form-control']) ?>
            </div>
        </div>
        <div class="form-group">
            <?= lang('description', [], 'description', ['class' => 'col-sm-3 control-label']) ?>
            <div class="col-sm-9">
                <?= form_textarea('description', $data['description'], ['class' => 'form-control']) ?>
            </div>
        </div>
        <div class="form-group">
            <?= lang('login_type', [], 'login_type', ['class' => 'col-sm-3 control-label']) ?>
            <div class="col-sm-9">
                <?= form_dropdown('login_type', Role_model::login_type_label(), $data['login_type'], ['class' => 'form-control']) ?>
            </div>
        </div>
        <div class="form-group">
            <?= lang('status', [], 'status', ['class' => 'col-sm-3 control-label']) ?>
            <div class="col-sm-9">
                <?= form_dropdown('status', Role_model::status_label(), $data['status'], ['class' => 'form-control']) ?>
            </div>
        </div>

        <div class="form-group">
            <div class="col-sm-9 col-sm-offset-3">
                <?= form_submit('submit', 'Submit', ['class' => 'btn blue']) ?>
            </div>
        </div>
    </div>
    <div class="col-sm-6">
        <?php foreach ($permission_group as $name => $permissions) : ?>
            <?php $checked_group = in_array($name, $permission_group_saved) ? ' checked="checked"' : null; ?>
            <div class="form-group">
                <div class="control-label col-sm-3" style="border-right:1px solid #ccc;">
                    <div class="mt-checkbox-inline" style="padding:0;">
                        <label class="mt-checkbox mt-checkbox-outline">
                            <input type="checkbox" name="permission_group[]" value="<?= $name ?>"  class="group-<?= str_replace(" ",'-', $name) ?>" onchange="script.toggle_checkbox(this)" data-target="data-<?= str_replace(" ",'-', $name) ?>" id="group-<?= str_replace(" ",'-', $name) ?>"<?= $checked_group ?>/>
                            <span></span>
                            <?= $name ?>
                        </label>
                    </div>
                </div>
                <div class="col-sm-9">
                    <div class="mt-checkbox-inline" style="padding:0;">
                        <?php foreach ($permissions as $permission) : ?>
                            <?php $checked_permission = in_array($permission->id, $permission_ids) ? ' checked="checked"' : null; ?>
                            <label class="mt-checkbox mt-checkbox-outline">
                                <input type="checkbox" name="permissions[]" value="<?= $permission->id ?>"  class="data-<?= str_replace(" ",'-', $name) ?>" onclick="script.toggle_checkbox_all(this)" data-target="group-<?= str_replace(" ",'-', $name) ?>"<?= $checked_permission ?> />
                                <span></span>
                                <?= $permission->name ?>
                            </label>
                        <?php endforeach; ?>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</div>
<?= form_close() ?>