<?php

namespace App\Jobs;

use App\Models\PengajuanSidangAkhir;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

use Ixudra\Curl\Facades\Curl;

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

    public $data;
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        if($this->data['jenis']==="penolakan"){
            $text =  "BETA\n".
            "Penolakan Pengajuan Sidang Akhir Mahasiswa: ".$this->data['nama']."\n".
            "Dengan Email : ".$this->data['email']."\n\n".
            "Alasan : ".$this->data['alasan'];
            Curl::to('https://api.telegram.org/bot6161516511:AAGZhf_o31HupIogjUbEgvwjukes-3P3zPo/sendMessage')
            ->withData( array( 'chat_id' => '-1001903808078', 'text' => $text, 'parse_mode' => 'Markdown') )
            ->get();
        }
        elseif($this->data['jenis'] == 'mahasiswa'){
            $data = PengajuanSidangAkhir::where('pengajuan_sidang_akhirs.id',$this->data['id'])
            ->select(
                'pengajuan_sidang_akhirs.caseid',
                'pengajuan_sidang_akhirs.nilai_plagiasi',
                'mahasiswas.nama',
                'mahasiswas.nim',
                'mahasiswas.email',
                'mahasiswas.judul_skripsi_id',
                'mahasiswas.judul_skripsi_en',
                'mahasiswas.dosen_dpu_id',)
            ->join('mahasiswas','mahasiswas.id','=','mahasiswas_id')
            ->first();
            $text =  "BETA - ".$data->caseid."\n".
            "Kirim Pengajuan A/N Mahasiswa: ".$data->nama."\n".
            "Kepada mahasiswa : ".$data->nama."\n\n".
            "Alamat Email : ".str_replace('_','\_',$data->email);
            Curl::to('https://api.telegram.org/bot6161516511:AAGZhf_o31HupIogjUbEgvwjukes-3P3zPo/sendMessage')
            ->withData( array( 'chat_id' => '-1001903808078', 'text' => $text, 'parse_mode' => 'Markdown') )
            ->get();
        }
        elseif($this->data['jenis'] == 'mahasiswa selesai'){
            $data = PengajuanSidangAkhir::where('pengajuan_sidang_akhirs.id',$this->data['id'])
            ->select(
                'pengajuan_sidang_akhirs.caseid',
                'pengajuan_sidang_akhirs.nilai_plagiasi',
                'mahasiswas.nama',
                'mahasiswas.nim',
                'mahasiswas.judul_seminar_proposal',
                'mahasiswas.dosen_dpu_id',
                'users.nama_gelar',
                'users.email')
            ->join('mahasiswas','mahasiswas.id','=','mahasiswas_id')
            ->join('users','users.id','=','dosen_dpu_id')
            ->first();
            $text =  "BETA - ".$data->caseid."\n".
            "Pengajuan Selesai A/N Mahasiswa: ".$data->nama."\n".
            "Kepada DPU : ".$data->nama_gelar."\n\n".
            "Alamat Email : ".str_replace('_','\_',$data->email);
            Curl::to('https://api.telegram.org/bot6161516511:AAGZhf_o31HupIogjUbEgvwjukes-3P3zPo/sendMessage')
            ->withData( array( 'chat_id' => '-1001903808078', 'text' => $text, 'parse_mode' => 'Markdown') )
            ->get();
        }
        elseif($this->data['jenis'] == 'dpu'){
            $data = PengajuanSidangAkhir::where('pengajuan_sidang_akhirs.id',$this->data['id'])
            ->select(
                'pengajuan_sidang_akhirs.caseid',
                'pengajuan_sidang_akhirs.nilai_plagiasi',
                'mahasiswas.nama',
                'mahasiswas.nim',
                'mahasiswas.judul_seminar_proposal',
                'mahasiswas.dosen_dpu_id',
                'users.nama_gelar',
                'users.email')
            ->join('mahasiswas','mahasiswas.id','=','mahasiswas_id')
            ->join('users','users.id','=','dosen_dpu_id')
            ->first();
            $text =  "BETA - ".$data->caseid."\n".
            "Kirim Pengajuan A/N Mahasiswa: ".$data->nama."\n".
            "Kepada DPU : ".$data->nama_gelar."\n\n".
            "Alamat Email : ".str_replace('_','\_',$data->email);
            Curl::to('https://api.telegram.org/bot6161516511:AAGZhf_o31HupIogjUbEgvwjukes-3P3zPo/sendMessage')
            ->withData( array( 'chat_id' => '-1001903808078', 'text' => $text, 'parse_mode' => 'Markdown') )
            ->get();
        }
    }
}