<?php

namespace App\Http\Controllers\RectificacionesActuales;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use App\User;

class c_RectificacionesLZ extends Controller
{
    public function rectificacionActualLZ(Request $request){

        $id = auth()->user()->id;
        $datos = DB::connection('usuariosMysql')->select("select * from vw_list_user WHERE id = '".$id."'");

        foreach($datos as $var){
            $mail = $var->email;
        }

        $LZ = DB::select("SELECT Rectifiaciones FROM referencias_lz WHERE folio = '".$request['id_ref']."'");
        foreach($LZ as $var){ $tRec = $var->Rectifiaciones; }

        $LZ = DB::select("SELECT * FROM referencias_lz WHERE folio = '".$request['id_ref']."'");
        foreach($LZ as $var){
            $cliente = $var->cliente;
            $tip_carga = $var->tip_carga;
            $tip_operacion = $var->tip_operacion;
            $fecha_eta = $var->fecha_eta;
            $cont1 = $var->cont1;
            $cont2 = $var->cont2;
            $cont3 = $var->cont3;
            $cont4 = $var->cont4;
            $cont5 = $var->cont5;
            $cont6 = $var->cont6;
            $cont7 = $var->cont7;
            $cont8 = $var->cont8;
            $cont9 = $var->cont9;
            $cont10 = $var->cont10;
        }
        $especificar = $request['especificar'];
        $cargo = $request['cargo'];
        $tRec = $tRec + 1;
        $cont = $tRec;
        $tRec = $request['id_ref'].'-'.$tRec;

        $LZ = DB::select("SELECT * FROM rectificaciones_lz ORDER BY id DESC LIMIT 1");
        foreach($LZ as $var){ $pedimento_pro = $var->pedimento; }

        $pedimento_pro = $pedimento_pro+1;
        $caracteres = strlen ( $pedimento_pro );
        $age = config('constants.fechaBitacora');
        if($caracteres == 7){ $pedimento_pro = $pedimento_pro; }
        if($caracteres == 4){ $pedimento_pro = ''.$age.'00'.$pedimento_pro; }

        $LZ = DB::select("SELECT * FROM rectificaciones_lz ORDER BY id DESC LIMIT 1");
        foreach($LZ as $var){ $pedimento_verif = $var->pedimento; }

        if($pedimento_pro != $pedimento_verif){
            DB::statement('ALTER TABLE rectificaciones_lz AUTO_INCREMENT = 1');
            DB::insert("INSERT INTO rectificaciones_lz VALUES(null, '".$tRec."', '".$pedimento_pro."', '".$cliente."', '".$tip_carga."', '".$tip_operacion."', '".$fecha_eta."', null, '".$mail."', '".$cont1."', '".$cont2."', '".$cont3."', '".$cont4."', '".$cont5."', '".$cont6."', '".$cont7."', '".$cont8."', '".$cont9."', '".$cont10."', '".$especificar."', '".$cargo."')");
            DB::update("UPDATE referencias_lz SET Rectifiaciones = '".$cont."' WHERE folio = '".$request['id_ref']."'");

            $list = '<tr>';
            $list .= '<td>'.$pedimento_pro.'</td>';
            $list .= '<td>'.$tRec.'</td>';
            $list .= '</tr>';
        }else{
            $list = "<script>alertify.error('Alguien te gano por milisegundos');</script>";
        }

        return $list;
    }
}
