<?php

namespace App\Http\Controllers\Admin\Pages\RoomCategories;

use App\Http\Controllers\Controller;
use App\Http\Requests\RoomCategories\RoomCategoryStoreRequest;
use App\Http\Requests\RoomCategories\RoomCategoryUpdateRequest;
use App\Models\RoomCategory;
use App\Traits\ImageCompressor;
use DataTables;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use PHPUnit\TextUI\XmlConfiguration\Exception;

class RoomCategoryController extends Controller
{


    use ImageCompressor;

    public function __construct()
    {
        $this->middleware('permission:view roomCategories')->only('index', 'ajax');
        $this->middleware('permission:create roomCategory')->only('create');
        $this->middleware('permission:store roomCategory')->only('store');
        $this->middleware('permission:edit roomCategory')->only('edit');
        $this->middleware('permission:update roomCategory')->only('update');
        $this->middleware('permission:destroy roomCategory')->only('destroy');
    }


    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $roomCategories =RoomCategory::all();
        return view('app.admin.pages.roomCategories.all-roomCategories',compact([
            'roomCategories'
        ]));
    }

    public function search(Request $request)
    {
        $query = $request->input('query');

        $roomCategories = RoomCategory::where('room_category_az', 'LIKE', "%{$query}%")
                            ->orWhere('room_category_ru', 'LIKE', "%{$query}%")
                            ->orWhere('room_category_en', 'LIKE', "%{$query}%")
                            ->get();

        return view('app.admin.pages.roomCategories.all-roomCategories',compact([
            'roomCategories'
        ]));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $roomCategories =RoomCategory::all();
        return view('app.admin.pages.roomCategories.create-roomCategory',compact([
            'roomCategories'
        ]));
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(RoomCategoryStoreRequest $request)
    {
        $validatedData = $request->safe();

        try {

            if ($request->hasFile('image')) {
                $validatedData['image'] = self::compressImage($request->file('image'), 'RoomCategoryImage');
            }

            RoomCategory::create($validatedData->except('partners'));



            return back()->with('success', __('Data successfully created!'));
        } catch (Exception $e) {
            return back()->withErrors([$e->getMessage()]);
        }
    }

    /**
     * Display the specified resource.
     */
    public function show(RoomCategory $roomCategory)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(RoomCategory $roomCategory)
    {
        $roomCategories =RoomCategory::all();
        return view('app.admin.pages.roomCategories.edit-roomCategory',compact([
            'roomCategory',
            'roomCategories'
        ]));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(RoomCategoryUpdateRequest $request, RoomCategory $roomCategory)
    {
        $validatedData = $request->safe();
        try {

            if ($request->hasFile('image')) {
                File::delete('/storage/images/' . $roomCategory->getAttribute('image'));
                $validatedData['image'] = self::compressImage($request->file('image'), 'RoomCategoryImage');
            } else {
                $validatedData['image'] = $roomCategory->getAttribute('image');
            }


            $roomCategory->update($validatedData->except('partners'));


            if ($request->hasFile('partners')) {
                foreach ($request->file('partners') as $image) {
                    MenuCategoryGallery::create([
                        'category_id' => $roomCategory->id,
                        'image' => self::compressImage($image, 'CategoryGallery')
                    ]);
                }
            }

            return back()->with('success', __('Data successfully updated!'));
        } catch (Exception $e) {
            return back()->withErrors([$e->getMessage()]);
        }
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {

        try {

            $roomCategory =RoomCategory::find($id);
            $roomCategory->delete();

            return back()->with('success', __('Data successfully deleted!'));
        } catch (Exception $e) {
            return back()->with('error', $e->getMessage());
        }
    }

    /**
     * @return JsonResponse
     * @throws Exception
     */
    public function ajax()
    {
        $roomCategories = RoomCategory::query();
        return DataTables::of($roomCategories)->editColumn('image', function (RoomCategory $roomCategory) {
            return view('app.admin.includes.table-elements.image', [
                'image' => $roomCategory->image()
            ]);
        })->addColumn('options', function (RoomCategory $roomCategory) {
            return view('app.admin.includes.table-elements.action-buttons', [
                'modelName' => 'roomCategory',
                'editUrl' => route('admin:roomCategories.edit', $roomCategory->getAttribute('id')),
                'deleteUrl' => route('admin:roomCategories.destroy', $roomCategory->getAttribute('id'))
            ]);
        })->toJson();
    }
}
