<?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\WarningRepository;
use Modules\Employee\Repositories\EmployeeRepository;

class WarningController extends Controller
{
    private $warningRepository;
    private $employeeRepository;

    public function __construct(WarningRepository $warningRepository, EmployeeRepository $employeeRepository)
    {
        $this->warningRepository = $warningRepository;
        $this->employeeRepository = $employeeRepository;
    }
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $data['title'] = 'Warning Lists';
        $data['warningLists'] = $this->warningRepository->findAll();
        return view('admin::warnings.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $data['title'] = 'Create Warning';
        $data['editable'] = false;
        $data['employeeLists'] = $this->employeeRepository->pluck();
        return view('admin::warnings.create', $data);
    }

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

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

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

    /**
     * Show the form for editing the specified resource.
     */
    public function edit($id)
    {
        try {
            $data['title'] = 'Edit Warning';
            $data['editable'] = true;
            $data['employeeLists'] = $this->employeeRepository->pluck();
            $data['warning'] = $this->warningRepository->getWarningById($id);

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

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

    }

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

            $this->warningRepository->update($id, $request->except(['_method', '_token']));
            toastr()->success('Warning Updated Successfully');

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

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