<?php

namespace Modules\Admin\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Modules\Admin\Repositories\ComplaintRepository;

class ComplaintController extends Controller
{
    private $complaintRepository;

    public function __construct(ComplaintRepository $complaintRepository)
    {
        $this->complaintRepository = $complaintRepository;
    }
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $data['title'] = 'Complaint Lists';
        $data['complaintLists'] = $this->complaintRepository->findAll();
        return view('admin::complaints.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $data['title'] = 'Create Complaint';
        $data['editable'] = false;
        return view('admin::complaints.create', $data);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request): RedirectResponse
    {
        try {
            $this->complaintRepository->create($request->all());
            toastr()->success('Complaint Created Successfully');

        } catch (\Throwable $th) {
            toastr()->error($th->getMessage());
        }
        return redirect()->route('complaint.index');
    }

    /**
     * Show the specified resource.
     */
    public function show($id)
    {
        return view('admin::complaints.show');
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit($id)
    {
        try {
            $data['title'] = 'Edit Complaint';
            $data['editable'] = true;
            $data['complaint'] = $this->complaintRepository->getComplaintById($id);

        } catch (\Throwable $th) {
            toastr()->error($th->getMessage());
        }

        return view('admin::complaints.edit', $data);

    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, $id): RedirectResponse
    {
        try {

            $this->complaintRepository->update($id, $request->all());
            toastr()->success('Complaint Updated Successfully');

        } catch (\Throwable $th) {
            toastr()->error($th->getMessage());
        }
        return redirect()->route('complaint.index');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {
        try {
            $this->complaintRepository->delete($id);
            toastr()->success('Complaint Deleted Successfully');
        } catch (\Throwable $th) {
            toastr()->error($th->getMessage());
        }
        return redirect()->route('complaint.index');
    }
}