<?php

namespace App\Http\Controllers;

use App\Models\PengajuanAduan;
use App\Models\User;

use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Request;

class LayananPengajuanAduanController extends Controller
{

public function index(){
    $data = PengajuanAduan::get();
    return view('layanan-aduan.index',['title' => 'Pengajuan Aduan | Admin Dashboard','data' => $data]);
}

public function tambah(){
    return view('layanan-aduan.tambah',['title' => 'Pengajuan Aduan']);
}

public function store(Request $request){
    $request->validate([
        'nama'=>'required',
        'nomor_telepon'=>'required|numeric',
        'subjek'=>'required',
        'lokasi'=>'required',
        'kategori'=>'required',
        'deskripsi_laporan'=>'required',
        'bukti_laporan'=>'required|mimes:pdf,png,jpg|max:4096',
    ],[
        'nomor_telepon.numeric' => 'nomor telepon tidak valid, harap menggunakan format 089xxxxx',
        'bukti_laporan.max' => 'Ukuran File Anda lebih dari 4Mb, Cek Ulang File anda',
        'bukti_laporan.mimes' => 'Format file tidak valid, pastikan file berupa gambar(JPG/PNG) atau pdf',
    ]);
    //get lastid + 1
    $countcaseid = PengajuanAduan::max('id')+1;
    $caseid = "A".date_format(date_create(),"Y").$countcaseid;

    $data_aduan['caseid'] = $caseid;
    $data_aduan['nama'] = $request->nama;
    $data_aduan['nomor_telepon'] = $request->nomor_telepon;
    $data_aduan['subjek'] = $request->subjek;
    $data_aduan['lokasi'] = $request->lokasi;
    $data_aduan['kategori'] = $request->kategori;
    $data_aduan['deskripsi_laporan'] = $request->deskripsi_laporan;
    
    $bukti_laporan = $request->file('bukti_laporan');
    $bukti_laporan_name = $caseid.'_'.str_replace(' ', '_',$bukti_laporan->getClientOriginalName());
    $path = 'aduan/'.$bukti_laporan_name;
    if(Storage::disk('public')->put($path, file_get_contents($bukti_laporan))){
        $data_aduan['bukti_laporan'] = $bukti_laporan_name;
        PengajuanAduan::create($data_aduan);
        return redirect()->route('layanan-aduan.tambah')->with('success','Sukses Tambah Data, Silahkan Monitoring Pengajuan Anda melalui menu monitoring');
    }
    return redirect()->route('layanan-aduan.tambah')->with('error','Gagal Tambah data, Cek kembali data yang anda masukkan'); 
}

public function selesai(Request $request){
    $request->validate([
        'keterangan_selesai'=>'required',
    ],[
        'keterangan_selesai.required' => 'Alasan Tidak boleh kosong, harap masukkan keterangan penyelesaian',
    ]);
    $data_aduan = PengajuanAduan::where('id',$request->id)->first();
    $data_aduan['keterangan_selesai'] = $request->keterangan_selesai;
    if($data_aduan->save()){
        return redirect()->route('layanan-aduan.index')->with('success','Sukses Menyelesaikan Laporan Aduan');
    }
    return redirect()->route('layanan-aduan.tambah')->with('error','Gagal Menyelesaikan Laporan aduan, silahkan coba klik selesaikan kembali');
}

public function destroy(Request $request){
    $id = $request->id;
    //cari data dan kirim notif sebelum dihapus
    $data = PengajuanAduan::where('id',$id)
    ->first();
    // $data_job = [
    //     'jenis' => 'penolakan',
    //     'judul_seminar_proposal' => $data->judul_seminar_proposal,
    //     'nama_mahasiswa' => $data->nama,
    //     'nim_mahasiswa' => $data->nim,
    //     'email' => $data->email,
    //     'created_at' => $data->created_at,
    //     'alasan' => $request->alasan,
    // ];
    $path = 'aduan/'.$data->bukti_laporan;
    //hapus file yang diupload mahasiswa
    if(Storage::disk('public')->exists($path)){
        //hapus data pengajuan mahasiswa
        Storage::disk('public')->delete($path);
    }
    
    $dataDelete = PengajuanAduan::findOrFail($id);
    
    //kirim notifikasi jika data berhasil dihapus
    if($dataDelete->delete()){
        // dispatch(new layananPengajuanDpuDpaJob($data_job));
        return redirect()->route('layanan-aduan.index')->with('success','Permintaan Sudah Diproses, Email akan segera dikirim ke penerima');
    }
    return redirect()->route('layanan-aduan.index')->with('error','Gagal Hapus Data, coba cek data yang akan dihapus lalu coba lagi!');
}
}

