<?php
defined('BASEPATH') or exit('No direct script access allowed');

class ControllerMaintenance extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->set_timezone();
        $this->load->library('encrypt');
        $this->load->library('Mycustom');
        $this->set_timezone();
    }
    public function set_timezone()
    {
        date_default_timezone_set("Asia/Jakarta");
    }
    public function index()
    {
        $hari = $this->mycustom->hariIndo(date('l', strtotime(date('Y-m-d'))));
        if ($hari == 'Minggu') :
            redirect('notifikasi/maintenance_active/3w6Zp6L6pLwOchNCud7j3BWC9BepfF5z');
        else:
            redirect('notifikasi/maintenance_finish/3w6Zp6L6pLwOchNCud7j3BWC9BepfF5z');
        endif;
    }

    public function active($id)
    {
        $kode_rahasia_asli = 'BAP_2025_API';
        $kode_rahasia = $this->encrypt->decode($id);
        $from = $this->path_root . 'finish_maintenance.php';
        $to   = $this->path_root . 'maintenance.php';
        if ($kode_rahasia_asli != $kode_rahasia) :
            redirect('/');
        else:
            if (file_exists($from)) {
                if (@rename($from, $to)) {
                    $this->notifMaintenanceAktif();
                    redirect('/');
                } else {
                    $this->notifMaintenanceAktifGagal();
                    redirect('/');
                }
            } else {
                $this->notifMaintenanceAktifGagal();
                redirect('/');
            }
        endif;
    }

    public function finish($id)
    {
        $kode_rahasia_asli = 'BAP_2025_API';
        $kode_rahasia = $this->encrypt->decode($id);
        $from   = $this->path_root . 'maintenance.php';
        $to = $this->path_root . 'finish_maintenance.php';
        if ($kode_rahasia_asli != $kode_rahasia) :
            redirect('/');
        else:
            if (file_exists($from)) {
                if (@rename($from, $to)) {
                    $this->notifMaintenanceFinish();
                    redirect('/');
                } else {
                    $this->notifMaintenanceFinishGagal();
                    redirect('/');
                }
            } else {
                $this->notifMaintenanceFinishGagal();
                redirect('/');
            }
        endif;
    }

    // Notifikasi
    // Maintenance Aktif
    // Sukses
    private function notifMaintenanceAktif()
    {
        $sett_wa = $this->db->get_where('sistem_wa', ['id' => 1])->row_array();
        $no_hp   = $sett_wa['uji_coba'];
        $pesan = 'Update Maintenance ' . date('Y-m-d') . ' Berhasil di Aktifkan.';
        $footer = '*KSP Bina Artha Prima*';
        $data_inputan = array(
            'api_key'    => $sett_wa['api_key'],
            'sender'     => $sett_wa['no_hp'],
            'number'     => $no_hp,
            'message'    => $pesan,
            'footer'     => $footer
        );
        $curl = curl_init($sett_wa['endpoint_url']);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data_inputan));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_exec($curl);
        curl_close($curl);
    }
    // Gagal
    private function notifMaintenanceAktifGagal()
    {
        $sett_wa = $this->db->get_where('sistem_wa', ['id' => 1])->row_array();
        $no_hp   = $sett_wa['uji_coba'];
        $pesan = 'Update Maintenance ' . date('Y-m-d') . ' Gagal di Aktifkan.';
        $footer = '*KSP Bina Artha Prima*';
        $data_inputan = array(
            'api_key'    => $sett_wa['api_key'],
            'sender'     => $sett_wa['no_hp'],
            'number'     => $no_hp,
            'message'    => $pesan,
            'footer'     => $footer
        );
        $curl = curl_init($sett_wa['endpoint_url']);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data_inputan));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_exec($curl);
        curl_close($curl);
    }
    // Maintenance No Aktif
    // Sukses
    private function notifMaintenanceFinish()
    {
        $sett_wa = $this->db->get_where('sistem_wa', ['id' => 1])->row_array();
        $no_hp   = $sett_wa['uji_coba'];
        $pesan = 'Update Maintenance ' . date('Y-m-d') . ' Berhasil di Matikan.';
        $footer = '*KSP Bina Artha Prima*';
        $data_inputan = array(
            'api_key'    => $sett_wa['api_key'],
            'sender'     => $sett_wa['no_hp'],
            'number'     => $no_hp,
            'message'    => $pesan,
            'footer'     => $footer
        );
        $curl = curl_init($sett_wa['endpoint_url']);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data_inputan));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_exec($curl);
        curl_close($curl);
    }
    // Gagal
    private function notifMaintenanceFinishGagal()
    {
        $sett_wa = $this->db->get_where('sistem_wa', ['id' => 1])->row_array();
        $no_hp   = $sett_wa['uji_coba'];
        $pesan = 'Update Maintenance ' . date('Y-m-d') . ' Gagal di Matikan.';
        $footer = '*KSP Bina Artha Prima*';
        $data_inputan = array(
            'api_key'    => $sett_wa['api_key'],
            'sender'     => $sett_wa['no_hp'],
            'number'     => $no_hp,
            'message'    => $pesan,
            'footer'     => $footer
        );
        $curl = curl_init($sett_wa['endpoint_url']);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data_inputan));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_exec($curl);
        curl_close($curl);
    }
}

/* End of file ControllerMaintenance.php and path \application\controllers\notifikasi\ControllerMaintenance.php */