<?php

namespace App\Http\Controllers\Admin;

/** Ajax Request */

use App\Http\Requests\Admin\WorkDayRequest;
use App\Models\BlockDate;
use App\Models\WorkDay;
use App\Services\Interfaces\WorkDayRepositoryInterface;
use App\Services\Response\ResponseService;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Redirector;
use Illuminate\Http\RedirectResponse;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;

class WorkDayController extends Controller
{

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

    public function __construct(WorkDayRepositoryInterface $workDayRepository)
    {
        $this->workDayRepository = $workDayRepository;
    }

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

    /**
     * return data of the workDays.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) //:View
    {
        $request->merge(['paginate'=> true]);
        $workDays= $this->workDayRepository->get($request);

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

    public function fetchCities(Request $request)
    {
        $workDays= $this->workDayRepository->get($request);
        return $workDays;
    }

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

    /**
     * Store a newly created WorkDay in storage.
     * @param WorkDayRequest $request
     * @return RedirectResponse|Redirector
     */
    public function store(WorkDayRequest $request) :JsonResponse
    {
        $this->workDayRepository->save($request, null);

        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('workDays.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Show the form for editing the specified WorkDay.
     *
     * @param  WorkDay  $workDay
     * @return View
     */
    public function edit($id) :View
    {
        // dd($workDay->id);
        $workDay = WorkDay::findOrFail($id);
        $action= route('workdays.update', $workDay->id);
        $head= __('workDays.edit');
        $dates = BlockDate::cursor();
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    public function addDate(Request $request)
    {
        return view( $this->viewDirectory.'add_date', get_defined_vars() );
    }

    /**
     * Update the specified WorkDay in storage.
     * @param WorkDayRequest $request
     * @param WorkDay $workDay
     * @return RedirectResponse|Redirector
     */
    public function update(WorkDayRequest $request, $id) :JsonResponse
    {
        $workDay = WorkDay::findOrFail($id);
        $this->workDayRepository->save($request, $workDay);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('workdays.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Remove the specified WorkDay from storage.
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :JsonResponse
    {
        $return= $this->workDayRepository->destroy($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Remove several workDays by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $return= $this->workDayRepository->destroyAll($request);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Restore the specified WorkDay from storage.
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function restore(Request $request, int $id) :JsonResponse
    {
        $return= $this->workDayRepository->restore($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Restore several workDays by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $return= $this->workDayRepository->restoreAll($request);
        return response()->json($return[0], $return[1] ?? 200);
    }
}
