<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Imports\Employee_detailsImport;
use App\Exports\Employee_detailsExport;
use Maatwebsite\Excel\Facades\Excel;
use Maatwebsite\Excel\HeadingRowImport;
use Illuminate\Support\Facades\Input;
use App\EmployeeTest;


class EmployeesController extends Controller
{
    
    public function index(){
        Return View('Employees.index');
    }
    //exportation function
    public function export() 
    {
        try {
            $export=Excel::download(new Employee_detailsExport, 'Employees.xls');
            return $export;
        } catch (\Exception $e) {
            return redirect('/employees')->with('error','Something wrong happened');
            
        }
        
    }
    //importation function
    public function import(Request $request) 
    {
        
        $this->validate($request, array(
            'file'      => 'required'
        ));
        try {
            if ($request -> hasFile('file')) {
                $extension = $request ->file ->getClientOriginalExtension();
                if ($extension == "xlsx" || $extension == "xls" || $extension == "csv" || $extension == "xlsm") {
                    $name = $request->file->getClientOriginalName();  
                     
                    $import = Excel::import(new Employee_detailsImport, request()->file('file'));
                      dd($import);
                     return redirect('/employees')->with('success','File has been successfully imported');
                }
                else {                 
                    return redirect('/employees')->with('error','File is a '.$extension.' file.!! Please upload a valid xls/csv file..!!'); 
                }
            }
            
        }
        catch (\Maatwebsite\Excel\Validators\ValidationException $e) {

            $failures = $e->failures();  
            // dd($failures);       
                            
            foreach ($failures as $failure) {
                $failure->row(); // row that went wrong
                
                $failure->attribute(); //  column index
                
                $failure->errors(); // Actual error messages from Laravel validator 
                
                 return redirect('/employees')->withErrors($failures);                                  
            }                          
        }           
        catch(\Exception $e){
            return redirect('/employees')->with('error','Something wrong happened');
        }
        
    } 
    //
    public function form(){
        $departments = DB::table('departments')->get();
        $businessunits = DB::table('business_units')->get();
        return view('Employees.Registration', compact(['departments','businessunits']));
        // Return View('Employees.Registration');
    } 
    //storing form attributes
    public function store()
    {
        $this->validate(request(), [
            'InputFirstname' => 'required',
            'InputLastname' => 'required',
            'InputEmail' => 'required|distinct',
            'InputGender' => 'required',
            'InputDate' => 'required',
            'InputYearsofExperience' => 'required',
        ]);
        
            $form = new EmployeeTest;
            
            //$form->employeid = request('employeid');
            $form->FirstName = request('InputFirstname');
            $form->LastName = request('InputLastname');
            $form->Email = request('InputEmail');
            $form->Gender = request('gender');
            $form->DateofJoining = request('InputDate');
            $form->YearsofExperience = request('InputYearsofExperience');
            $form->save();
          
            return redirect('/registration')->with('success','Employee has successfully been added');
    } 
   
}
