<?php

namespace App\Http\Controllers\Admin\Master;

# Models
use App\Models\Announcement;

# Other Classes
use DB;
use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class Announcementcontroller extends Controller
{
    # Define Base view
    protected $view = 'admin.master.announcement.';

    #Bind Announcement model
    protected $announcement;

    /**
     * Defining the default constructor for controller
     *
     */

    public function __construct(
        Announcement       $announcement
    ) {

        $this->announcement     = $announcement;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        try {

            $input = $request->all();

            #get all paymentUser resources
            $announcements = $this->announcement->where('created_at', '!=', null);

            if (isset($input['search_name']) || isset($input['search_status'])) {

                if (isset($input['search_name'])) {
                    $announcements->where('name', 'LIKE', '%' . $input['search_name'] . '%');
                }
                if (isset($input['search_status'])) {
                    $announcements->where('status', $input['search_status']);
                }
            }
            $details['lists'] = $announcements->orderBy('id', 'DESC')->paginate(50);
            #render view
            return view($this->view . 'index', $details);
        } catch (\Exception $ex) {
            toastr()->success($ex->getMessage());
            return back()->with('error', $ex->getMessage());
        }
    }

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

            return view($this->view . 'create');
        } catch (\Exception $ex) {
            toastr()->success($ex->getMessage());
            return back()->with('error', $ex->getMessage());
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        try {
            #validate the request

            $validate = Validator::make($request->all(), [
                'title'                 => 'required',
                'description'           => 'required',
            ]);

            if ($validate->fails()) {
                toastr()->error($validate->messages());
                return redirect()->back()->withInput()->with('error', $validate->messages())->withErrors($validate->messages());
            }
            #get the requested data in Data variable
            $data = [
                'title'                     => $request->title,
                'description'               => $request->description,
                'status'                    => '1',
            ];
            #store the newly creted resoures
            $this->announcement->create($data);
            #return redirect back to view
            toastr()->success('Added Successfully');
            return redirect('/admin/announcement')->with('message', 'Announcement Added Successfully');
        } catch (\Exception $ex) {
            toastr()->success($ex->getMessage());
            return back()->with('error', $ex->getMessage());
        }
    }

    /**
     * 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)
    {
        try {
            # Fetch records
            $details['detail'] = $this->announcement->where('id', $id)->first();
            #return to view
            return view($this->view . 'edit', $details);
        } catch (\Exception $ex) {
            toastr()->success($ex->getMessage());
            return back()->with('error', $ex->getMessage());
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        try {
            #validate the request
            $validate = Validator::make($request->all(), [
                'title'                 => 'required',
                'description'           => 'required',
            ]);

            if ($validate->fails()) {
                toastr()->error($validate->messages());
                return redirect()->back()->withInput()->with('error', $validate->messages())->withErrors($validate->messages());
            }
            #get the requested data in Data variable
            $data = [
                'title'                     => $request->title,
                'description'               => $request->description,
                'status'                    => '1',
            ];

            #update the newly added resoures
            $this->announcement->whereId($id)->update($data);
            #return redirect back to view
            toastr()->success('Updated Successfully');
            return redirect('admin/announcement')->with('message', 'Announcement Updated Successfully');
        } catch (\Exception $ex) {
            toastr()->success($ex->getMessage());
            return back()->with('error', $ex->getMessage());
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $caste = $this->announcement::find($id);
        $caste->delete();
        toastr()->success('You are successfully Deleted');
        return redirect('admin/announcement')->with('message', 'Announcement Deleted Successfully');
    }
    /**
     * change status of specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function changeStatus(Request $request, $id)
    {

        #get the detail of requested resources
        $announcements = $this->announcement->whereId($request->id)->first();

        if ($announcements->status == 1) {
            $status = '0';
        } else {
            $status = '1';
        }
        #get the status in data variable
        $data = ['status' => $status];

        #update status
        $this->announcement->whereId($request->id)->update($data);
        $announcements = $this->announcement->whereId($request->id)->first();
        $message = $announcements->status == 0 ? 'InActive' : 'Active';
        toastr()->success('You are successfully ' . $message);
        return redirect('admin/announcement')->with('message', 'Announcement Status Changed Successfully');
    }
}
