<?php

namespace App\Http\Controllers\Admin;

use App\Career;
use App\Upload\FileHelper;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Requests\CareerRequest;
use Illuminate\Routing\Redirector;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

class CareerController extends Controller
{

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

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

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

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

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

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

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

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

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

        $careers= $query->paginate(100);

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


    /**
     * Show the form for editing the specified resource.
     *
     * @param Career $career
     * @return View
     */
    public function show(Career $career) :View
    {
        $head = __('careers.head').' - '.$career->name;
        return view( $this->viewDirectory.'form', compact('career', 'head') );
    }

    
    /**
     * Remove the specified resource from storage.
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :JsonResponse
    {
        $career = Career::withTrashed()->find($id);
        if ($career) {
            if ($career->deleted_at) {
                FileHelper::delete($career->file);
                $career->forceDelete();
            } else {
                $career->delete();
            }
            return response()->json(['message'=> __('dashboard.deletedDone')]);
        } else{
            return response()->json(['message'=> __('dashboard.noResult')], 400);
        }
    }

    /**
     * Remove several careers by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $ids= $request->ids;
        if ($request->force) {
            $files= Career::onlyTrashed()->whereIn('id', $ids)->get()->pluck('file')->toArray();
            // dd($files);
            foreach($files as $file){
                FileHelper::delete($file);
            }
            Career::onlyTrashed()->whereIn('id', $ids)->forceDelete();
        } else {
            Career::whereIn('id', $ids)->delete();
        }

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

    /**
     * Restore the specified category from storage
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function restore(Request $request, int $id) :JsonResponse
    {
        $career = Career::withTrashed()->find($id);

        if ($career){
            $career->restore();
            return response()->json(['message'=> __('dashboard.saveDone')]);
        }
        return response()->json(['message'=> __('dashboard.noResult')], 400);
    }

    /**
     * Restore several careers by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $careers = Career::whereIn('id', $request->ids)->onlyTrashed();
        if ($careers){
            $careers->restore();
            return response()->json(['message'=> __('dashboard.saveDone')]);
        }
    }
}
