<?php
if (empty($_GET['id'])) {
    header("Location: " . $wo['config']['site_url']);
    exit();
}
$user_data = userData($_GET['id']);
if (empty($user_data)) {
    header("Location: " . $wo['config']['site_url']);
    exit();
}
$user_data = ToArray($user_data);
$per_array = array();
$wo['all_pages'] = scandir('admin-panel/pages');
unset($wo['all_pages'] [0]);
unset($wo['all_pages'] [1]);
unset($wo['all_pages'] [2]);
if (!empty($wo['all_pages'])) {
    foreach ($wo['all_pages']  as $key => $value) {
        if (in_array($value,$wo['mod_pages'])) {
            $per_array[$value] = 1;
        }
        else{
            $per_array[$value] = 0;
        } 
    }
}
if (empty($user_data['permission'])) {
    $permission = json_encode($per_array);
    $db->where('id',$user_data['id'])->update('users',array('permission' => $permission));
    $user_data = Wo_UserData($user_data['id']);
    $user_data = ToArray($user_data);
    $user_data['permission'] = json_decode($user_data['permission'],true);
}
else{
    $user_data['permission'] = json_decode($user_data['permission'],true);
    if (!empty($user_data['permission'])) {
        foreach ($per_array as $key => $value) {
            if (!in_array($key, array_keys($user_data['permission']))) {
                $user_data['permission'][$key] = 0;
            }
        }
        $permission = json_encode($user_data['permission']);
        $db->where('id',$user_data['id'])->update('users',array('permission' => $permission));
        $user_data = Wo_UserData($user_data['id']);
        $user_data = ToArray($user_data);
        $user_data['permission'] = json_decode($user_data['permission'],true);
    }
    else{
        $permission = json_encode($per_array);
        $db->where('id',$user_data['id'])->update('users',array('permission' => $permission));
        $user_data = Wo_UserData($user_data['id']);
        $user_data = ToArray($user_data);
        $user_data['permission'] = json_decode($user_data['permission'],true);
    }
}

?>
<div class="container-fluid">
    <div>
        <h3>Manage user permissions</h3>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item">
                    <a href="#">Home</a>
                </li>
                <li class="breadcrumb-item">
                    <a href="#">Users</a>
                </li>
                <li class="breadcrumb-item active" aria-current="page">Manage user permissions</li>
            </ol>
        </nav>
    </div>
    <div class="row">
        
        <div class="clearfix"></div>
        <div class="col-md-12">
            <div class="card">
                <div class="card-body">
                    <h6 class="card-title">User Permission</h6>
                    <div class="float-left">
                        <label for="dashboard" class="main-label"></label>
                    </div>
                    <div class="text-center">
                        <button type="button" name="permission_type" value="normal" id="btn_permission_normal" class="btn select_user_perm <?php echo($user_data['admin'] == 0 ? 'btn-info' : 'active_permissions') ?>" <?php if($user_data['admin'] != 0){ ?>onclick="Wo_Permission(<?php echo $user_data['id']?>,'hide','normal');" <?php } ?>>
                            <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path fill="currentColor" d="M12,4A4,4 0 0,1 16,8A4,4 0 0,1 12,12A4,4 0 0,1 8,8A4,4 0 0,1 12,4M12,14C16.42,14 20,15.79 20,18V20H4V18C4,15.79 7.58,14 12,14Z" /></svg> Normal
                        </button>
                        <button type="button" name="permission_type" value="moderator" id="btn_permission_moderator" class="btn select_user_perm <?php echo($user_data['admin'] == 2 ? 'btn-info' : 'active_permissions') ?>" <?php if($user_data['admin'] != 2){ ?>onclick="Wo_Permission(<?php echo $user_data['id']?>,'hide','moderator');" <?php } ?>>
                            <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path fill="currentColor" d="M12 3C14.21 3 16 4.79 16 7S14.21 11 12 11 8 9.21 8 7 9.79 3 12 3M16 13.54C16 14.6 15.72 17.07 13.81 19.83L13 15L13.94 13.12C13.32 13.05 12.67 13 12 13S10.68 13.05 10.06 13.12L11 15L10.19 19.83C8.28 17.07 8 14.6 8 13.54C5.61 14.24 4 15.5 4 17V21H20V17C20 15.5 18.4 14.24 16 13.54Z" /></svg> Moderator
                        </button>
                        <button type="button" name="permission_type" value="admin" id="btn_permission_admin" class="btn select_user_perm <?php echo($user_data['admin'] == 1 ? 'btn-info' : 'active_permissions') ?>" <?php if($user_data['admin'] != 1){ ?> onclick="Wo_Permission(<?php echo $user_data['id']?>,'hide','admin');" <?php } ?>>
                            <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path fill="currentColor" d="M12,1L3,5V11C3,16.55 6.84,21.74 12,23C17.16,21.74 21,16.55 21,11V5L12,1M12,5A3,3 0 0,1 15,8A3,3 0 0,1 12,11A3,3 0 0,1 9,8A3,3 0 0,1 12,5M17.13,17C15.92,18.85 14.11,20.24 12,20.92C9.89,20.24 8.08,18.85 6.87,17C6.53,16.5 6.24,16 6,15.47C6,13.82 8.71,12.47 12,12.47C15.29,12.47 18,13.79 18,15.47C17.76,16 17.47,16.5 17.13,17Z" /></svg> Admin
                        </button>
                    </div>
                    <div class="clearfix"></div>
                </div>
            </div>
            <div>
        </div>
        </div>
        <br>
        <div class="clearfix"></div>
        <?php if ($user_data['admin'] == 2) { 
            $SideArray = array(
                0 => array('main_title' => 'Dashboard',
                           'sub' => array('dashboard' => 'Dashboard')),
                1 => array('main_title' => 'Settings',
                           'sub' => array('general-settings' => 'General Configuration',
                                          'site-settings' => 'Website Information',
                                          'site-features' => 'Manage Site Features',
                                          'email-settings' => 'E-mail & SMS Settings',
                                          'video-settings' => 'Chat & Video/Audio',
                                          'social-login' => 'Social Login Settings',
                                          'amazon-settings' => 'File Upload Configuration',
                                          'live' => 'Setup Live Streaming')),
                2 => array('main_title' => 'Languages',
                           'sub' => array('add-language' => 'Add New Language & Keys',
                                          'edit-lang' => 'Edit Language',
                                          'manage-languages' => 'Manage Languages')),
                3 => array('main_title' => 'Gifts',
                           'sub' => array('manage-gifts' => 'Manage Gifts',
                                          'add-new-gift' => 'Add New Gift')),
                4 => array('main_title' => 'Reports',
                           'sub' => array('manage-reports' => 'Manage Reports')),
            ); ?>
            <div class="col-lg-3 col-md-3">
                <?php foreach ($SideArray as $key => $value) { ?>
                    <div class="card">
                        <div class="card-body">
                            <h6 class="card-title"><?php echo($value['main_title']); ?></h6>
                            <?php foreach ($value['sub'] as $key2 => $value2) { ?>
                                <div class="float-left">
                                    <label for="<?php echo($key2); ?>" class="main-label"><?php echo($value2); ?></label>
                                </div>
                                <div class="form-group float-right switcher">
                                    <input type="hidden" name="<?php echo($key2); ?>" value="0" />
                                    <input type="checkbox" name="<?php echo($key2); ?>" id="chck-<?php echo($key2); ?>" value="1" <?php echo ($user_data['permission'][$key2] == 1) ? 'checked': '';?>>
                                    <label for="chck-<?php echo($key2); ?>" class="check-trail"><span class="check-handler"></span></label>
                                </div>
                                <div class="clearfix"></div>
                                <?php 
                                if (array_keys($value['sub'])[count($value['sub']) - 1] != $key2) { ?>
                                    <hr>
                                <?php } ?>
                            <?php } ?>
                        </div>
                    </div>
               <?php  } ?>
            </div>
            <?php 
            $SideArray = array(
                0 => array('main_title' => 'Users',
                           'sub' => array('manage-users' => 'Manage Users',
                                          'manage-genders' => 'Manage Genders',
                                          'manage-countries' => 'Manage Countries',
                                          'manage-profile-fields' => 'Manage Custom Profile Fields',
                                          'add-new-profile-field' => 'Add Custom Profile Fields',
                                          'edit-profile-field' => 'Edit Custom Profile Fields',
                                          'manage-success-stories' => 'Manage Success Stories',
                                          'edit-success-stories' => 'Edit Success Stories',
                                          'manage-user-verification' => 'Manage Verification',
                                          'manage-verification-requests' => 'Manage User Verification Requests',
                                          'affiliates-settings' => 'Affiliates Settings',
                                          'payment-requests' => 'Payment Requests',
                                          'referrals-list' => 'Referrals List')),
                1 => array('main_title' => 'Design',
                           'sub' => array('manage-themes' => 'Themes',
                                          'change-site-desgin' => 'Change Site Design')),
                2 => array('main_title' => 'API Settings',
                           'sub' => array('push-notifications-system' => 'Push Notifications Settings')),
            ); ?>
            <div class="col-lg-3 col-md-3">
                <?php foreach ($SideArray as $key => $value) { ?>
                    <div class="card">
                        <div class="card-body">
                            <h6 class="card-title"><?php echo($value['main_title']); ?></h6>
                            <?php foreach ($value['sub'] as $key2 => $value2) { ?>
                                <div class="float-left">
                                    <label for="<?php echo($key2); ?>" class="main-label"><?php echo($value2); ?></label>
                                </div>
                                <div class="form-group float-right switcher">
                                    <input type="hidden" name="<?php echo($key2); ?>" value="0" />
                                    <input type="checkbox" name="<?php echo($key2); ?>" id="chck-<?php echo($key2); ?>" value="1" <?php echo ($user_data['permission'][$key2] == 1) ? 'checked': '';?>>
                                    <label for="chck-<?php echo($key2); ?>" class="check-trail"><span class="check-handler"></span></label>
                                </div>
                                <div class="clearfix"></div>
                                <?php 
                                if (array_keys($value['sub'])[count($value['sub']) - 1] != $key2) { ?>
                                    <hr>
                                <?php } ?>
                            <?php } ?>
                        </div>
                    </div>
               <?php  } ?>
            </div>
            <?php 
            $SideArray = array(
                0 => array('main_title' => 'Payments & Ads',
                           'sub' => array('payment-settings' => 'Payment Configuration',
                                          'payments' => 'Payments',
                                          'manage-payments' => 'Manage Payments',
                                          'manage-currencies' => 'Manage Currencies',
                                          'bank-receipts' => 'Manage bank receipts',
                                          'manage-website-ads' => 'Manage Website Ads')),
                1 => array('main_title' => 'Photos',
                           'sub' => array('manage-photos' => 'Manage Photos & Videos')),
                2 => array('main_title' => 'Stickers',
                           'sub' => array('manage-stickers' => 'Manage stickers',
                                          'add-new-sticker' => 'Add New sticker')),
                3 => array('main_title' => 'Blogs',
                           'sub' => array('manage-articles' => 'Manage Blog',
                                          'add-new-article' => 'Add Blog',
                                          'edit-article' => 'Edit Blog',
                                          'manage-blog-categories' => 'Blog categories')),
                4 => array('main_title' => 'Changelogs',
                           'sub' => array('changelog' => 'Changelogs'))
            ); ?>
            <div class="col-lg-3 col-md-3">
                <?php foreach ($SideArray as $key => $value) { ?>
                    <div class="card">
                        <div class="card-body">
                            <h6 class="card-title"><?php echo($value['main_title']); ?></h6>
                            <?php foreach ($value['sub'] as $key2 => $value2) { ?>
                                <div class="float-left">
                                    <label for="<?php echo($key2); ?>" class="main-label"><?php echo($value2); ?></label>
                                </div>
                                <div class="form-group float-right switcher">
                                    <input type="hidden" name="<?php echo($key2); ?>" value="0" />
                                    <input type="checkbox" name="<?php echo($key2); ?>" id="chck-<?php echo($key2); ?>" value="1" <?php echo ($user_data['permission'][$key2] == 1) ? 'checked': '';?>>
                                    <label for="chck-<?php echo($key2); ?>" class="check-trail"><span class="check-handler"></span></label>
                                </div>
                                <div class="clearfix"></div>
                                <?php 
                                if (array_keys($value['sub'])[count($value['sub']) - 1] != $key2) { ?>
                                    <hr>
                                <?php } ?>
                            <?php } ?>
                        </div>
                    </div>
               <?php  } ?>
            </div>
            <?php 
            $SideArray = array(
                0 => array('main_title' => 'Tools',
                           'sub' => array('fake-users' => 'Fake User Generator',
                                          'manage-announcements' => 'Announcements',
                                          'ban-users' => 'BlackList',
                                          'mock-email' => 'Send E-mail',
                                          'manage-invitation' => 'Users Invitation',
                                          'manage-invitation-keys' => 'Invitation Codes',
                                          'manage-apps' => 'Applications',
                                          'auto-like' => 'Auto Like')),
                1 => array('main_title' => 'Pages',
                           'sub' => array('manage-custom-pages' => 'Manage Custom Pages',
                                          'add-new-custom-page' => 'Add Custom Pages',
                                          'edit-custom-page' => 'Edit Custom Pages',
                                          'edit-terms-pages' => 'Edit Terms Pages',
                                          'pages-seo' => 'Edit Pages SEO Information',
                                          'manage-faqs' => 'Manage FAQs')),
            ); ?>
            <div class="col-lg-3 col-md-3">
                <?php foreach ($SideArray as $key => $value) { ?>
                    <div class="card">
                        <div class="card-body">
                            <h6 class="card-title"><?php echo($value['main_title']); ?></h6>
                            <?php foreach ($value['sub'] as $key2 => $value2) { ?>
                                <div class="float-left">
                                    <label for="<?php echo($key2); ?>" class="main-label"><?php echo($value2); ?></label>
                                </div>
                                <div class="form-group float-right switcher">
                                    <input type="hidden" name="<?php echo($key2); ?>" value="0" />
                                    <input type="checkbox" name="<?php echo($key2); ?>" id="chck-<?php echo($key2); ?>" value="1" <?php echo ($user_data['permission'][$key2] == 1) ? 'checked': '';?>>
                                    <label for="chck-<?php echo($key2); ?>" class="check-trail"><span class="check-handler"></span></label>
                                </div>
                                <div class="clearfix"></div>
                                <?php 
                                if (array_keys($value['sub'])[count($value['sub']) - 1] != $key2) { ?>
                                    <hr>
                                <?php } ?>
                            <?php } ?>
                        </div>
                    </div>
               <?php  } ?>
            </div>


        <?php }elseif($user_data['admin'] == 1){ ?>
            <div class="col-lg-12 col-md-12">
                <div class="alert alert-warning">Admin can manage everything</div>
            </div>
        <?php } ?>
    </div>
</div>
<div class="modal fade" id="PermissionModal" tabindex="-1" role="dialog" aria-labelledby="exampleModal1Label" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModal1Label">Add as a  Moderator?</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="ModeratorModalAlert"></div>
                <p class="permission_text">Are you sure you want to add as a  Moderator?</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-secondary">Add</button>
            </div>
        </div>
    </div>
</div>
<script>
    $(function() {
        $('.switcher input[type=checkbox]').click(function () {
            setToTrue = 0;
            if ($(this).is(":checked") === true) {
                setToTrue = 1;
            }
            var permission = $(this).attr('name');
            var hash_id = $('input[name=hash_id]').val();
            var objData = {};
            objData['permission'] = permission;
            objData['permission_val'] = setToTrue;
            var user_id = '<?php echo (int)$user_data['id'];?>';
            objData['user_id'] = user_id;
            objData['hash_id'] = hash_id;
            $.get( Wo_Ajax_Requests_File() + '?f=admin_setting&s=update_moderator_permission', objData);
        });
    });

    function Wo_Permission(user_id,type = 'show',type2) {
        if (type == 'hide') {
            $('#PermissionModal').find('.btn-secondary').attr('onclick', "Wo_Permission('"+user_id+"','show','"+type2+"')");
            text = 'Add as a normal user?';
            text2 = 'Are you sure you want to add as a normal user?';
            if (type2 == 'moderator') {
                text = 'Add as a moderator?';
                text2 = 'Are you sure you want to add as a moderator?';
            }
            if (type2 == 'admin') {
                text = 'Add as an admin?';
                text2 = 'Are you sure you want to add as an admin?';
            }
            $('#PermissionModal').find('.modal-title').html(text);
            $('#PermissionModal').find('.permission_text').html(text2);
            $('#PermissionModal').modal('show');
            return false;
        }
        hash_id = $('#hash_id').val();
        $.get(Wo_Ajax_Requests_File(),{f:'admin_setting', s:'permission', user_id: user_id, hash_id: hash_id,type: type2},function(data) {
            location.reload();
        });
    }
</script>