<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
use App\Http\Controllers\SendEmailsOperatorController;

class SendEmailImmatNotFountToAll extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'mail:send_immat_not_found_to_all {--yes}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Envoyer un mail a tous les opérateurs dont le(s) véhicules ont un problème de remontée chez PVO/B2L';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        if($this->option('yes')){
            $array_sending_mail_operator = SendEmailsOperatorController::listVehiclesImmatNotFound();
            for($i = 0; $i < count($array_sending_mail_operator); $i++){

                    $find = true;
                    $destinataire = $array_sending_mail_operator[$i]["email"];
                    $operator_id = $array_sending_mail_operator[$i]["operator_id"];
                    $probleme = '';
                    $name = $array_sending_mail_operator[$i]["name"];

                    foreach($array_sending_mail_operator[$i]["vehicules"] as $items){
                        $error1 = $items['error1'];
                        if(isset($items['error2']))
                        {
                            $error2 = $items['error2'];
                            $probleme .= $items['name'] . " : " . $items['error1'] . " & " . $items['error2'] . " <a href=\"https://mycarlab.fr/clients/" . $items['client_id'] . "/vehicule/" . $items['id'] . "\">Modifier</a><br>";
                        }
                        else
                        {
                            $probleme .= $items['name'] . " : " . $items['error1'] . " <a href=\"https://mycarlab.fr/clients/" . $items['client_id'] . "/vehicule/" . $items['id'] . "\">Modifier</a><br>";
                        }
                        if(env('EMAIL_DEV_TEST_TO_ALL') == FALSE)
                        {
                            SendEmailsOperatorController::edit($items['id']);
                        }
                    }
                    if(isset($req['erreurs'])){
                        $this->error("Error : " .  $req['erreurs'] . " !\n" . "Échec de l'envoi des mails !");
                    }
                    else{
                        $this->info("Success !\n" . "Les mails ont bien été envoyé aux opérateurs");
                    }
                    $email = file_get_contents(resource_path('views/email_immat_not_found.blade.php'));
                    $lien_immat = str_replace("{liens_vehicules}", $probleme, $email);
                    $message = str_replace("{Operator_name}", $array_sending_mail_operator[$i]["name"], $lien_immat);

                    $req = Http::asForm()->post('https://www.spot-hit.fr/api/envoyer/e-mail', [
                        'key' => '495d6d6f3018d0d42ead704e6f9de82f',
                        'sujet' => 'Problème de remontage de véhicule(s)',
                        'message' => $message,
                        'destinataires' => $destinataire,
                        'expediteur' => 'carlab@test.com',
                        'nom_expediteur' => 'Carlab'
                    ])->json();

            }
        }
        else{
            $this->info("Vous allez envoyer un email a tous les opérateurs dont le(s) véhicule(s) ne sont pas remontés. \n");
            if($this->confirm('Êtes-vous sur ?')){

                $array_sending_mail_operator = SendEmailsOperatorController::listVehiclesImmatNotFound();
                for($i = 0; $i < count($array_sending_mail_operator); $i++){

                        $find = true;
                        $destinataire = $array_sending_mail_operator[$i]["email"];
                        $operator_id = $array_sending_mail_operator[$i]["operator_id"];
                        $probleme = '';
                        $name = $array_sending_mail_operator[$i]["name"];

                        foreach($array_sending_mail_operator[$i]["vehicules"] as $items){
                            $error1 = $items['error1'];
                            if(isset($items['error2']))
                            {
                                $error2 = $items['error2'];
                                $probleme .= $items['name'] . " : " . $items['error1'] . " & " . $items['error2'] . " <a href=\"https://mycarlab.fr/clients/" . $items['client_id'] . "/vehicule/" . $items['id'] . "\">Modifier</a><br>";
                            }
                            else
                            {
                                $probleme .= $items['name'] . " : " . $items['error1'] . " <a href=\"https://mycarlab.fr/clients/" . $items['client_id'] . "/vehicule/" . $items['id'] . "\">Modifier</a><br>";
                            }
                            if(env('EMAIL_DEV_TEST') == FALSE)
                            {
                                SendEmailsOperatorController::edit($items['id']);
                            }
                        }
                        if(isset($req['erreurs'])){
                            $this->error("Error : " .  $req['erreurs'] . " !\n" . "Échec de l'envoi des mails !");
                        }
                        else{
                            $this->info("Success !\n" . "Les mails ont bien été envoyé aux opérateurs");
                        }
                        $email = file_get_contents(resource_path('views/email_immat_not_found.blade.php'));
                        $lien_immat = str_replace("{liens_vehicules}", $probleme, $email);
                        $message = str_replace("{Operator_name}", $array_sending_mail_operator[$i]["name"], $lien_immat);

                        $req = Http::asForm()->post('https://www.spot-hit.fr/api/envoyer/e-mail', [
                            'key' => '495d6d6f3018d0d42ead704e6f9de82f',
                            'sujet' => 'Problème de remontage de véhicule(s)',
                            'message' => $message,
                            'destinataires' => $destinataire,
                            'expediteur' => 'carlab@test.com',
                            'nom_expediteur' => 'Carlab'
                        ])->json();

                }
            }
        }
    }
}

