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

class ControllerCekHariLibur extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->set_timezone();
        $this->load->library('encrypt');
        $this->load->library('form_validation');
        $this->load->library('Mycustom');
        $this->load->library('pdfgenerator');
        $this->load->library('Notifikasi');
        $this->load->model('bagian_notif/koperasi');
        $this->set_timezone();
    }
    public function set_timezone()
    {
        date_default_timezone_set("Asia/Jakarta");
    }

    public function index()
    {
        $hari_baru = $this->tanggalMerah('2024-09-08');
        if ($hari_baru['hari_libur'] == 1) :
            $data = array(
                'tgl_tempo' => $hari_baru['tgl_baru']
            );
            $this->db->where('tgl_tempo', date('Y-m-d'));
            $this->db->where('status_angsuran', 'BELUM');
            $this->db->update('p_koperasi_angsuran', $data);
        endif;
    }

    private function tanggalMerah($value)
    {
        $array = json_decode(file_get_contents("https://raw.githubusercontent.com/guangrei/APIHariLibur_V2/main/calendar.json"), true);
        //check tanggal merah berdasarkan libur nasional
        //check tanggal merah berdasarkan hari minggu
        if (date("D", strtotime($value)) === "Sun") :
            $data = array(
                'hari_libur' => 1,
                'tgl_baru'   => date('Y-m-d', strtotime('+' . 1 . ' days', strtotime($value)))
            );
        //check tanggal merah berdasarkan libur nasional
        elseif (isset($array[$value]) && $array[$value]["holiday"]) :
            $data = array(
                'hari_libur' => 2,
                'tgl_baru'   => date('Y-m-d', strtotime('+' . 1 . ' week', strtotime($value)))
            );
        //bukan tanggal merah
        else :
            $data = array(
                'hari_libur' => 0,
                'tgl_baru'   => date('Y-m-d')
            );
        endif;
        return $data;


        // $cek_angsuran = $this->db->get_where('p_koperasi_angsuran', ['id' => htmlspecialchars($this->input->post('angs_id'))])->row_array();
        // $daftar_angsuran = $this->db->get_where('p_koperasi_angsuran', ['kontrak_id' => $cek_angsuran['kontrak_id']])->result();
        // $data_kontrak = $this->db->get_where('p_koperasi_kontrak', ['id' => $cek_angsuran['kontrak_id']])->row_array();
        // if ($data_kontrak['aturan_id'] == 1) :
        //     foreach ($daftar_angsuran as $Daftar_angsuran) :
        //         if ($Daftar_angsuran->id >= htmlspecialchars($this->input->post('angs_id'))) :
        //             $data = array(
        //                 'tgl_tempo'     => date('Y-m-d', strtotime('+' . 1 . ' week', strtotime($Daftar_angsuran->tgl_tempo)))
        //             );
        //             $this->db->where('id', $Daftar_angsuran->id);
        //             $this->db->update('p_koperasi_angsuran', $data);
        //         endif;
        //     endforeach;
        // elseif ($data_kontrak['aturan_id'] == 2):
        //     foreach ($daftar_angsuran as $Daftar_angsuran) :
        //         if ($Daftar_angsuran->id >= htmlspecialchars($this->input->post('angs_id'))) :
        //             $data = array(
        //                 'tgl_tempo'     => date('Y-m-d', strtotime('+' . 2 . ' week', strtotime($Daftar_angsuran->tgl_tempo)))
        //             );
        //             $this->db->where('id', $Daftar_angsuran->id);
        //             $this->db->update('p_koperasi_angsuran', $data);
        //         endif;
        //     endforeach;
        // elseif ($data_kontrak['aturan_id'] == 3):
        //     foreach ($daftar_angsuran as $Daftar_angsuran) :
        //         if ($Daftar_angsuran->id >= htmlspecialchars($this->input->post('angs_id'))) :
        //             $data = array(
        //                 'tgl_tempo'     => date('Y-m-d', strtotime('+' . 1 . ' month', strtotime($Daftar_angsuran->tgl_tempo)))
        //             );
        //             $this->db->where('id', $Daftar_angsuran->id);
        //             $this->db->update('p_koperasi_angsuran', $data);
        //         endif;
        //     endforeach;
        // elseif ($data_kontrak['aturan_id'] == 4):
        //     foreach ($daftar_angsuran as $Daftar_angsuran) :
        //         if ($Daftar_angsuran->id >= htmlspecialchars($this->input->post('angs_id'))) :
        //             $data = array(
        //                 'tgl_tempo'     => date('Y-m-d', strtotime('+' . $data_kontrak['tempo_pinjaman'] . ' month', strtotime($Daftar_angsuran->tgl_tempo)))
        //             );
        //             $this->db->where('id', $Daftar_angsuran->id);
        //             $this->db->update('p_koperasi_angsuran', $data);
        //         endif;
        //     endforeach;
        // elseif ($data_kontrak['aturan_id'] == 5):
        //     foreach ($daftar_angsuran as $Daftar_angsuran) :
        //         if ($Daftar_angsuran->id >= htmlspecialchars($this->input->post('angs_id'))) :
        //             $data = array(
        //                 'tgl_tempo'     => date('Y-m-d', strtotime('+' . 1 . ' month', strtotime($Daftar_angsuran->tgl_tempo)))
        //             );
        //             $this->db->where('id', $Daftar_angsuran->id);
        //             $this->db->update('p_koperasi_angsuran', $data);
        //         endif;
        //     endforeach;
        // elseif ($data_kontrak['aturan_id'] == 6):
        //     foreach ($daftar_angsuran as $Daftar_angsuran) :
        //         if ($Daftar_angsuran->id >= htmlspecialchars($this->input->post('angs_id'))) :
        //             $data = array(
        //                 'tgl_tempo'     => date('Y-m-d', strtotime('+' . 1 . ' month', strtotime($Daftar_angsuran->tgl_tempo)))
        //             );
        //             $this->db->where('id', $Daftar_angsuran->id);
        //             $this->db->update('p_koperasi_angsuran', $data);
        //         endif;
        //     endforeach;
        // endif;
    }
}

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