<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use DB;

class TaxeController extends Controller
{
    //


    function nouveau() {

        if(session('user')== null) {
            return redirect('/');
        }

        $taxe = DB::table('taxes')->orderBydesc('id')->get();

        return view('taxes/nouveau',[

            'taxe' => $taxe,

        ]);
    }

    function venteticket() {

        if(session('user')== null) {
            return redirect('/');
        }

        $typeticket = DB::table('ticket')->get();

        $ticket = DB::table('vente_ticket')->orderBydesc('id')->get();
        $client = DB::table('client_externe')->orderBydesc('id')->get();

        return view('ticket/vente_ticket',[

            'ticket' => $ticket,
            'typeticket' => $typeticket,
            'client' => $client,

        ]);
    }




    function detail($id) {

        if(session('user')== null) {
            return redirect('/');
        }

        $detail = DB::table('ligne_vente_ticket')->where('vente_ticket_id', $id)->get(); 

        return view('ticket/detail',[

            'detail' => $detail,

        ]);
    }

    function modifier($id) {

        if(session('user')== null) {
            return redirect('/');
        }

        $taxe = DB::table('taxes')->orderBydesc('id')->get();

        $tax = DB::table('taxes')->where('id', $id)->first(); 

        return view('taxes/modification',[

            'taxe' => $taxe,
            'tax' => $tax,

        ]);
    }


    function enregistrer_vente (Request $request){

        $client= request("client");
        $typeticket= request("typeticket");
        $debut_plage= request("debut_plage");
        $qte= request("qte");
        $dateventetic= implode('-', array_reverse(explode('/', request('dateventetic'))));

        $montyptic = DB::table('ticket')->where('id', $typeticket)->first()->montant;
        $verifplage = DB::table('ligne_vente_ticket')->where('ref', $debut_plage)->first(); 
		
		if ($verifplage){
			$message = [
                    "title" => 'Attention',
                    "type" => 'error',
                    "response" => 'Ce numero de ticket a été déjà utilisé',
                ];
        
                return $message; 
		} else {
            DB::table('vente_ticket')->insert(

                [   
                    'montant' => ( $montyptic * $qte),
                    'type_ticket' => $typeticket,
                    'date' => $dateventetic,
                    'qte' => $qte,
                    'debut_plage' => $debut_plage,
                    'client_externe_id' => $client,
                ]
            );

            $lastventeid = DB::table('vente_ticket')->get()->last()->id;

            $lastventeqte = DB::table('vente_ticket')->get()->last()->qte;

            $lastventetype = DB::table('vente_ticket')->get()->last()->type_ticket;
			
			$lastventetyped = $lastventeqte - 1;

            for ($i=0; $i <= $lastventetyped; $i++) {
				$t = ($debut_plage + $i);
                 DB::table('ligne_vente_ticket')->insert(
                     [
 
                           'vente_ticket_id' => $lastventeid,
                           'ref' => $t, 
                           'type_ticket_id' => $lastventetype,
                           'client_externe_id' => $client,
                     ]);
              
            }

               $message = [
                    "title" => 'success',
                    "type" => 'success',
                    "response" => 'Enregistrement effectué avec succès !',
                ];
        
                return $message; 
    }
	}  


    function enregistrer (Request $request){

        $taux= request("taux");
        $lib= request("lib");
        $date= implode('-', array_reverse(explode('/', request('date'))));
              
        $veri = DB::table('taxes')->where('taux', $taux)->where('date_entree', $date)->first();

            if($veri){
                $message = [
                    "title" => 'Error',
                    "type" => 'error',
                    "response" => 'Cette taxe a été déjà enregistrée !',
                ];
    
                return $message;
            }

            else{
                  
                    DB::table('taxes')->insert(
    
                        [   
                            'taux' => $taux,
                            'date_entree' => $date,
                            'lib' => $lib,
                            'user_id' => session('user')->id,
                        ]
                    );
        
                $message = [
                    "title" => 'success',
                    "type" => 'success',
                    "response" => 'Enregistrement effectué avec succès !',
                ];
        
                return $message; 
            } 

    }


    function valider (Request $request, $id){

        $taux= request("taux");
        $lib= request("lib");
        //$datetic= implode('-', array_reverse(explode('/', request('datetic'))));
        $today = date('Y-m-d');     
        //$veri = DB::table('ticket')->where('id', $id)->where('montant', $mont)->where('date', $datetic)->first();

            /*if($veri){
                $message = [
                    "title" => 'Error',
                    "type" => 'error',
                    "response" => 'Ce ticket a été déjà enregistrée !',
                ];
    
                return $message;
            }

            else{ */
                  
                DB::table('taxes')->where('id', $id)->update(
    
                        [   
                            'taux' => $taux,
                            'date_entree' => $today,
                            'lib' => $lib,
                        ]
                    );
        
                $message = [
                    "title" => 'success',
                    "type" => 'success',
                    "response" => 'Modification effectuée avec succès !',
                ];
        
                return $message; 
            }

    


    function VoirSuppressionventeticket ($id) {

        $venteticket= DB::table('vente_ticket')->where('id',$id)->first();
        return [

            'venteticket' => $venteticket,
        ];
    }

    function supprimerventeticket () {

        $id=request('id');

        $veri =  DB::table('vente')->where('vente_ticket_id', $id)->first();

        if($veri) {

            return redirect()->route('vente_ticket')->with('error', 'Suppression impossible ! l\'un des tickets de cette vente a été déjà utilisé');
        
        }else{

            DB::table('ligne_vente_ticket')->where('vente_ticket_id', $id)->delete();

            DB::table('vente_ticket')->where('id', $id)->delete();
    
            return redirect()->route('vente_ticket')->with('status', 'Suppression effectuée avec succes');
        }

    }


    function VoirSuppression ($id) {

        $taxe= DB::table('taxes')->where('id',$id)->first();
        return [

            'taxe' => $taxe,
        ];
    }

    function supprimer () {

        $id=request('id');

        DB::table('taxes')->where('id', $id)->delete();

        return redirect()->route('nouveau-taxe')->with('status', 'Suppression effectuée avec succes');
    }

}
