<?php

namespace App\Http\Controllers\Backend\Others;

use App\Http\Controllers\Controller;
use App\Models\Feature;
use App\Models\Language;
use Illuminate\Http\Request;
use Validator;

class FeatureController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if ($request->has('lang')) {
            $language = Language::where('code', $request->lang)->firstOrFail();
            $features = Feature::where('lang', $language->code)->with('language')->get();
            return view('backend.others.features.index', ['features' => $features, 'active' => $language->name]);
        } else {
            return redirect(url()->current() . '?lang=' . env('DEFAULT_LANGUAGE'));
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('backend.others.features.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'lang' => ['required', 'string', 'max:3', 'exists:languages,code'],
            'title' => ['required', 'string', 'max:255', 'min:2'],
            'image' => ['required', 'image', 'mimes:png,jpg,jpeg,svg', 'max:2048'],
            'body' => ['required', 'string', 'max:600'],
        ]);
        if ($validator->fails()) {
            foreach ($validator->errors()->all() as $error) {
                toastr()->error($error);
            }
            return back()->withInput();
        }
        $image = fileUpload($request->file('image'), 'images/others/features/');
        if ($image) {
            $feature = Feature::create([
                'lang' => $request->lang,
                'title' => $request->title,
                'image' => $image,
                'body' => $request->body,
            ]);
            if ($feature) {
                toastr()->success(admin_trans('Created Successfully'));
                return redirect(route('admin.features.index') . '?lang=' . $feature->lang);
            }
        } else {
            toastr()->error(admin_trans('Upload error'));
            return back();
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Feature  $feature
     * @return \Illuminate\Http\Response
     */
    public function show(Feature $feature)
    {
        return abort(404);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Feature  $feature
     * @return \Illuminate\Http\Response
     */
    public function edit(Feature $feature)
    {
        return view('backend.others.features.edit', ['feature' => $feature]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Feature  $feature
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Feature $feature)
    {
        $validator = Validator::make($request->all(), [
            'lang' => ['required', 'string', 'max:3', 'exists:languages,code'],
            'title' => ['required', 'string', 'max:255', 'min:2'],
            'image' => ['nullable', 'image', 'mimes:png,jpg,jpeg,svg', 'max:2048'],
            'body' => ['required', 'string', 'max:600'],
        ]);
        if ($validator->fails()) {
            foreach ($validator->errors()->all() as $error) {
                toastr()->error($error);
            }
            return back()->withInput();
        }
        if ($request->has('image')) {
            $image = fileUpload($request->file('image'), 'images/others/features/', null, null, $feature->image);
        } else {
            $image = $feature->image;
        }
        if ($image) {
            $update = $feature->update([
                'lang' => $request->lang,
                'title' => $request->title,
                'image' => $image,
                'body' => $request->body,
            ]);
            if ($update) {
                toastr()->success(admin_trans('Updated Successfully'));
                return back();
            }
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Feature  $feature
     * @return \Illuminate\Http\Response
     */
    public function destroy(Feature $feature)
    {
        removeFile(public_path($feature->image));
        $feature->delete();
        toastr()->success(admin_trans('Deleted Successfully'));
        return back();
    }
}
