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

class ControllerWA 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->library('Phpmailer_library');
        $this->load->model('bagian_notif/koperasi');
        $this->set_timezone();
    }
    public function set_timezone()
    {
        date_default_timezone_set("Asia/Jakarta");
    }
    public function index()
    {
        redirect('/');
    }
    public function slip_gaji($id)
    {
        // Data Login
        $data_login            = $this->userlogin->user_login(); // Menampilkan siapa yang login
        // End Data Login
        $gaji_id   = $this->encrypt->decode($id);
        $data_gaji = $this->db->get_where('k_gaji', ['id' => $gaji_id])->row_array();
        $data_karyawan = $this->db->get_where('sistem_login', ['id' => $data_gaji['login_id']])->row_array();
        $this->_kirimSlipWa($data_gaji, $data_karyawan);
        $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil mengirim Slip Gaji ke WhatApps!</div>');
        if ($data_login->role_id == 2):
            // Manager
            redirect('manager/identitas/detail_gaji/' . $this->encrypt->encode($gaji_id));
        elseif ($data_login->role_id == 3):
            // Keuangan
            redirect('keuangan/identitas/detail_gaji/' . $this->encrypt->encode($gaji_id));
        elseif ($data_login->role_id == 4):
            // Kabag Arisan
            redirect('arisan/salery/detail/' . $this->encrypt->encode($gaji_id));
        elseif ($data_login->role_id == 6):
            // Keuangan
            redirect('kabag_marketing/salery/detail/' . $this->encrypt->encode($gaji_id));
        elseif ($data_login->role_id == 7):
            // Keuangan
            redirect('marketing/salery/detail/' . $this->encrypt->encode($gaji_id));
        endif;
    }
    // Kirim Slip Gaji Wa
    private function _kirimSlipWa($data_gaji, $data_karyawan)
    {
        // setting WA
        $sett_wa = $this->db->get_where('sistem_wa', ['id' => 2])->row_array();
        $cek_nomor = substr($data_karyawan['no_hp'], 0, 1);
        if ($sett_wa['status'] == 'AKTIF') :
            if ($cek_nomor == 0) :
                $no_hp = '62' . substr($data_karyawan['no_hp'], 1, 20);
            else :
                $no_hp = $data_karyawan['no_hp'];
            endif;
            // Contoh URL http://localhost/binaartha/assets/slip_gaji/2025016.pdf
            $url = base_url('assets/slip_gaji/' . date('Y', strtotime($data_gaji['tgl_gaji'])) . date('m', strtotime($data_gaji['tgl_gaji'])) . $data_karyawan['id'] . '.pdf');
        else:
            $no_hp = $sett_wa['uji_coba'];
            $url = 'https://siap.kspbinaartha.com/assets/slip_gaji/20250124.pdf';
        endif;
        $caption = 'Hey, ' . $data_karyawan['nm_panggilan'] . ', Berikut Slip Gaji Periode ' . date('m', strtotime($data_gaji['tgl_gaji'])) . '-' . date('Y', strtotime($data_gaji['tgl_gaji'])) . '. Untuk membuka Slip Gaji diatas diperlukan Password, password tersebut terdiri dari :

- YYYY : Tahun Kelahiran Anda
- MM : Bulan Kelahiran Anda
- DD : Tanggal Kelahiran Anda

Contoh : Tanggal lahir *01 Febuari 1980*, Maka password : *19800201*.
        
*Info Hub :* 085162800600
*_PESAN OTOMATIS ROBIN_*';
        $body = array(
            "api_key" => $sett_wa['api_key'],
            "receiver" => $no_hp,
            "data" => array(
                "url" => $url,
                "media_type" => "file",
                "caption" => $caption
            )
        );
        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => $sett_wa['endpoint_url'],
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => json_encode($body),
            CURLOPT_HTTPHEADER => [
                "Accept: */*",
                "Content-Type: application/json",
            ],
        ]);
        curl_exec($curl);
        curl_error($curl);
        curl_close($curl);
    }
}

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