<?php

namespace Modules\Employee\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Modules\Employee\Repositories\EmployeeInterface;

class EmployeeController extends Controller
{

    private EmployeeInterface $employeeRepository;

    public function __construct(EmployeeInterface $employeeRepository)
    {
        $this->employeeRepository = $employeeRepository;
    }
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $data['employees'] = $this->employeeRepository->findAll();
        return view('employee::index', $data);
    }

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

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $inputData = $request->only(['first_name', 'last_name']);
        // try {

        if ($request->hasFile('profile_pic')) {
            $fileName = time() . '_' . $request->profile_pic->getClientOriginalName();
            $filePath = $request->file('profile_pic')->storeAs('uploads', $fileName, 'public');
            $inputData['profile_picture'] = time() . '_' . $request->profile_pic->getClientOriginalName();
            // $fileModel->file_path = '/storage/' . $filePath;
            // $fileModel->save();
        }

        $this->employeeRepository->create($inputData);
        toastr()->success('Employee Created Succesfully');
        // } catch (\Throwable $th) {
        //     toastr()->error($th->getMessage());
        // }
        return redirect()->route('employee.index');
    }

    /**
     * Show the specified resource.
     */
    public function show($id)
    {

        return view('employee::show');
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit($id)
    {
        $data['title'] = 'Edit Employee';
        $data['employee'] = $this->employeeRepository->getEmployeeById($id);
        return view('employee::edit', $data);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, $id): RedirectResponse
    {
        $inputData = $request->only(['first_name', 'last_name']);
        // try {

        if ($request->hasFile('profile_pic')) {
            $fileName = time() . '_' . $request->profile_pic->getClientOriginalName();
            $filePath = $request->file('profile_pic')->storeAs('uploads', $fileName, 'public');
            $inputData['profile_picture'] = time() . '_' . $request->profile_pic->getClientOriginalName();
        }

        $this->employeeRepository->update($id, $inputData);
        toastr()->success('Employee Created Succesfully');
        // } catch (\Throwable $th) {
        //     toastr()->error($th->getMessage());
        // }
        return redirect()->route('employee.index');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {
        //
    }
}