<?php

namespace App\Http\Controllers\Admin;

use App\Models\Demand;
use App\Models\Product;
use App\Models\Work;

use Illuminate\View\View;

use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;

class DemandController extends Controller
{

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

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

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

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

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

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

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

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

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

    public function edit(Demand $demand) :View
    {
        $head= __('demands.edit');

        return view( $this->viewDirectory.'form', compact('demand','head') );
    }

    public function destroy(Request $request, int $id) :JsonResponse
    {
        $demand = Demand::withTrashed()->find($id);
        if ($demand) {
            if ($demand->deleted_at) {
                $demand->forceDelete();
            } else {
                $demand->delete();
            }
            return response()->json(['message'=> __('dashboard.deletedDone')]);
        } else{
            return response()->json(['message'=> __('dashboard.noResult')], 400);
        }
    }

    /**
     * Remove several pages by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $ids= $request->ids;
        if ($request->force) {
            Demand::onlyTrashed()->whereIn('id', $ids)->forceDelete();
        } else {
            Demand::whereIn('id', $ids)->delete();
        }

        return response()->json(['message'=> __('dashboard.deletedDone')]);
    }
    public function restore(Request $request, int $id) :JsonResponse
    {
        $demand = Demand::withTrashed()->find($id);

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

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