<?php

namespace App\Jobs;
use Illuminate\Filesystem\Filesystem;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use App\Department;
use App\Division;
use App\UsersDb;
use App\ConflictOfInterest;
use App\Annexure_One;
use App\Annexure_Two;
use Illuminate\Support\Facades\Storage;
use App\Annexure_Three;
use App\Annexure_Four;
use App\Annexure_Five;
use App\Annexure_Six;
use App\Annexure_Seven;
use App\Annexure_Eight;
use App\Annexure_Nine;
use App\Annexure_Ten;
use App\Annexure_Eleven;
use App\Annexure_Twelve;
use App\Annexure_Thirteen;
use App\Annexure_Fourteen;
use App\Annexure_Status;
use App\AdminModel;
use App\Annexure_Fifteen;
use Illuminate\Support\Facades\Session;
use PDF;
use ZipArchive;

class GenerateBatchCodes implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $data;

    public $tries = 1;

    public $timeout = 9999999999999999;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {

      try {
        Log::info('run log');
      $abs_path = storage_path("/app/public/download/");
if (file_exists($abs_path)) {
  //File::delete($abs_path.$dfname.'.pdf');
  $file = new Filesystem;
$file->cleanDirectory($abs_path);
}
      ini_set('max_execution_time', 5580);
      $data = $this->data;
      // Log::info($data);
      foreach ($data['deptt'] as $key ) {

      //check if user has started anything
      //
      // $checkstatus = Annexure_Status::where('userid',$key->id)->count();
      //
      //
      // //return redirect(route('report.groupmanager.index'))->with('error','No '.$div->name.' member has filled the COI Form');
      // if ($checkstatus>0) {
      $a1 = Annexure_One::query()->where('userid',$key->id)->first();

      $a2 = Annexure_Two::query()->where('userid',$key->id)->first();

      $a3 = Annexure_Three::query()->where('userid',$key->id)->first();

      $a4 = Annexure_Four::query()->where('userid',$key->id)->first();

      $a5 = Annexure_Five::query()->where('userid',$key->id)->first();

      $a6 = Annexure_Six::query()->where('userid',$key->id)->first();

      $a7 = Annexure_Seven::query()->where('userid',$key->id)->first();

      $a8 = Annexure_Eight::query()->where('userid',$key->id)->first();

      $a9 = Annexure_Nine::query()->where('userid',$key->id)->first();

      $a10 = Annexure_Ten::query()->where('userid',$key->id)->first();

      $a11 = Annexure_Eleven::query()->where('userid',$key->id)->first();


      $a12 = Annexure_Twelve::query()->where('userid',$key->id)->first();


      $a13 = Annexure_Thirteen::query()->where('userid',$key->id)->first();


      $a14 = Annexure_Fourteen::query()->where('userid',$key->id)->first();


      $a15 = Annexure_Fifteen::query()->where('userid',$key->id)->first();

      $a16 = Annexure_Fifteen::query()->where('userid',$key->id)->first();
      $coi = ConflictOfInterest::query()->where('userid',$key->id)->first();
      $dfname = 'MMTN_COI_'.$key->name.'_'.date('m-Y');
      $data=[
      'a1'=>$a1,
      'a2'=>$a2,
      'a3'=>$a3,
      'a4'=>$a4,
      'a5'=>$a5,
      'a6'=>$a6,
      'a7'=>$a7,
      'a8'=>$a8,
      'a9'=>$a9,
      'a10'=>$a10,
      'a11'=>$a11,
      'a12'=>$a12,
      'a13'=>$a13,
      'a14'=>$a14,
      'a15'=>$a15,
      'a16'=>$a16,
      'coi'=>$key,
      ];
      // $abs_path = storage_path("/tmp/");
      // if (file_exists($abs_path)) {
      //   File::delete($abs_path.$dfname.'.pdf');
      //   $file = new Filesystem;
      // $file->cleanDirectory($abs_path);
      // }

      $adminidd = Session::get('adminid');
      //
      // $admin = AdminModel::where('id',$adminidd)->first();
      // $admin->log('(Admin):Download coi of department ');

      $pdf = PDF::loadView('admin.annexures.download',$data)->save($abs_path.$dfname.'.pdf');

    }

    $zipper = new \Chumper\Zipper\Zipper;
    $files = $abs_path;

            $zipper->make($abs_path."Department_Report.zip")->add($files)->close();

              if (File::exists($abs_path."Department_Report.zip")) {
Log::info('done');
// return response()->download($abs_path."Department_Report.zip");
// return redirect()->route('report.groupmanager.dept.index');
$rrr = Storage::url("storage/Department_Report.zip");

return Storage::download($rrr);
}
            // return response()->download($abs_path."Department_Report.zip");

            }catch (\Exception $ex)
            {
            Log::info($ex->getMessage());
            // $updatebatch = BatchGenerated::where('id',$data['batch_id'])->first();
            // $updatebatch->status='Generation Failed';
            // $updatebatch->error_mess=$ex->getMessage();
            // $updatebatch->save();
            }











    }

}
