<?php

namespace App\Http\Controllers\Admin\Faq;

use App\Http\Controllers\Controller;
use App\Models\Faq;
use App\Models\FaqHeader;
use Illuminate\Http\Request;

class FaqController extends Controller
{
    public function index(){
        $faqs=Faq::orderBy('created_at', 'DESC')->get();
        return view('admin.pages.faq.index',compact('faqs'));
    }
    public  function add(){
        return view('admin.pages.faq.add');
    }
    public function save(Request $request)
    {
        $request->validate([
            'question' => 'required',
            'ans' => 'required',

        ]);


        $faq = new Faq();
        $faq->question = $request->get('question');
        $faq->ans = $request->get('ans');
        $faq->save();


        return redirect()->route('admin::faq')->with('success','FAQ Add Successfully');

    }

    public function edit($id){
        $faq=Faq::find($id);
        return view('admin.pages.faq.edit',compact('faq'));
    }
    public function update(Request $request,$id)
    {
        $request->validate([
            'question' => 'required',
            'ans' => 'required',

        ]);

        $faq = Faq::find($id);

        $faq->question = $request->get('question');
        $faq->ans = $request->get('ans');
        $faq->save();
        return redirect()->route('admin::faq')->with('success','FAQ Update Successfully');
    }
    public function delete($id)
    {
        $data = Faq::where('id',$id)->first();
        if (!empty($data)) {
            $data->delete();
            return response()->json(['status' => 200]);
        }else{
            return response()->json(['status' => 400]);
        }
    }
    public function status(Request $request)
    {
        $id = $request->get('id');
        $status = $request->get('status');
        if($status=='Active'){
            Faq::where('id',$id)->update([
                'status' => 'Inactive',
            ]);
            $st='Inactive';
            $html='<a href="javascript:void(0);" class="btn btn-warning btn-sm" onclick="active_inactive_solution('.$id.','.$st.')" ><span class="fa fa-ban"></span> </a>&emsp;';
            return json_encode(array('id'=>$id,'html'=>$html, 'status'=>$st));
        }
        else{
            Faq::where('id',$id)->update([
                'status' => 'Active',
            ]);
            $st='Active';
            $html = '<a href="javascript:void(0);" class="btn btn-success btn-sm" onclick="active_inactive_solution('.$id.','.$st.')"><span class="fa fa-check"></span> </a>&emsp;';
            return json_encode(array('id'=>$id,'html'=>$html, 'status'=>$st));
        }
    }

    public function faqheader(){
        $faqsheder=FaqHeader::get();
        return view('admin.pages.faq.faq',compact('faqsheder'));
    }

    public function edit_faq_header($id){
        $faqshead=FaqHeader::find($id);
        return view('admin.pages.faq.faq_edit',compact('faqshead'));
    }
    public function update_faq_header(Request $request,$id)
    {
        $request->validate([
            'title' => 'required',
            'description' => 'required',

        ]);

        $faq = FaqHeader::find($id);

        $faq->title = $request->get('title');
        $faq->description = $request->get('description');
        $faq->save();
        return redirect()->route('admin::faq_header')->with('success','FAQ Header Update Successfully');
    }

    public function status_faq_header(Request $request)
    {
        $id = $request->get('id');
        $status = $request->get('status');
        if($status=='Active'){
            FaqHeader::where('id',$id)->update([
                'status' => 'Inactive',
            ]);
            $st='Inactive';
            $html='<a href="javascript:void(0);" class="btn btn-warning btn-sm" onclick="active_inactive_solution('.$id.','.$st.')" ><span class="fa fa-ban"></span> </a>&emsp;';
            return json_encode(array('id'=>$id,'html'=>$html, 'status'=>$st));
        }
        else{
            FaqHeader::where('id',$id)->update([
                'status' => 'Active',
            ]);
            $st='Active';
            $html = '<a href="javascript:void(0);" class="btn btn-success btn-sm" onclick="active_inactive_solution('.$id.','.$st.')"><span class="fa fa-check"></span> </a>&emsp;';
            return json_encode(array('id'=>$id,'html'=>$html, 'status'=>$st));
        }
    }

}
