<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\publicPoetCategory;

class PlCategoryController extends Controller
{
    public function index()
    {
        $category = publicPoetCategory::orderBy('id','desc')->paginate(20);
        return view('back-end.plcategories.create', compact('category'));
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'title_en' => 'required',
        ]);
        $category = new publicPoetCategory();
        $category->name_en = $request->title_en;
        $category->name_ps = $request->title_ps;
        $category->details = $request->title_dr;
        $category->save();
        if ($category) {
            session()->flash("msg", "Success!");
            return redirect()->route('plcategories.index');
        }
    }

    /**
     * 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)
    {
        $category = publicPoetCategory::find($id);
        return view('back-end.plcategories.edit', compact('category'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'title_en' => 'required',
        ]);
        $category = publicPoetCategory::find($id);
        $category->title_en = $request->title_en;
        $category->title_ps = $request->title_ps;
        $category->title_dr = $request->title_dr;
        $category->save();
        if ($category) {
            session()->flash("msg", "Success!");
            return redirect()->route('plcategories.index');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function status($id)
    {
        $category = publicPoetCategory::find($id);
        if ($category->status == 0) {
            publicPoetCategory::where('id', $id)->update([
                'status' => 1
            ]);
        }
        if ($category->status == 1) {
            category::where('id', $id)->update([
                'status' => 0
            ]);
        }
        session()->flash("msg", "Success!");
        return back();
    }
}
