<?php

namespace App\Http\Controllers\SourceTypes;

use App\DataTables\SourceTypeDataTable;
use App\DataTables\SourceTypeDataTableEditor;
use App\SourceType;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class SourceTypesOverviewController extends Controller
{

    /**
     * SourceTypesOverviewController constructor.
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * @param SourceTypeDataTable $dataTable
     * @param Request $request
     * @return mixed
     */
    public function index(SourceTypeDataTable $dataTable, Request $request)
    {
        if ($request->filtered == 1) {

            $this->handleRequest($request);
        } else {

            if (is_null(session('sourceTypesLen'))) {
                // set table length
                session(['sourceTypesLen' => Auth::user()->table_page_length]);
            }
        }

        $page = session('sourceTypesPage');
        $pageLength = session('sourceTypesLen');
        $pageParam = $request->page;
        $lenParam = $request->len;
        $sortc = session('artistsSortColumn');
        $sortd = session('artistsSortDirection');
        $sortcParam = $request->sortc;
        $sortdParam = $request->sortd;

        return $dataTable->with(['page'=>$page, 'pageLength'=>$pageLength, 'pageParam'=>$pageParam, 'lenParam'=>$lenParam, 'sortc'=>$sortc, 'sortd'=>$sortd, 'sortcParam'=>$sortcParam, 'sortdParam'=>$sortdParam])
            ->render('source-types.source-types-overview', ['page'=>$page, 'pageLength'=>$pageLength, 'pageParam'=>$pageParam, 'lenParam'=>$lenParam, 'sortc'=>$sortc, 'sortd'=>$sortd, 'sortcParam'=>$sortcParam, 'sortdParam'=>$sortdParam]);
    }

    /**
     * @param SourceTypeDataTableEditor $editor
     * @return \Illuminate\Http\JsonResponse|mixed
     * @throws \Yajra\DataTables\DataTablesEditorException
     */
    public function store(SourceTypeDataTableEditor $editor)
    {
        return $editor->process(request());
    }

    /**
     * @param $request
     */
    public function handleRequest($request) {

        // set page in session
        if (!is_null($request->page)) {
            session(['sourceTypesPage' => $request->page]);
        } else {
            session(['sourceTypesPage' => null]);
        }

        // set page length in session
        if ($request->len > 0) {
            session(['sourceTypesLen' => $request->len]);
        } else {
            session(['sourceTypesLen' => Auth::user()->table_page_length]);
        }

        // set sort column in session
        if (!is_null($request->sortc)) {
            session(['sourceTypesSortColumn' => $request->sortc]);
        } else {
            session(['sourceTypesSortColumn' => null]);
        }

        // set sort direction in session
        if (!is_null($request->sortd)) {
            session(['sourceTypesSortDirection' => $request->sortd]);
        } else {
            session(['sourceTypesSortDirection' => null]);
        }
    }
}
