<?php

namespace App\Http\Controllers\Admin\Core\Auth;

use App\Datatables\Admin\Core\Auth\RolesDataTable;
use App\Models\Core\App\ApplicationModule;
use App\Models\Core\Auth\Permission;
use App\Models\Core\Auth\Role;
use App\Repositories\Core\Auth\Roles;
use Illuminate\Http\Request;
use Yajra\DataTables\DataTables;

class RoleController extends Controller
{
    public function index(RolesDataTable $dataTable)
    {
        $data = \App\Repositories\Config::init();
        $data['modules'] = ApplicationModule::with('permissions')->get()->toArray();

        return $dataTable->render('admin.Core.Auth.Role.index', $data);
    }

    public function getTable(Request $request)
    {
        $model = Role::orderBy('name')->get();

        return Datatables::of($model)->make(true);
    }

    public function create()
    {
        $data = \App\Repositories\Config::init();

        return view('Admin.Core.Auth.Role.create', $data);
    }

    public function edit(Request $request, $id = false)
    {
        $id = $id ? $id : $request->input('id');
        $data = \App\Repositories\Config::init();
        $data['role'] = Roles::getWithPermissions($id);
        $data['modules'] = ApplicationModule::with('permissions')->get()->toArray();

        return view('Admin.Core.Auth.Role.edit', $data);
    }

    public function store(Request $request)
    {
        Roles::store($request->all());

        return response()->json(['error' => 0]);
    }

    public function destroy(Request $request, $id = false)
    {
        $id = $id ? $id : $request->input('id');
        Roles::delete($id);

        return response()->json(['error' => 0]);
    }

    public function manage(Request $request, $id = false)
    {
        $data = [];
        $id = (int) $id ? $id : $request->input('id');
        $data['role'] = Role::with('permissions')->find($id)->toArray();
        $permissions = Permission::orderBy('app')->orderBy('module')->orderBy('action')->get();
        foreach ($permissions as $permission) {
            $data[$permission->app][$permission->module][] = [
                'action' => $permission->action,
                'description' => $permission->description,
                'id' => $permission->id,
            ];
        }
        // Debug::fdump($data);
        // Debug::fdump($permissions);
        // exit;
        $data['permissions'] = $data;

        return view('Admin.Core.Auth.Role.manage', $data);
    }

    public function manage_store(Request $request)
    {
        $data = $request->all();
        $role = Role::find($data['role_id']);
        foreach ($data['permissions'] as $key) {
            $permission = Permission::find($key);
            $role->attachPermission($permission);
        }

        return response()->json(['error' => 0]);
    }

    public function toggleActive(Request $request)
    {
        $data = Roles::toggle_active($request->input('id'), ($request->input('active') == 'true') ? 1 : 0);

        return response()->json(['error' => 0]);
    }
}
