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

class ControllerEmail 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->_kirimSlipEmail($gaji_id, $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 Email!</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):
            // Kabag Marketing
            redirect('kabag_marketing/salery/detail/' . $this->encrypt->encode($gaji_id));
        elseif ($data_login->role_id == 7):
            // Marketing
            redirect('marketing/salery/detail/' . $this->encrypt->encode($gaji_id));
        endif;
    }
    // Email Slip Gaji
    private function _kirimSlipEmail($gaji_id, $data_gaji, $data_karyawan)
    {
        // Data Gaji
        $data_gaji = $this->db->get_where('k_gaji', ['id' => $gaji_id])->row_array();
        // Periode Gaji
        $bulan     = $this->mycustom->bulanIndo(date('m', strtotime($data_gaji['tgl_gaji'])));
        $tahun     = date('Y', strtotime($data_gaji['tgl_gaji']));
        $periode   = $bulan . ' ' . $tahun;
        $nm_file   = date('Y', strtotime($data_gaji['tgl_gaji'])) . date('d', strtotime($data_gaji['tgl_gaji'])) . $data_karyawan['id'];
        // PHPMailer object
        $mail = $this->phpmailer_library->load();
        $sett_mail = $this->db->get_where('sistem_mail', ['id' => 1])->row_array();
        // SMTP configuration
        $mail->isSMTP();
        $mail->Host     = $sett_mail['host_mail'];
        $mail->SMTPAuth = true;
        $mail->Username = $sett_mail['user_mail'];
        $mail->Password = $this->encrypt->decode($sett_mail['pass_mail']);
        $mail->SMTPSecure = 'ssl';
        $mail->Port     = 465;

        $mail->setFrom($sett_mail['user_mail'], $sett_mail['ket_mail']);
        $mail->addReplyTo('dev.binaartha@gmail.com', 'Slip Gaji Karyawan');

        // Add a recipient
        $mail->addAddress($data_karyawan['email']);
        // Email subject
        $mail->Subject = 'Slip Gaji Karyawan';
        // Set email format to HTML
        $mail->isHTML(true);
        // Email body content
        $mailContent = 'Yth, Bpk/Ibu ' . $data_karyawan['nm_panggilan'] . ', bersama email ini, kami sampaikan Slip Gaji Karyawan periode ' . $periode . '. <br><br> Silahkan untuk membuka Slip Gaji dengan password Anda.<br><br>
        Password Anda adalah 8 digit tanggal lahir anda dalam format YYYYMMDD, dimana : <br>
            <p>- YYYY : Tahun Kelahiran Anda</p>
            <p>- MM : Bulan Kelahiran Anda</p>
            <p>- DD : Tanggal Kelahiran Anda</p><br>
            Contoh : Tanggal lahir <b>01 Febuari 1980</b>, Maka password : <b>19800201</b><br><br><br>
            Terimakasih,<br><br><br>
            Hormat Kami,<br>
            KSP Bina Artha Prima';
        $mail->Body = $mailContent;
        $url = 'https://siap.kspbinaartha.com/assets/slip_gaji/' . $nm_file . '.pdf';
        $mail->addStringAttachment(
            file_get_contents($url),
            'slip_gaji_' . $nm_file . '.pdf'
        );
    }
}

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