<?php

namespace App\Controllers;

use App\Models\SettingsModel;


class SettingsController extends BaseController
{
    public function __construct()
    {
        $this->Settings_model = new SettingsModel();
    }
    private $Settings_model;
    public function index()
    {

        $setting = $this->appSetting();
        $data = [

            'setting' => $setting,
            'title' => 'Pengaturan'
        ];
        return view('Panel/Settings/settingssekolah', $data);
    }
    public function getData()
    {
        $setting = $this->Settings_model->get_by_id(1);

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

    public function update($id = 1)
    {
        $appName = $this->request->getPost('appname');
        $logo = $this->request->getFile('logo');
        $logomentri = $this->request->getFile('logomentri');
        $kopsurat = $this->request->getFile('kopsurat');
        // Inisialisasi data awal
        $data = [
            'appname' => $appName,
            'nama_sekolah' => $this->request->getPost('nama_sekolah'),
            'jenjang' => $this->request->getPost('jenjang'),
            'alamat_sekolah' => $this->request->getPost('alamat_sekolah'),
            'kelurahan' => $this->request->getPost('kelurahan'),
            'kecamatan' => $this->request->getPost('kecamatan'),
            'kota' => $this->request->getPost('kota'),
            'provinsi' => $this->request->getPost('provinsi'),
            'website' => $this->request->getPost('website'),
            'email' => $this->request->getPost('email'),
            'npsn' => $this->request->getPost('npsn'),
            'nss' => $this->request->getPost('nss'),
            'nama_kepsek' => $this->request->getPost('nama_kepsek'),
            'kementrian' => $this->request->getPost('kementrian'),
            'nip_kepsek' => $this->request->getPost('nip_kepsek'),
            'key_encrypt' => $this->request->getPost('key_encrypt')
        ];
        // Validasi untuk Logo
        if (!empty($logo->getName())) {
            if ($logo->isValid() && ($logo->getClientMimeType() === 'image/png' || $logo->getClientMimeType() === 'image/jpeg')) {
                $maxSize = 2 * 1024 * 1024; // Ukuran maksimum 2MB
                if ($logo->getSize() > $maxSize) {
                    $response = ['message' => 'Ukuran file logo terlalu besar.'];
                    return $this->response->setJSON($response)->setStatusCode(400);
                }
                // Baca data gambar
                $imageData = file_get_contents($logo->getTempName());

                // Konversi gambar ke base64
                $base64Image = base64_encode($imageData);
                // Pindahkan logo ke folder uploads
                $logo->move(FCPATH . 'assets/img');

                $data['logo'] = $logo->getName();
            } else {
                $response = ['message' => 'Jenis file logo tidak valid. Harap unggah file PNG atau JPEG.'];
                return $this->response->setJSON($response)->setStatusCode(400);
            }
        }
        if (!empty($logomentri->getName())) {
            if ($logomentri->isValid() && ($logomentri->getClientMimeType() === 'image/png' || $logomentri->getClientMimeType() === 'image/jpeg')) {
                $maxSize = 2 * 1024 * 1024; // Ukuran maksimum 2MB
                if ($logomentri->getSize() > $maxSize) {
                    $response = ['message' => 'Ukuran file logo terlalu besar.'];
                    return $this->response->setJSON($response)->setStatusCode(400);
                }
                // Mendapatkan nama sementara file
                $tempPath = $logomentri->getTempName();
                // Pindahkan logo ke folder uploads
                $newFileName = "logomentri." . $logomentri->getExtension();
                //$logomentri->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['logokementrian'] = $newFileName;
            } else {
                $response = ['message' => 'Jenis file logo tidak valid. Harap unggah file PNG atau JPEG.'];
                return $this->response->setJSON($response)->setStatusCode(400);
            }
        }
        if (!empty($kopsurat->getName())) {
            if ($kopsurat->isValid() && ($kopsurat->getClientMimeType() === 'image/png' || $kopsurat->getClientMimeType() === 'image/jpeg')) {
                $maxSize = 2 * 1024 * 1024; // Ukuran maksimum 2MB
                if ($kopsurat->getSize() > $maxSize) {
                    $response = ['message' => 'Ukuran file kopsurat terlalu besar.'];
                    return $this->response->setJSON($response)->setStatusCode(400);
                }
                $newFileName = "kop_surat." . $kopsurat->getExtension();
                // Pindahkan logo ke folder uploads
                $kopsurat->move(FCPATH . 'assets/img', $newFileName, true);
                // Perbarui nilai logo di $data jika logo valid
                $data['kop_surat'] = $newFileName;
            } else {
                $response = ['message' => 'Jenis file kop tidak valid. Harap unggah file PNG atau JPEG.' . $kopsurat->getName()];
                return $this->response->setJSON($response)->setStatusCode(400);
            }
        }

        $exec = $this->Settings_model->settings_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)
            ->fit(200, 200, 'center')
            ->save($targetPath, $quality); // Simpan gambar dengan kualitas kompresi yang ditentukan
    }
    public function updatekartu($id = 1)
    {
        $data = [];
        $kartudepanlandscape = $this->request->getFile('filedepanlandscape');
        // Inisialisasi data awal
        // Validasi untuk Logo
        if (!empty($kartudepanlandscape->getName())) {
            if ($kartudepanlandscape->isValid() && ($kartudepanlandscape->getClientMimeType() === 'image/png' || $kartudepanlandscape->getClientMimeType() === 'image/jpeg')) {
                $maxSize = 2 * 1024 * 1024; // Ukuran maksimum 2MB
                if ($kartudepanlandscape->getSize() > $maxSize) {
                    $response = ['message' => 'Ukuran file logo terlalu besar.'];
                    return $this->response->setJSON($response)->setStatusCode(400);
                }
                // Generate nama file baru
                $newFileName = 'bc_depan_landscape.' . $kartudepanlandscape->getExtension();

                // Pindahkan logo ke folder uploads dan ganti nama file
                $kartudepanlandscape->move(FCPATH . 'assets/img/kartu', $newFileName, true);

                // Perbarui nilai logo di $data dengan nama file baru
                $data['kartu_depan_landscape'] = $newFileName;
            } else {
                $response = ['message' => 'Jenis file logo tidak valid. Harap unggah file PNG atau JPEG.'];
                return $this->response->setJSON($response)->setStatusCode(400);
            }
            $exec = $this->Settings_model->settings_update(['id' => $id], $data);
        }
        $kartubelakanglandscape = $this->request->getFile('filebelakanglandscape');
        // Inisialisasi data awal
        // Validasi untuk Logo
        if (!empty($kartubelakanglandscape->getName())) {
            if ($kartubelakanglandscape->isValid() && ($kartubelakanglandscape->getClientMimeType() === 'image/png' || $kartubelakanglandscape->getClientMimeType() === 'image/jpeg')) {
                $maxSize = 2 * 1024 * 1024; // Ukuran maksimum 2MB
                if ($kartubelakanglandscape->getSize() > $maxSize) {
                    $response = ['message' => 'Ukuran file logo terlalu besar.'];
                    return $this->response->setJSON($response)->setStatusCode(400);
                }
                // Generate nama file baru
                $newFileName = 'bc_belakang_landscape.' . $kartubelakanglandscape->getExtension();

                // Pindahkan logo ke folder uploads dan ganti nama file
                $kartubelakanglandscape->move(FCPATH . 'assets/img/kartu', $newFileName, true);

                // Perbarui nilai logo di $data dengan nama file baru
                $data['kartu_belakang_landscape'] = $newFileName;
            } else {
                $response = ['message' => 'Jenis file logo tidak valid. Harap unggah file PNG atau JPEG.'];
                return $this->response->setJSON($response)->setStatusCode(400);
            }
            $exec = $this->Settings_model->settings_update(['id' => $id], $data);
        }

        $kartudepanportrait = $this->request->getFile('filedepanportrait');

        if (!empty($kartudepanportrait->getName())) {
            if ($kartudepanportrait->isValid() && ($kartudepanportrait->getClientMimeType() === 'image/png' || $kartudepanportrait->getClientMimeType() === 'image/jpeg')) {
                $maxSize = 2 * 1024 * 1024; // Ukuran maksimum 2MB
                if ($kartudepanportrait->getSize() > $maxSize) {
                    $response = ['message' => 'Ukuran file logo terlalu besar.'];
                    return $this->response->setJSON($response)->setStatusCode(400);
                }
                // Generate nama file baru
                $newFileName = 'bc_depan_portrait.' . $kartudepanportrait->getExtension();

                // Pindahkan logo ke folder uploads dan ganti nama file
                $kartudepanportrait->move(FCPATH . 'assets/img/kartu', $newFileName, true);

                // Perbarui nilai logo di $data dengan nama file baru
                $data['kartu_depan_portrait'] = $newFileName;
            } else {
                $response = ['message' => 'Jenis file logo tidak valid. Harap unggah file PNG atau JPEG.'];
                return $this->response->setJSON($response)->setStatusCode(400);
            }
            $exec = $this->Settings_model->settings_update(['id' => $id], $data);
        }
        $kartubelakangportrait = $this->request->getFile('filebelakangportrait');
        // Inisialisasi data awal
        // Validasi untuk Logo
        if (!empty($kartubelakangportrait->getName())) {
            if ($kartubelakangportrait->isValid() && ($kartubelakangportrait->getClientMimeType() === 'image/png' || $kartubelakangportrait->getClientMimeType() === 'image/jpeg')) {
                $maxSize = 2 * 1024 * 1024; // Ukuran maksimum 2MB
                if ($kartubelakangportrait->getSize() > $maxSize) {
                    $response = ['message' => 'Ukuran file logo terlalu besar.'];
                    return $this->response->setJSON($response)->setStatusCode(400);
                }
                // Generate nama file baru
                $newFileName = 'bc_belakang_portrait.' . $kartubelakangportrait->getExtension();

                // Pindahkan logo ke folder uploads dan ganti nama file
                $kartubelakangportrait->move(FCPATH . 'assets/img/kartu', $newFileName, true);

                // Perbarui nilai logo di $data dengan nama file baru
                $data['kartu_belakang_portrait'] = $newFileName;
            } else {
                $response = ['message' => 'Jenis file logo tidak valid. Harap unggah file PNG atau JPEG.'];
                return $this->response->setJSON($response)->setStatusCode(400);
            }
            $exec = $this->Settings_model->settings_update(['id' => $id], $data);
        }


        return $this->response->setJSON(['success' => true, 'message' => 'update successfully']);
    }
}
