<?php

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

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;

use App\Repositories\Core\App\Applications;
use App\Repositories\Core\App\ApplicationModules;
use App\Datatables\Admin\Core\App\ApplicationModulesDataTable;

class ApplicationModuleController extends Controller
{
    public function index(ApplicationModulesDataTable $dataTable)
    {
        $data = \App\Repositories\Config::init();
        return $dataTable->render('admin.Core.App.ApplicationModule.index', $data);
    }

    public function create()
    {
        $data = \App\Repositories\Config::init();
        $data['applications'] = Applications::getOptions();
        return view('Admin.Core.App.ApplicationModule.create', $data);
    }

    public function edit(Request $request, $id = false)
    {
        $id = $id ? $id : $request->input('id');
        $data = \App\Repositories\Config::init();
        $data = ApplicationModules::get($id)->toArray();
        $data['applications'] = Applications::getOptions();
        return view('Admin.Core.App.ApplicationModule.edit', $data);
    }

    public function store(Request $request)
    {
        ApplicationModules::store($request->all());
        return redirect()->route('admin.Core.App.ApplicationModule.index')
            ->with('growl', [__('admin.successadd'), 'success']);
    }

    public function destroy(Request $request, $id = false)
    {
        $id = $id ? $id : $request->input('id');
        ApplicationModules::destroy($id);
        return response()->json(['error' => 0]);
    }

    public function toggleActive(Request $request)
    {
        $data = ApplicationModules::toggle_active($request->input('id'), ($request->input('active') == 'true') ? 1 : 0);
        return response()->json(['error' => 0]);
    }
}
