<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;

class EmployeController extends Controller
{
    //
    function employe()
    {
		if(session('user')== null) {
            return redirect('/');
        }
		$employe = DB::table('employe')->orderBydesc('id')->get();
	return view('employes/nouveau', [
		'employe' => $employe,
	]);
    }
    function liste() {
        if(session('user')== null) {
            return redirect('/');
        }
        $employe = DB::table('employe')->orderBydesc('id')->get();
        return view('employes/liste',[
            'employe' => $employe,
        ]);
    }
    function listehors() {
        if(session('user')== null) {
            return redirect('/');
        }
        $employe = DB::table('employe')->where('statut', 0)->orderBydesc('id')->get();
        return view('employes/listehors',[
            'employe' => $employe,
        ]);
    }
    function listesuspendu() {
        if(session('user')== null) {
            return redirect('/');
        }
        $employe = DB::table('employe')->where('statut', 2)->orderBydesc('id')->get();
        return view('employes/listesuspendu',[
            'employe' => $employe,
        ]);
    }
    function modifier($id) {
        if(session('user')== null) {
            return redirect('/');
        }
        $emp = DB::table('employe')->where('id', $id)->first();
        $employe = DB::table('employe')->orderBy('nom', 'asc')->get();
        return view('employes/modifier',[
            'employe' => $employe,
            'emp' => $emp,
        ]);
    }

    function enregistrer(Request $request)
    {

        $nom=request('nom');
        $prenoms=request('prenoms');
        $fonction=request('fonction');
        $telephone=request('telephone');
        $salaire_base=request('salaire_base');
        $typeemploye=request('typeemploye');
        $today=date('Y-m-d');

        $verification= DB::table('employe')->where('nom',$nom)->where('prenoms',$prenoms)->first();
        if($verification){
            $message = [
                "title" => 'Error',
                "type" => 'error',
                "response" => 'Cet enregistrement existe déjà',
            ];

            return $message;
        }
        else {
	
				DB::table('employe')->insert(
                [
                    'nom' => $nom,
                    'prenoms' => $prenoms,
                    'fonction' => $fonction,
                    'typeemploye' => $typeemploye,
                    'salaire_base' => $salaire_base,
                    'telephone' => $telephone,
					'date' => $today,
                ]
            );
            $idLastemploye = DB::table('employe')->get()->last()->id;
            $id2= strlen($idLastemploye);
            if ($id2==1){
                DB::table('employe')->where('id', $idLastemploye)->update(
                    [
                        'matricule' => '00'.$idLastemploye,
                    ]
                );
            } elseif ($id2==2){
                DB::table('employe')->where('id', $idLastemploye)->update(
                    [
                        'matricule' => '0'.$idLastemploye,
                    ]
                );
            } else {
                DB::table('employe')->where('id', $idLastemploye)->update(
                    [
                        'matricule' => $idLastemploye,
                    ]
                );
            }
            $message = [
                "title" => 'success',
                "type" => 'success',
                "response" => 'Enregistrement effectué avec succès !',
            ];

            return $message;

		}
    }

    function valider(Request $request, $id)
    {

        $nom=request('nom');
        $prenoms=request('prenoms');
        $fonction=request('fonction');
        $telephone=request('telephone');
        $salaire_base=request('salaire_base');
        $typeemploye=request('typeemploye');
        $today=date('Y-m-d');

        DB::table('employe')->where('id', $id)->update(
                [
                    'nom' => $nom,
                    'prenoms' => $prenoms,
                    'fonction' => $fonction,
                    'typeemploye' => $typeemploye,
                    'salaire_base' => $salaire_base,
                    'telephone' => $telephone,
					'date' => $today,
                ]
            );
            return redirect()->route('liste_des_employes')->with('status', 'Modification effectuée avec succes');
    }

    function Voir ($id) {
        $emp= DB::table('employe')->where('id',$id)->first();
        return [
            'emp' => $emp,
        ];
    }
	function absence($id) {
        if(session('user')== null) {
            return redirect('/');
        }
		$absence= DB::table('absence')->where('employe_id',$id)->get();
        return view('employes/absence',[
            'id' => $id,
            'absence' => $absence,
        ]);
    }
	function enregistrer_absence($id)
    {
        $idemp=request('idemp');
        $motif=request('motif');
        $date_absence=implode('-', array_reverse(explode('/', request('date_absence'))));
		$verif = DB::table('absence')->where('employe_id', $idemp)->where('date_absence', $date_absence)->first();
		if ($verif){
			 $message = [
                "title" => 'Attention',
                "type" => 'error',
                "response" => 'Une absence a déjà été enregistrée sur cette date !',
            ];
            return $message;
		} else {
            DB::table('absence')->insert(
                [
                    'employe_id' => $idemp,
                    'motif' => $motif,
                    'date_absence' => $date_absence,
                ]
            );
		}
            $message = [
                "title" => 'success',
                "type" => 'success',
                "response" => 'Enregistrement effectué avec succès !',
                "idemploye" => $idemp,
            ];

            return $message;

    }
	function voirabsence ($id) {
        $absence= DB::table('absence')->where('id',$id)->first();
        return [
            'absence' => $absence,
        ];
    }
	function supprimerabsence() {
		$id = request('id');
        $emp = DB::table('absence')->where('id', $id)->first()->employe_id;
        DB::table('absence')->where('id',$id)->delete();
        return redirect()->route('absence_employe',['id'=>$emp])->with('status', 'Suppression effectuée avec succès');
    }
    //GESTION DES AVANCES
    function enregistrer_avance($id)
    {
        $idemp=request('idemp');
        $montant=request('montant');
        $date_avance=implode('-', array_reverse(explode('/', request('date_avance'))));
        $today = date('Y-m-d');
		$verif = DB::table('avance')->where('employe_id', $idemp)->where('date_avance', $date_avance)->first();
		if ($verif){
			 $message = [
                "title" => 'Attention',
                "type" => 'error',
                "response" => 'Une avancea déjà été enregistré sur cette date !',
            ];
            return $message;
		} else {
            DB::table('avance')->insert(
                [
                    'employe_id' => $idemp,
                    'montant' => $montant,
                    'date_avance' => $date_avance,
                    'dateenreg' => $today,
                    'user_id' => session('user')->id,
                ]
            );
		}
            $message = [
                "title" => 'success',
                "type" => 'success',
                "response" => 'Enregistrement effectué avec succès !',
                "idemploye" => $idemp,
            ];

            return $message;
    }
    function voiravance ($id) {
        $avance= DB::table('avance')->where('id',$id)->first();
        return [
            'avance' => $avance,
        ];
    }
	function supprimeravance() {
		$id = request('id');
        $emp = DB::table('avance')->where('id', $id)->first()->employe_id;
        DB::table('avance')->where('id',$id)->delete();
        return redirect()->route('avance_employe',['id'=>$emp])->with('status', 'Suppression effectuée avec succès');
    }
    function avance($id) {
        if(session('user')== null) {
            return redirect('/');
        }
		$avance= DB::table('avance')->where('employe_id',$id)->get();
        return view('employes/avance',[
            'id' => $id,
            'avance' => $avance,
        ]);
    }

}
