<?php

namespace App\Http\Controllers\Artists;

use App\Artist;
use App\DataTables\ArtistDataTable;
use App\DataTables\ArtistDataTableEditor;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class ArtistsOverviewController extends Controller
{

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

    /**
     * @param ArtistDataTable $dataTable
     * @return mixed
     */
    public function index(ArtistDataTable $dataTable, Request $request)
    {
        //if ($request->filtered == 1 || !is_null($request->status)) {
        if ($request->filtered == 1) {

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

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

        $selectedFilterStatus = session('artistsStatus');
        $selectedFilterLanguage = null; //session('auctionObjectsUploadLanguage');
        $selectedFilterSearchColumn = session('artistsSearchColumn');
        $selectedFilterSearch = session('artistsSearch');

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

        /*if ($request->len > 0) {
            $pageLength = (int) $request->len;
        } else {
            $pageLength = Auth::user()->table_page_length;
        }*/

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

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

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

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

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

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

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

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

        // set search column in session
        if (!is_null($request->sc)) {
            session(['artistsSearchColumn' => $request->sc]);
        } else {
            session(['artistsSearchColumn' => null]);
        }

        // set search column in session
        if (!is_null($request->s)) {
            session(['artistsSearch' => $request->s]);
        } else {
            session(['artistsSearch' => null]);
        }

    }
}
