<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Repositories\FaqRepository;
use Illuminate\Http\Request;

class FaqController extends Controller
{
    protected $faqRepo;
    public function __construct(FaqRepository $faqRepository)
    {
        $this->faqRepo = $faqRepository;
    }


    public function index()
    {

        return $this->faqRepo->indexPage();
    }

    public function createPage()
    {
        return $this->faqRepo->createPage();

    }

    public function edit($id)
    {
        return $this->faqRepo->editPage($id);

    }

    public function store(Request $request)
    {
        $request->validate([
            'heading' => 'required',
            'description' => 'required',
        ]);
        try {
           $this->faqRepo->create($request->all());
           
            return redirect()->route('admin.faq.list')->with('success', 'Faq Successfully Added');
        } catch (\Exception $e) {
          
            return redirect()->back()->with('error', $e->getMessage());
        }

    }

    public function update(Request $request, $id)
    {
        $request->validate([
            'heading' => 'required',
            'description' => 'required',
        ]);
        try {
            $this->faqRepo->update($request->all(), $id);
            return redirect()->route('admin.faq.list')->with('success', 'Faq Successfully Updated');
        } catch (\Exception $e) {
            return redirect()->back()->with('error', $e->getMessage());

        }

    }

    public function delete(Request $request)
    {
        $this->faqRepo->delete($request->id);
        return response()->json(['success' => 'Record successfully deleted!']);
    }
}
