<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Models\Employee;
use App\Repositories\EmployeeRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class EmployeesController extends Controller {

    private $employeeRepository;

    public function __construct(EmployeeRepository $employeeRepository)
    {
        $this->employeeRepository=$employeeRepository;
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $employees=$this->employeeRepository->all();
        return view('employees.index' , compact('employees'));

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        $employee = new Employee();
        return view('employees.create', compact('employee'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store(Requests\CreateEmployeeRequest $request)
    {
        $properties = $request->all();
        $properties['password'] = Hash::make($request->get('password'));
        if($request->get('date_resignation')=="")
            unset($properties['date_resignation']);
        if($request->file('image')) {
            $imageName = $properties['cin']. '.' . $request->file('image')->getClientOriginalExtension();
            $properties['image'] = $imageName;
            $request->file('image')->move(public_path('img/profiles'), $imageName);
        }
        $employee = $this->employeeRepository->create($properties);
        if(!$employee)
            return "404";
        return redirect(route('employee.index'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        $employee = $this->employeeRepository->find($id);
        $logs = $employee->logs()->get();
        return view('employees.show' , compact('employee','logs'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        $employee = $this->employeeRepository->find($id);
        return view('employees.edit' , compact('employee'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update(Requests\EditEmployeeRequest $request)
    {
        $properties = $request->all();
        $properties['password'] = Hash::make($request->get('password'));
        if($request->get('date_resignation')=="")
            unset($properties['date_resignation']);
        if($request->file('image')) {
            $imageName = $properties['cin'] . '.' . $request->file('image')->getClientOriginalExtension();
            $properties['image'] = $imageName;
            $request->file('image')->move(public_path('img/profiles'), $imageName);
        }
        unset($properties['password_confirmation']);
        unset($properties['_method']);
        unset($properties['_token']);
        $employee = $this->employeeRepository->update($properties,$request->get('id'));
        if  (!$employee)
            return abort(403, 'Unauthorized action.');

        return redirect(route('employee.index'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        $this->employeeRepository->delete($id);
        return redirect(route('employee.index'));
    }

}
