<?php
session_start();

require_once '../Config/Database.php';
require_once '../Model/RolePermission.php';
require_once '../Model/SystemPermission.php';
require_once '../Model/Role.php';
$system_permission = new SystemPermission();
$role_permission = new RolePermission();
$role = new Role();
$system_permissions = $system_permission->read_all_system_permissions();

$role_permissions = $role_permission->read_all_role_permissions();

if(isset($_GET['all_role_permissions'])){
    echo json_encode(
        array("data"=>$role_permissions)
    ); 
}

if(isset($_POST['add_role_permission'])){
    $system_roles = $_POST['role'];
    $set_permissions  = $_POST['system_permission'];
    foreach($system_roles as $r){
        foreach($set_permissions as $p){
            if($p=="All"){
                foreach($system_permissions as $s){
                    $data = array(
                        "system_permission_id"=>$s['system_permission_id'],
                        "role_id"=>$r
                    );
                    $validate = $role_permission->validate_role_permission($s['system_permission_id'],$r);
                    if(!$validate){
                        $success = $role_permission->add_role_permission($data);
                    }else{
                        $success = $role_permission->remove_to_add($p,$r);
                    }
                }
                break;
            }else{
                $data = array(
                    "system_permission_id"=>$p,
                    "role_id"=>$r
                );
                $validate = $role_permission->validate_role_permission($p,$r);
                if(!$validate){
                    $success = $role_permission->add_role_permission($data);
                }else{
                    $success = $role_permission->remove_to_add($p,$r);
                }
            }
            
        }
    }
    
    
    if($success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}

if(isset($_POST['update_role_permission'])){
    $role_permission_id = $_POST['role_permission_id'];
    $display_name = $_POST['display_name'];
    $role_name = $_POST['role_name'];
    $system_permission_details = $system_permission->get_system_permission_by_name($display_name);
    $system_permission_id = $system_permission_details[0]['system_permission_id'];
    $role_details = $role->get_role_by_name($role_name);
    $role_id = $system_permission_details[0]['role_id'];
    $data = array(
        "system_permission_id"=>$system_permission_id,
        "role_id"=>$role_id
    );
    $key = array(
        "role_permission_id"=>$role_permission_id
    );
    $success = $role_permission->update_role_permission($key,$data);
    if($success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}


if(isset($_POST['remove_role_permission'])){
    
    $system_roles = $_POST['role'];
    $set_permissions  = $_POST['system_permission'];
    foreach($system_roles as $r){
        $role_id = $r;
        foreach($set_permissions as $p){
            if($p=="All"){
                foreach($system_permissions as $s){
                    $data = array(
                        "system_permission_id"=>$s['system_permission_id'],
                        "role_id"=>$r
                    );
                    $validate = $role_permission->validate_role_permission($s['system_permission_id'],$r);
                    if($validate){
                        $success = $role_permission->remove_to_add($s['system_permission_id'],$r);
                    }else{
                        $success = $role_permission->remove_to_add($s['system_permission_id'],$r);
                    }
                }
                break;
            }else{
                $data = array(
                    "system_permission_id"=>$p,
                    "role_id"=>$r
                );
                $validate = $role_permission->validate_role_permission($p,$r);
                if($validate){
                    $success = $role_permission->remove_to_add($p,$r);
                }else{
                    $success = $role_permission->remove_to_add($p,$r);
                }
            }
            
        }
    }
    
    
    if(!$success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}