<?php

namespace Hura8\Components\Staff\Model;


class StaffPermissionModel
{

    protected  $objStaffModel;

    public function __construct()
    {
        $this->objStaffModel = new StaffModel();
    }


    public function getUserEntityPermission($admin_id) {

        /*return [
            // entity group => entity
            'product' => [
                'item' => [
                    'view'   => true,
                    'create' => false,
                    'delete' => true,
                    'update' => false,
                ],
            ],

            'article' => [
                'item' => [
                    'view'   => true,
                    'create' => false,
                    'delete' => false,
                    'update' => true,
                ],
            ]
        ];*/

        $admin_info = $this->objStaffModel->getInfo($admin_id);

        if($admin_info['entity_permission']) {
            $entity_permission = \json_decode($admin_info['entity_permission'], true);

            if(sizeof($entity_permission) > 0) {
                return $entity_permission;
            }
        }

        // else use department
        if($admin_info['department']) {
            $objStaffDepartmentPermissionModel = new StaffDepartmentPermissionModel();
            $department_entity_permission = $objStaffDepartmentPermissionModel->getDepartmentEntityPermission($admin_info['department']);

            return $department_entity_permission;
        }

        return [];
    }


    public function getUserMenuPermission($admin_id) {
        $admin_info = $this->objStaffModel->getInfo($admin_id);
        return ($admin_info['menu_permission']) ? \json_decode($admin_info['menu_permission'], true) : [];
    }

}
