<?php

namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use App\Http\Requests\Dashboard\PostRequest;
use App\Http\Requests\Dashboard\PostUpdateRequest;
use App\Models\Category;
use App\Models\Photo;
use App\Models\Post;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

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

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(PostRequest $request)
    {

        $inputs = $request->except('photo', 'keyword_ar', 'keyword_en');
        $inputs['photo'] = $request->photo ? $request->photo : 'http://127.0.0.1:8000/storage/1/download.jpeg';
        $user = Auth::user();

        $post = $user->posts()->create($inputs);

        if ($request->images > 0) {
            foreach (explode(',', $request->images) as $image) {
                $post->images()->create([
                    'image' => $image,
                ]);
            }
        }

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

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

    /**
     * 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)
    {
        //
        $post = Post::findOrFail($id);
        $categories = Category::pluck('name_ar', 'id')->all();
        return view('admin.posts.edit', compact('post', 'categories'));
    }

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

        $inputs = $request->all();
        $user = Auth::user();

        $user->posts()->whereId($id)->first()->update($inputs);

        $post = Post::find($id);

        if ($request->images > 0) {
            $post->images()->delete();
            foreach (explode(',', $request->images) as $image) {
                $post->images()->create([
                    'image' => $image,
                ]);
            }
        }

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

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

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $post = Post::findOrFail($id);
        $post->delete();
        session()->flash('post_action_msg', '"' . $post->title . '" is deleted');
        return response()->json();
    }

    // bluckDelete
    public function bluckDelete(Request $request)
    {

        $posts = Post::findOrFail($request->ids);

        foreach ($posts as $post) {
            $post->delete();
        } // end foeach

        session()->flash('post_action_msg', awtTrans('تم الحذف بنجاح'));
        return redirect()->back();
    }
}
