<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<style>
.perm-table { width:100%; border-collapse:collapse; font-size:12.5px; }
.perm-table thead th {
    background:#f8f9fb; font-size:11px; font-weight:700; color:#888;
    text-transform:uppercase; letter-spacing:.5px;
    padding:10px 12px; border-bottom:2px solid #e2e6ea;
    text-align:center; white-space:nowrap; position:sticky; top:0; z-index:1;
}
.perm-table thead th:first-child { text-align:left; min-width:200px; }
.perm-table tbody tr { border-bottom:1px solid #f0f2f5; }
.perm-table tbody tr:hover { background:#f8f9ff; }
.perm-table tbody td { padding:7px 12px; vertical-align:middle; text-align:center; }
.perm-table tbody td:first-child { text-align:left; }
.perm-table .perm-group td:first-child { font-weight:700; color:#1e3a5f; background:#eef3fa; padding-left:12px; }
.perm-table .perm-group td { background:#eef3fa; }
.perm-table .perm-sub td:first-child { color:#555; padding-left:28px; }
.perm-table .perm-sub td:first-child::before { content:"→ "; color:#aaa; font-size:11px; }
.perm-checkbox { width:16px; height:16px; cursor:pointer; accent-color:#1e3a5f; }
.perm-checkbox:disabled { accent-color:#2e7d32; cursor:not-allowed; opacity:.7; }
</style>

<section class="content-header">
    <h1><i class="fa fa-shield"></i> <?php echo lang('permissions') ?> <small><?php echo lang('list') ?></small></h1>
    <ol class="breadcrumb">
        <li><a href="<?php echo site_url('admin') ?>"><i class="fa fa-dashboard"></i> Dashboard</a></li>
        <li class="active"><?php echo lang('permissions') ?></li>
    </ol>
</section>

<section class="content">

<div class="box box-primary">
    <div class="box-header with-border">
        <h3 class="box-title"><i class="fa fa-shield"></i> Permissões por Perfil</h3>
        <div class="box-tools pull-right">
            <?php if(check_user_role(50)==1): ?>
            <button type="submit" form="permission" class="btn btn-primary btn-sm">
                <i class="fa fa-save"></i> Salvar Permissões
            </button>
            <?php endif; ?>
        </div>
    </div>
    <div class="box-body no-padding">
        <div style="overflow-x:auto">
            <form id="permission" method="post" action="<?php echo site_url('admin/permissions/') ?>">
            <table class="perm-table">
                <thead>
                    <tr>
                        <th>Ação</th>
                        <?php foreach($departments as $dept): ?>
                        <th><?php echo ucwords($dept->name) ?></th>
                        <?php endforeach; ?>
                    </tr>
                </thead>
                <tbody>
                <?php $pactions = $actions; foreach($pactions as $paction): if($paction->parent_id == 0): ?>
                <tr class="perm-group">
                    <td><?php echo htmlspecialchars($paction->alias) ?></td>
                    <?php foreach($departments as $depart):
                        $checked = '';
                        foreach($permissions as $perm): if($perm->role_id==$depart->id && $perm->action_id==$paction->id) { $checked='checked'; break; } endforeach;
                    ?>
                    <td><input type="checkbox" class="perm-checkbox" name="access[<?php echo $depart->id ?>][<?php echo $paction->id ?>]" <?php echo $checked ?>></td>
                    <?php endforeach; ?>
                </tr>
                <?php foreach($actions as $action): if($action->is_hidden==0 && $action->parent_id==$paction->id): ?>
                <tr class="perm-sub">
                    <td><?php echo htmlspecialchars($action->alias) ?></td>
                    <?php if($action->always_allowed==1): ?>
                    <?php foreach($departments as $depart): ?>
                    <td><input type="checkbox" class="perm-checkbox" name="access[<?php echo $depart->id ?>][<?php echo $action->id ?>]" checked disabled></td>
                    <?php endforeach; ?>
                    <?php else: ?>
                    <?php foreach($departments as $depart):
                        $checked = '';
                        foreach($permissions as $perm): if($perm->role_id==$depart->id && $perm->action_id==$action->id) { $checked='checked'; break; } endforeach;
                    ?>
                    <td><input type="checkbox" class="perm-checkbox" name="access[<?php echo $depart->id ?>][<?php echo $action->id ?>]" <?php echo $checked ?>></td>
                    <?php endforeach; ?>
                    <?php endif; ?>
                </tr>
                <?php endif; endforeach; ?>
                <?php endif; endforeach; ?>
                </tbody>
            </table>
            </form>
        </div>
    </div>
    <?php if(check_user_role(50)==1): ?>
    <div class="box-footer">
        <button type="submit" form="permission" class="btn btn-primary">
            <i class="fa fa-save"></i> Salvar Permissões
        </button>
    </div>
    <?php endif; ?>
</div>

</section>
