<?php

namespace App\Http\Controllers\Medium;

use App\DataTables\MediumDataTable;
use App\DataTables\MediumDataTableEditor;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class MediumOverviewController extends Controller
{

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

    /*public function index(Request $request)
    {
        if ($request->filtered == 1) {

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

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

        $page = session('auctionObjectsUploadPage');
        $pageLength = session('auctionObjectsUploadLen');


    }

    public function handleRequest($request)
    {

    }*/

    /**
     * @param MediumDataTable $dataTable
     * @param Request $request
     * @return mixed
     */
    public function index(MediumDataTable $dataTable, Request $request)
    {

        if ($request->filtered == 1) {

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

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

        $page = session('mediumPage');
        $pageLength = session('mediumLen');
        $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('medium.medium-overview', ['page'=>$page, 'pageLength'=>$pageLength, 'pageParam'=>$pageParam, 'lenParam'=>$lenParam, 'sortc'=>$sortc, 'sortd'=>$sortd, 'sortcParam'=>$sortcParam, 'sortdParam'=>$sortdParam]);
    }

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

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

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

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

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

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


}
