<?php

namespace App\Http\Controllers\Admin;

/** Ajax Request */

use Exception;
use App\Day;
use App\Log;
use App\Helpers\ImageHelper;
use Illuminate\View\View;
use App\DayDescription;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Redirector;
use Illuminate\Http\RedirectResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\DayRequest; // for Validation
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

class DayController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/dashboard/days';
    /** View folder */
    private $viewDirectory= 'admin.days.';

    /**
     * Display a listing of the days.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('days.head');
        $request= $request->toArray();
        return view($this->viewDirectory.'index',compact('title'));
    }

    /**
     * return data of the days.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $query = Day::latest();

        if ($request->date_from) {
            $query->whereDate('days.created_at', '>=', $request->date_from);
        }

        if ($request->date_to) {
            $query->whereDate('days.created_at', '<=', $request->date_to);
        }

        if ($request->name) {
            $query->where('cd.name', 'LIKE', '%'.$request->name.'%');
        }

        if ( !is_null($request->status) && $request->status == 0) {
            $query->onlyTrashed();
        }
        $days= $query->paginate(100);

        return view($this->viewDirectory.'grid',compact('days'));
    }

    /**
     * Show the form for creating a new day.
     *
     * @return View
     */
    public function create() :View
    {
        $action= route('days.store');
        $head= __('days.new');
        return view( $this->viewDirectory.'form', compact('action', 'head') );
    }

    /**
     * Store a newly created Ad in storage.
     * @param DayRequest $request
     * @return RedirectResponse|Redirector
     */
    public function store(DayRequest $request) :RedirectResponse
    {
       
        Log::create(['user_id'=>\Auth::user()->id,'type'=>'عدد اليوم','action'=>'اضافة']);

        $day = Day::create($request->all());
        if($request->hasFile('file')){
        $day->file = upload($request->file('file'));
        $day->save();
        }

        return redirect($this->redirectSuccessPath)->with('message', __('dashboard.saveDone'));
    }

    /**
     * Show the form for editing the specified Day.
     *
     * @param  Day  $day
     * @return View
     */
    
    public function edit(Day $day) :View
    {
        $action= route('days.update', $day->id);
        $head = __('days.edit');
       

        return view( $this->viewDirectory.'form', compact('day', 'action','head') );
    }
    
    public function show(Day $day)
    {
        

         
          $file= public_path(). $day->file;

    $headers = array(
              'Content-Type: application/pdf',
            );
return \Response::download($file, 'Diet.pdf', $headers);
    }

    /**
     * Update the specified day in storage.
     * @param DayRequest $request
     * @param Day $day
     * @return RedirectResponse|Redirector
     */
    public function update(DayRequest $request, Day $day) :RedirectResponse
    {
        // Update the "updated_at" column only
        $day->update($request->all());
       if($request->hasFile('file')){
        $day->file = upload($request->file('file'));
        $day->save();
        }
        Log::create(['user_id'=>\Auth::user()->id,'type'=>'عدد اليوم','action'=>'تعديل']);


       
        return redirect($this->redirectSuccessPath)->with('message', __('dashboard.saveDone'));
    }

    /**
     * Handle Save form data
     *
     * @param DayRequest $request
     * @param int $day_id
     * @return void
     */

     private function saveData(DayRequest $request, int $day_id ) :void
     {

        $requestData= $request->all();

        foreach (languages() as $lang) {
            $data=[
                'day_id'=> $day_id,
                'language_id'=> $lang->id,
                'name'=> $requestData['name_'.$lang->local],
            ];

            DayDescription::create($data);
        }
        
        

     }

    /**
     * Remove the specified Day from storage.
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :JsonResponse
    {
        $day= Day::withTrashed()->find($id);
        Log::create(['user_id'=>\Auth::user()->id,'type'=>'عدد اليوم','action'=>'حذف']);
        if ($day) {
            if ($day->deleted_at) {
                $day->forceDelete();
            } else {
                $day->delete();
            }
            return response()->json(['message'=> __('dashboard.deletedDone')]);
        } else{
            return response()->json(['message'=> __('dashboard.noResult')], 400);
        }
    }

    /**
     * Remove several days by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $ids= $request->ids;
        Log::create(['user_id'=>\Auth::user()->id,'type'=>'عدد اليوم','action'=>'حذف']);

        if ($request->force) {
            Day::onlyTrashed()->whereIn('id', $ids)->forceDelete();
        } else {
            Day::whereIn('id', $ids)->delete();
        }

        return response()->json(['message'=> __('dashboard.deletedDone')]);
    }

    /**
     * Restore the specified Day from storage.
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function restore(Request $request, int $id) :JsonResponse
    {
        $day= Day::withTrashed()->find($id);
        Log::create(['user_id'=>\Auth::user()->id,'type'=>'عدد اليوم','action'=>'استرجاع']);
        if ($day) {
            $day->restore();
            return response()->json(['message'=> __('dashboard.saveDone')]);
        }
        return response()->json(['message'=> __('dashboard.noResult')], 400);

    }

    /**
     * Restore several days by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $ids= $request->ids;
        Log::create(['user_id'=>\Auth::user()->id,'type'=>'عدد اليوم','action'=>'استرجاع']);
        Day::whereIn('id', $ids)->onlyTrashed()->restore();
        return response()->json(['message'=> __('dashboard.saveDone')]);
    }
}
