<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Http\Requests\Backend\PageRequest;
use App\Models\SettingManagement\Page;

class PageController extends Controller{

    public $page;

    public function __construct(Page $page){

        $this->middleware('permission:page-list')
            ->only(["index"]);

        $this->middleware('permission:page-show')
            ->only(["show"]);

        $this->middleware('permission:page-create')
            ->only(["store"]);

        $this->middleware('permission:page-update')
            ->only(['update',"edit"]);

        $this->middleware('permission:page-delete')
            ->only(['destroy']);

        $this->page = $page;
    }

    public function index(){
        $pages = $this->page->all();
        return view('backend.pages.index', compact('pages'));
    }

    public function show(Page $page){
        return view('backend.pages.show', compact('page'));
    }

    public function create(){
        return view('backend.pages.create');
    }

    public function store(PageRequest $request){
        $this->page->create($request->validated());
        return redirect()->route('admin.pages.index')
            ->with(['message' => action_message("create"), 'alert-type' => 'success',]);

    }

    public function edit(Page $page){
        return view('backend.pages.edit', compact('page'));
    }

    public function update(PageRequest $request, Page $page){
        $page->update($request->validated());

        return redirect()->route('admin.pages.index')
            ->with(['message' => action_message("update"), 'alert-type' => 'success',]);
    }

    public function destroy(Page $page){
        $page->delete();
        return redirect()->back()->with(['message' => action_message("delete"), 'alert-type' => 'success',]);
    }

}
