<?php

namespace App\Http\Controllers\Hr;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use App\Http\Requests\DepartmentRequest;
use App\Department;
use App\AssignDepartment;
use EF;
use Illuminate\Contracts\Encryption\DecryptException;

class DepartmentController extends Controller
{
   
    public function index()
    {   
        $department = new Department;
        
        return view('hr.department.manage_department',
        	[

        		'department_list'       => $department->getDepartmentList(),
        		'department_label_list' => EF::departmentWhiteLabel(),
        	]
    );
       
    }

    public function storeDepartment(DepartmentRequest $request)
    {
        $department = new Department;

        $curr_department = $department->storeDepartment($request->input());

        if(isset($curr_department->id))
        {
            parent::setLogMessage('Department "'.$curr_department->name.'" has been created');

            parent::successMessage('Department has been created.',parent::getLogMessage());
        }

        return redirect('departments');
    }


     public function updateDepartment(DepartmentRequest $request)
    {
        try 
        {
            $id = decrypt($request->department);

            $department = new Department;


            $curr_department = $department->updateDepartment($request->input(),$id);

            if(isset($curr_department))
            {
                parent::setLogMessage('Department "'.$curr_department->name.'" has been updated');

                parent::successMessage('Department has been updated.',parent::getLogMessage());
            }
        } 
        catch (DecryptException $e) 
        {
            
        }

        return redirect('departments');
    }

     public function assignDepartment(DepartmentRequest $request)
    {
        try 
        {

            $form_collect  = $request->input();

            $form_collect['department'] = decrypt($form_collect['department']);

            $form_collect['user_id']    = decrypt($form_collect['user_id']);
          

            $assign_department = new AssignDepartment;


            $assign_department_obj = $assign_department->assignDepartment($form_collect);

            if(isset($assign_department_obj))
            {
                $department_name = $assign_department_obj->department->name;

                parent::setLogMessage('Department "'.$department_name.'" has been assigned');

                parent::successMessage('Department has been assigned.',parent::getLogMessage());
            }
        } 
        catch (DecryptException $e) 
        {
            
        }

        return redirect('employees');
    }




     public function removeDepartment($id)
    {
        return redirect('home');
        
        parent::errorMessage('Department not found.');

        try 
        {
            $id = decrypt($id);

            $department = new Department;

            if($department->removeDepartment($id))
            {
                parent::successMessage('Department has been removed.');
            } 
        } 
        catch (DecryptException $e) 
        {
            
        }

        return redirect('departments');
    }
}
