<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Page;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class PageController extends Controller
{

     /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    function __construct()
    {
        $this->middleware('role_or_permission:Page access|Page create|Page edit|Page delete', ['only' => ['index','show']]);
        $this->middleware('role_or_permission:Page create', ['only' => ['create','store']]);
        $this->middleware('role_or_permission:Page edit', ['only' => ['edit','update']]);
        $this->middleware('role_or_permission:Page delete', ['only' => ['destroy']]);
    }


    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = Page::all();
        return view('admin.page.index',compact('data'));
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = new Page;
        $request->validate([
            'title' => 'required|min:3|unique:pages',
            'content' => 'min:3', //
            'meta_title' => 'min:3|max:255',
            'meta_description' => 'min:3',
            'meta_keywords' => 'min:3',
            'meta_image' => 'image|mimes:jpeg,png,jpg,gif|max:2048',
        ]);

        $data->title = $request->title;
        $data->content = $request->content;
        $data->meta_title = $request->meta_title;
        $data->meta_description = $request->meta_description;
        $data->meta_keywords = $request->meta_keywords;
        $data->slug = Str::slug($request->title);


        if ($request->hasFile('meta_image')) {
            $image_path = $data->meta_image;
            if(file_exists($image_path)){
                unlink($image_path);
            }
            $logo = $request->file('meta_image');
            $filename = time() . '.' . $logo->getClientOriginalExtension();
            $logo->storeAs('public/pages-logo', $filename);
            $data->meta_image = 'storage/pages-logo/'.$filename;
        }

        $data->save();

        $notification = array(
            'messege' => 'New Pages Created Successfully',
            'alert-type' => 'success'
        );
        return Redirect()->route('admin.pages.index')->with($notification);
    }

    /**
     * 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)
    {
        $data = Page::find($id);
        return view('admin.page.edit',compact('data'));
    }

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


        $request->validate([
            'title' => 'min:3|unique:pages,title,' . $data->id,
            'content' => 'min:3',
            'meta_title' => 'min:3|max:255',
            'meta_description' => 'min:3',
            'meta_keywords' => 'min:3',
            'meta_image' => 'image|mimes:jpeg,png,jpg,gif|max:2048',
        ]);

        $data->title = $request->title;
        $data->content = $request->content;
        $data->meta_title = $request->meta_title;
        $data->meta_description = $request->meta_description;
        $data->meta_keywords = $request->meta_keywords;
        $data->slug = Str::slug($request->title);


        if ($request->hasFile('meta_image')) {
            $image_path = $data->meta_image;
            if(file_exists($image_path)){
                unlink($image_path);
            }
            $logo = $request->file('meta_image');
            $filename = time() . '.' . $logo->getClientOriginalExtension();
            $logo->storeAs('public/pages-logo', $filename);
            $data->meta_image = 'storage/pages-logo/'.$filename;
        }

        $data->save();

        $notification = array(
            'messege' => 'Updated Pages Successfully',
            'alert-type' => 'success'
        );
        return Redirect()->route('admin.pages.index')->with($notification);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = Page::find($id);
        $data->delete();
        $notification = array(
            'messege' => 'Deleted Pages Successfully !!!',
            'alert-type' => 'success'
        );
        return Redirect()->back()->with($notification);
    }
}
