<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use PDF;
class SalaireController extends Controller
{
    function ajouter($id)
    {
        /*if(session('user')== null) {
            return redirect('/');
        }*/
        $idemploye = $id;
        session(['idemploye'=> $idemploye]);
        $employe = DB::table('employe')->where('id', $idemploye)->first();
        $nbreabsence = DB::table('absence')->where('employe_id', $idemploye)->where('etat', 'En cours')->get()->count();
        //$emp=DB::table('employe')->get();
        return view('salaires/nouveau',[
            'id' => $idemploye,
            'employe' => $employe,
            'nbreabsence' => $nbreabsence,
        ]);
    }

    function liste($id) {
        /*if(session('user')== null) {
            return redirect('/');
        }*/
        $salaires = DB::table('salaires')->where('employe_id', $id)->orderBydesc('id')->get();
        return view('salaires/liste',[
            'salaire' => $salaires,
        ]);
    }

    function VoirSuppression ($id) {
        $salaires= DB::table('salaires')->where('id',$id)->first();
        return [
            'salaire' => $salaires,
        ];
    }
    function supprimer () {

        $id=request('id');
        DB::table('salaires')->where('id', $id)->delete();
        return redirect()->route('liste_des_salaires', ['id' => session('idemploye')])->with('status', 'Suppression effectuée avec succes');
    }

    function generer (Request $request, $id){

        $employe= $id;
        $date_debut=request('date_debut');
        $date_debut= explode('/', request('date_debut'));
        $date_debut= array_reverse($date_debut);
        $date_debut= implode('-', $date_debut);
        $date_fin=request('date_fin');
        $date_fin= explode('/', request('date_fin'));
        $date_fin= array_reverse($date_fin);
        $date_fin= implode('-', $date_fin);
        $salaire=request('salaire');
        $nbabsence = request('nbabsence');
        if(request('sal')){
            $sal=collect(request('sal'))->chunk(2);
        }

        $verification= DB::table('salaires')->where('employe_id',$employe)->where('date_debut',$date_debut)->where('date_fin',$date_fin)->first();

        if($verification){
            $message = [
                "title" => 'Error',
                "type" => 'error',
                "response" => 'Désolé vous avez déjà génerer le salaire pour ce mois!!',
            ];

            return $message;

        } else {

            DB::table('salaires')->insert(
                [

                    'employe_id' => $employe,
                    'date_debut' => $date_debut,
                    'date_fin' => $date_fin,
                    'sursalaire' => $sursalaire,
                    'idt_transport' => $idttransport,
                    'idt_logement' => $idtlogement,
                    'idt_fonction' => $idtfonction,
                    'retenu_base' => $retenuebase,
                    'retenuIUTS' => $retenue,
                ]
            );

            $idLastsalaire = DB::table('salaires')->get()->last()->id;
            $i=0;
            if(request('sal')){
                foreach ($sal as $key=> $sals){

                    DB::table('primes')->insert(
                        [
                            'salaire_id' => $idLastsalaire,
                            'type' => $sals[0 +$i],
                            'montant' => $sals[1 +$i],
                        ]
                    );

                    $i += count($sals->toArray());

                }
            }

            $message = [
                "title" => 'success',
                "type" => 'success',
                "response" => 'Salaire enregistrée avec succes !',
            ];
            return $message;
        }


       }
	   public function voirRecuSalaire($id)
    {
        if(session('user')== null) {
            return redirect('/');
        }



        $salaire = DB::table('salaires')->where('id', $id)->first();
        $primes = DB::table('primes')->where('salaire_id', $id)->get();
        $totalprime = DB::table('primes')->where('salaire_id', $id)->get()->sum('montant');
        $employe = DB::table('employe')->where('id', $salaire->employe_id)->first();
        //$agence = DB::table('agence')->where('id', $employe->agence_id)->first();
        //$fonction = DB::table('fonction')->where('id', $employe->fonction_id)->first();
        //$service = DB::table('service')->where('id', $fonction->service_id)->first();
        $salb = DB::table('employe')->where('id', $salaire->employe_id)->first()->salaire_base;
        $coutretardAbsence = round(($salb / 24), 2) ;
        $dds = DB::table('salaires')->where('id', $id)->first()->date_debut;
        $dfs = DB::table('salaires')->where('id', $id)->first()->date_fin;
        $datds=implode('-', array_reverse(explode('/', $dds)));
        $datfs=implode('-', array_reverse(explode('/', $dfs)));
		
        /*$nbreretard = DB::table('retard')->where('employe_id', $salaire->employe_id)
        ->where('motif', 'Aucun motif')
        ->whereBetween('date_retard',[$datds,$datfs])
        ->get()->count();*/

        /*$nbreabsence = DB::table('absence')->where('employe_id', $salaire->employe_id)
        ->where('motif', 'Aucun motif')
        ->whereBetween('date_absence',[$datds,$datfs])
        ->get()->count();*/

        //$totalcoutretard = ($nbreretard * $coutretardAbsence);
        //$totalcoutabsence = ($nbreabsence * $coutretardAbsence);

        $pdf = PDF::loadView('salaires/recu_salaire', [
                 'salaire' => $salaire,
                 'prime' => $primes,
                 'totalprime' => $totalprime,
                 'employe' => $employe,
                 //'agence' => $agence,
                 //'fonction' => $fonction,
                 //'service' => $service,
                 //'nbreretard' => $nbreretard,
                 //'nbreabsence' => $nbreabsence,
                 //'totalcoutretard' => $totalcoutretard,
                 //'totalcoutabsence' => $totalcoutabsence,
                 //'coutretardAbsence' =>  $coutretardAbsence
        ]);
        return $pdf->stream();
    }
}
