<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\CodeRequest;

use App\Code;

use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Routing\Redirector;
use GuzzleHttp\Client;
use Illuminate\View\View;

class CodeController extends Controller
{
    private $redirectSuccessPath= '/admin/codes';
    /** View folder */
    private $viewDirectory= 'admin.codes.';


     /**
     * Display a listing of the resource.
     *
     * @return View
     */

    public function index() :View
    {
        $title = __('codes.head');
        return view($this->viewDirectory.'index',['title'=>$title]);
    }

     /**
     * return data of the users.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $query = Code::orderBy('id', 'DESC');

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

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

        if ($request->code) {
            $query->where('code', 'LIKE', '%'.$request->code.'%');
        }
       
        if ($request->percentage) {
            $query->where('percentage', 'LIKE', '%'.$request->percentage.'%');
        }
       
        
         if ( !is_null($request->status) && $request->status == 0) {
            $query->onlyTrashed();
        }

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

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

     /**
     * Show the form for creating a new resource.
     *
     * @return View
     */
    public function create() :View
    {
        $action    = route('codes.store');
        $head      = metaFields('codes', 'add_new', getCurrentLocale()) ?? __('codes.new');

        return view($this->viewDirectory.'form',['action'=>$action , 'head'=>$head]);
    }

    /**
     * Store a newly created resource in storage.
     * @param CodeRequest $request
     * @return RedirectResponse
     */
    public function store(CodeRequest $request) :RedirectResponse
    {

        Code::create($request->all());

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param Code $code
     * @return View
     */

    public function edit(Code $code):View
    {

        $action    = route('codes.update', $code->id);
        $head      = metaFields('codes', 'edit', getCurrentLocale()) ?? __('codes.edit');
      

        if (is_null($code)) {
            return redirect()->action('Admin\CodeController@index');
        }
            return view( $this->viewDirectory.'form', compact('code', 'action', 'head' ) );
    }

    /**
     * Update the specified resource in storage.
     * @param CodeRequest $request
     * @param Code $code
     * @return RedirectResponse|Redirector
     */

    public function update(CodeRequest $request , Code $code) :RedirectResponse
    {
        $code->update($request->all());
        return redirect($this->redirectSuccessPath)->with('message', __('dashboard.saveDone'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */

    public function destroy(Request $request, int $id) :JsonResponse
    {
        $data = Code::withTrashed()->find($id);
        if ($data) {
            if ($data->deleted_at) {
                $data->forceDelete();
            } else {
                $data->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) {
            Code::onlyTrashed()->whereIn('id', $ids)->forceDelete();
        } else {
            Code::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
    {

        $data = Code::withTrashed()->find($id);

        if ($data){
            $data->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
    {
        $data = Code::whereIn('id', $request->ids)->onlyTrashed();
        if ($data){
            $data->restore();
            return response()->json(['message'=> __('dashboard.saveDone')]);
        }
    }


}
