<?php
session_start();

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

$user_permissions = $user_permission->read_all_user_permissions();

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

if(isset($_POST['add_employee_permission'])){
    $employees = $_POST['employees'];
    $set_permissions  = $_POST['system_permission'];
    foreach($employees as $r){
        foreach($set_permissions as $p){
            if($p=="All"){
                foreach($system_permissions as $s){
                    $data = array(
                        "system_permission_id"=>$s['system_permission_id'],
                        "employee_id"=>$r,
                        "state"=>"Extend"
                    );
                    $validate = $user_permission->validate_user_permission($s['system_permission_id'],$r);
                    if(!$validate){
                        $success = $user_permission->add_user_permission($data);
                    }else{
                        $user_permission_id = $validate[0]['user_permission_id'];
                        $data = array(
                            "state"=>"Extend"
                        );
                        $key = array(
                            "user_permission_id"=>$user_permission_id
                        );
                        $success = $user_permission->update_user_permission($key,$data);
                    }
                }
                break;
            }else{
                $data = array(
                    "system_permission_id"=>$p,
                    "employee_id"=>$r,
                    "state"=>"Extend"
                );
                $validate = $user_permission->validate_user_permission($p,$r);
                if(!$validate){
                    $success = $user_permission->add_user_permission($data);
                }else{
                    $user_permission_id = $validate[0]['user_permission_id'];
                        $data = array(
                            "state"=>"Extend"
                        );
                        $key = array(
                            "user_permission_id"=>$user_permission_id
                        );
                        $success = $user_permission->update_user_permission($key,$data);
                }
            }
            
        }
    }
    
    
    if($success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}

if(isset($_POST['update_user_permission'])){
    $user_permission_id = $_POST['user_permission_id'];
    $display_name = $_POST['display_name'];
    $user_name = $_POST['user_name'];
    $system_permission_details = $system_permission->get_system_permission_by_name($display_name);
    $system_permission_id = $system_permission_details[0]['system_permission_id'];
    $user_details = $role->get_user_by_name($user_name);
    $employee_id = $system_permission_details[0]['employee_id'];
    $data = array(
        "system_permission_id"=>$system_permission_id,
        "employee_id"=>$employee_id
    );
    $key = array(
        "user_permission_id"=>$user_permission_id
    );
    $success = $user_permission->update_user_permission($key,$data);
    if($success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}


if(isset($_POST['limit_employee_permission'])){
    $employees = $_POST['employees'];
    $set_permissions  = $_POST['system_permission'];
    foreach($employees as $r){
        foreach($set_permissions as $p){
            if($p=="All"){
                foreach($system_permissions as $s){
                    $data = array(
                        "system_permission_id"=>$s['system_permission_id'],
                        "employee_id"=>$r,
                        "state"=>"Limit"
                    );
                    $validate = $user_permission->validate_user_permission($s['system_permission_id'],$r);
                    
                    if(!$validate){
                        $success = $user_permission->add_user_permission($data);
                    }else{
                        $user_permission_id = $validate[0]['user_permission_id'];
                        $data = array(
                            "state"=>"Limit"
                        );
                        $key = array(
                            "user_permission_id"=>$user_permission_id
                        );
                        $success = $user_permission->update_user_permission($key,$data);
                    }
                }
                break;
            }else{
                $data = array(
                    "system_permission_id"=>$p,
                    "employee_id"=>$r,
                    "state"=>"Limit"
                );
                $validate = $user_permission->validate_user_permission($p,$r);
                if(!$validate){
                    $success = $user_permission->add_user_permission($data);
                }else{
                    $user_permission_id = $validate[0]['user_permission_id'];
                        $data = array(
                            "state"=>"Limit"
                        );
                        $key = array(
                            "user_permission_id"=>$user_permission_id
                        );
                        $success = $user_permission->update_user_permission($key,$data);
                }
            }
            
        }
    }
    
    
    if($success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}

if(isset($_POST['remove_employee_permission'])){
    $employees = $_POST['employees'];
    $set_permissions  = $_POST['system_permission'];
    foreach($employees as $r){
        foreach($set_permissions as $p){
            if($p=="All"){
                foreach($system_permissions as $s){
                    $data = array(
                        "system_permission_id"=>$s['system_permission_id'],
                        "employee_id"=>$r,
                        "state"=>"Limit"
                    );
                    $validate = $user_permission->validate_user_permission($s['system_permission_id'],$r);
                    if(!$validate){
                        $success = $user_permission->remove_to_add($s['system_permission_id'],$r);
                    }else{
                        $success = $user_permission->remove_to_add($s['system_permission_id'],$r);
                    }
                }
                break;
            }else{
                $data = array(
                    "system_permission_id"=>$p,
                    "employee_id"=>$r,
                    "state"=>"Limit"
                );
                $validate = $user_permission->validate_user_permission($p,$r);
                if(!$validate){
                    $success = $user_permission->remove_to_add($p,$r);
                }else{
                    $success = $user_permission->remove_to_add($p,$r);
                }
            }
            
        }
    }
    
    
    if(!$success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}