<?php

namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use App\Http\Requests\Dashboard\ServiceRequest;
use App\Http\Requests\Dashboard\ServiceUpdateRequest;
use App\Models\ImageWithDescService;
use App\Models\Service;
use Illuminate\Http\Request;

class AdminServicesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $services = Service::all();
        return view('admin.services.index', compact('services'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $services = Service::where('parent_id' , null)->pluck('name_ar', 'id')->all();
        return view('admin.services.create' , compact('services'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ServiceRequest $request)
    {
        $inputs = $request->except('photo');
        $inputs['photo'] = $request->photo ? $request->photo : 'http://127.0.0.1:8000/storage/1/download.jpeg';
        $service = Service::create($inputs);


        foreach ($request->image as $key => $image) {

            $fileName = time() . '_' . $image->getClientOriginalName();
            $new_image = $image->storeAs('services', $fileName, 'public');

            $service->imagewithdesc()->create([
                'image' => $new_image,
                'desc_ar' => $request->desc2_ar[$key],
                'desc_en' => $request->desc2_en[$key],

            ]);
        }

        if ($request->keyword_ar) {
            foreach ($request->keyword_ar as $key => $keyword_ar) {
                $service->keywords()->create([
                    'body_ar' => $keyword_ar,
                    'body_en' => $request->keyword_en[$key],
                ]);
            }
        }

        session()->flash('service_create_msg', awtTrans('تم الاضافة بنجاح'));
        return redirect('/admin/services');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $service = Service::findOrFail($id);
        $services = Service::where('parent_id' , null)->pluck('name_ar', 'id')->all();

        return view('admin.services.edit', compact('service' , 'services'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(ServiceUpdateRequest $request, $id)
    {

        $inputs = $request->all();
        // dd($inputs);
        Service::findOrFail($id)->update($inputs);

        $service = Service::findOrFail($id);

        if ($request->desc2_arr) {
            // $service->imagewithdesc()->delete();
            foreach ($request->desc2_arr as $key => $desc2_arr) {
                if($request->imagee)
                {
                    $fileName = time() . '_' . $request->imagee[$key]->getClientOriginalName();
                    $new_image = $request->imagee[$key]->storeAs('services', $fileName, 'public');
                } else {
                    $new_image = '';
                }

                $service->imagewithdesc()->create([
                    'image' => $new_image,
                    'desc_ar' => $desc2_arr,
                    'desc_en' => $request->desc2_enn[$key],

                ]);

            }
        }

        if ($request->keyword_ar) {
            $service->keywords()->delete();
            foreach ($request->keyword_ar as $key => $keyword_ar) {
                $service->keywords()->create([
                    'body_ar' => $keyword_ar,
                    'body_en' => $request->keyword_en[$key],
                ]);
            }
        }

        session()->flash('service_update_msg', awtTrans('تم التعديل بنجاح'));
        return redirect()->back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $service = Service::findOrFail($id);
        $service->delete();

        return response()->json();
    }
    //removeImagewithdesc
    public function removeImagewithdesc(Request $request)
    {
        //
        $image = ImageWithDescService::find($request->id);
        $image->delete();

        return response()->json();
    }
    //editImagewithdesc
    public function editImagewithdesc(Request $request)
    {
        // dd($request->all());
        $image = ImageWithDescService::find($request->id);

        $image->update([
            'image' => $request->image == "undefined" ? $image->image : $request->image->storeAs('services', time() . '_' . $request->image->getClientOriginalName(), 'public'),
            'desc_ar' => $request->desc_ar,
            'desc_en' => $request->desc_en,
        ]);

        return response()->json();
    }
}
