<?php

namespace App\Console\Commands;


use Carbon\Carbon;
use Illuminate\Console\Command;
use DB;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Redirect;
use Portal\Ecuti\Model\VwMainAppVerify;
use Portal\Ecuti\Model\VwMainAppApproval;
use Portal\Ecuti\Model\MainApp;
use Portal\Ecuti\Model\AppFdCartaMain;
use App\User;
use Mail;





class NotifyAction extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'schedule:notify-action';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Send Notification to verifier and approval (status 2 and 4)';
    
 

    /**
     * Create a new command instance.
     *
     * @return void
     */
   /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
            $mainapplist = array();
            $data = array();


            $checkactive=DB::SELECT('SELECT status from settings where id = 1 limit 1');

            if($checkactive['0']->status == 1)
            {

                    $verirylist = VwMainAppVerify::where('status', '=', 2)
                                                    ->select('vuid','email')
                                                    ->groupBy('vuid','email')->get();

                    $verirylistcount = VwMainAppVerify::where('status', '=', 2)
                                                    ->select('vuid','email')
                                                    ->groupBy('vuid','email')->first();
                    echo('a1-Check Verify');echo "\n"; 
                    if($verirylistcount)
                    {
                        echo('a2-list all application on verify');echo "\n"; 
                        foreach ($verirylist as $key => $value) 
                        {

                                $vuids = VwMainAppVerify::where('status', '=', 2)
                                                        ->where('vuid', '=', $value->vuid )
                                                        ->get();

                               
                                $int = 0;
                                foreach ($vuids as $key => $values) 
                                {
                                   
                                    $data[] = $values->id;

                                }
                                         

                                $users = User::where('id','=',$value->vuid)->get();

                                $update = DB::SELECT("INSERT INTO v_test (`main_id`,`case`) value ('$users','12')");

                                Mail::to($users)->send(new \Portal\Ecuti\Events\NotifyEmailAction($data,12));

                                $data = [];

                        
                        }
                        echo('a3-email all application on verify');echo "\n"; 
                    }else
                    {
                        $update = DB::SELECT("INSERT INTO v_test (`main_id`,`case`) values ('notfound','12')");
                        echo('a2-No Verify Found');echo "\n"; echo "\n"; 
                    }

                    $approvallist = VwMainAppApproval::where('status', '=', 4)
                                                    ->select('auid','email')
                                                    ->groupBy('auid','email')->get();

                    $approvallistcount = VwMainAppApproval::where('status', '=', 4)
                                                    ->select('auid','email')
                                                    ->groupBy('auid','email')->first();

                    echo('b1-check approval');echo "\n"; 
                    if($approvallistcount)
                    {                     
                        echo('b2-list all application on approval');    echo "\n";        
                        foreach ($approvallist as $key => $value) 
                        {
                                $auids = VwMainAppApproval::where('status', '=', 4)
                                                        ->where('auid', '=', $value->auid )
                                                        ->get();

                               
                                $int = 0;
                                foreach ($auids as $key => $values) 
                                {
                                   
                                    $data[] = $values->id;

                                }
                                         
                                $users = User::where('id','=',$value->auid)->get();

                                Mail::to($users)->send(new \Portal\Ecuti\Events\NotifyEmailAction($data,13));
                                $update = DB::SELECT("INSERT INTO v_test (`main_id`,`case`) value ('$users','13')");

                                $data = [];
                        }
                        echo('b3-email all application on approval');echo "\n"; 
                    }else
                    {
                        $update = DB::SELECT("INSERT INTO v_test (`main_id`,`case`) values ('notfound','13')");
                        echo('b2-No Approval Found'); echo "\n"; 
                    }

                    $checkactive=DB::SELECT('UPDATE settings set status = 2 where id = 1');
                    echo('c-close cron'); echo "\n"; 
            }else{

                $update = DB::SELECT("INSERT INTO v_test (`main_id`,`case`) values ('cron not active','2')");
                echo('d-No active Cron');
            }





               
                  

    }
}

