<?php

namespace App\Controllers;

use App\Models\Kelulusan_model;
use App\Models\Siswa_model;
use Dompdf\Dompdf;

class Kelulusan extends BaseController
{
    public function __construct()
    {
        $this->Kelulusan_model = new Kelulusan_model();
        $this->Siswa_model = new Siswa_model();
    }
    private $Kelulusan_model;
    private $Siswa_model;
    public function index()
    {

        $setting = $this->appSetting();
        $data = [
            // 'nama_user' => $user->nama,
            'setting' => $setting,
            'title' => 'Cetak SKL'
        ];
        return view('Admin/Kelulusan/index', $data);
    }
    public function indexTemplate()
    {

        $setting = $this->appSetting();
        $data = [
            // 'nama_user' => $user->nama,
            'setting' => $setting,
            'title' => 'Template SKL'
        ];
        return view('Admin/Kelulusan/settings_skl', $data);
    }
    public function getDataTemplate()
    {
        $setting = $this->Kelulusan_model->get_by_id(1);

        return $this->response->setJSON($setting);
    }

    public function updateTemplate($id = 1)
    {

        $ttd = $this->request->getFile('ttd');
        // $ttd = $this->request->getFile('ttd');
        // $kopsurat = $this->request->getFile('kopsurat');
        // Inisialisasi data awal
        $data = [
            'nama_surat' => $this->request->getPost('judul'),
            'header_surat' => $this->request->getPost('header_surat'),
            'no_surat' => $this->request->getPost('no_surat'),
            'tgl_surat' => $this->request->getPost('tgl_surat'),
            'dasar_surat' => $this->request->getPost('dasar_surat'),
            'isi_surat' => $this->request->getPost('isi_surat'),
            'penutup_surat' => $this->request->getPost('penutup_surat'),
            'nilai' => $this->request->getPost('nilai') ?? 'N',
            'jurusan' => $this->request->getPost('jurusan') ?? 'N',
            'kop_img' => $this->request->getPost('kop_img') ?? 'N',
        ];

        if (!empty($ttd->getName())) {
            if ($ttd->isValid() && ($ttd->getClientMimeType() === 'image/png' || $ttd->getClientMimeType() === 'image/jpeg')) {
                $maxSize = 2 * 1024 * 1024; // Ukuran maksimum 2MB
                if ($ttd->getSize() > $maxSize) {
                    $response = ['message' => 'Ukuran file logo terlalu besar.'];
                    return $this->response->setJSON($response)->setStatusCode(400);
                }
                // Mendapatkan nama sementara file
                $tempPath = $ttd->getTempName();
                // Pindahkan logo ke folder uploads
                $newFileName = "ttd." . $ttd->getExtension();
                //$ttd->move(FCPATH . 'assets/img', $newFileName, true);
                $path = FCPATH . 'assets/img/' . $newFileName;
                $this->compressImage($tempPath, $path, 80);
                $imageData = file_get_contents($path);
                $base64Image = base64_encode($imageData);
                $data['ttd'] = $newFileName;
                $data['ttd64'] = $base64Image;
            } else {
                $response = ['message' => 'Jenis file logo tidak valid. Harap unggah file PNG atau JPEG.'];
                return $this->response->setJSON($response)->setStatusCode(400);
            }
        }

        $exec = $this->Kelulusan_model->kelulusan_update(['id' => $id], $data);

        if ($exec) {
            return $this->response->setJSON(['success' => true, 'message' => 'User update successfully']);
        } else {
            //return $this->response->setJSON($error);
            return $this->response->setJSON(['success' => false, 'message' => 'Tidak ada data yang mengalami perubahan']);
        }
    }
    private function compressImage($sourcePath, $targetPath, $quality)
    {
        $image = \Config\Services::image(); // Menginisialisasi objek Image Manipulation
        $image->withFile($sourcePath)

            ->save($targetPath, $quality); // Simpan gambar dengan kualitas kompresi yang ditentukan
    }
    public function printSKL($idsiswa)
    {

        // Load library DOMPDF
        $siswa = $this->Siswa_model->get_by_id($idsiswa);
        $settingskl = $this->Kelulusan_model->get_setting_skl();
        $penilaian = $this->Kelulusan_model->get_penilaian($siswa->nis);
        // dd($penilaian);
        $setting = $this->appSetting();
        $data = [
            'siswa' => $siswa,
            'setting' => $setting,
            'settingskl' => $settingskl,
            'penilaian' => $penilaian
        ];
        $dompdf = new Dompdf();
        // Render halaman HTML menjadi PDF
        $html = view('Admin/Kelulusan/print_skl', $data); // Ganti 'pdf_template' dengan nama file view Anda
        $dompdf->loadHtml($html);
        // Atur ukuran kertas secara kustom (F4)
        $dompdf->setPaper(array(0, 0, 609.4488, 935.433), 'portrait'); // Gunakan array dengan nilai (0, 0, lebar, tinggi)

        $dompdf->render();

        // Tampilkan PDF dalam browser
        $dompdf->stream("SKL-" . $siswa->nama . ".pdf", array("Attachment" => false));

        // Untuk menyimpan PDF ke server, Anda dapat menggunakan kode berikut:
        // $output = $dompdf->output();
        // file_put_contents('path/to/save/file.pdf', $output);
    }
}
