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

class ControllerLogsWhatApps extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        //validasi jika user belum login
        check_not_login();
        if ($this->session->userdata('level') != 1) {
            redirect();
        }
        $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_owner/insert_model');
        $this->load->model('bagian_owner/select_model');
        $this->load->model('bagian_owner/update_model');
    }
    public function set_timezone()
    {
        date_default_timezone_set("Asia/Jakarta");
    }

    public function index()
    {
        $notif                 = $this->notifikasi->notif_owner();
        // Data Login
        $data_login            = $this->userlogin->user_login();
        $data_id_login         = $this->session->userdata('isloggedin');
        // Data Isian Konten
        // Data Logs Notifikasi
        $tanggal = date('d');
        $bulan = date('m');
        $tahun = date('Y');
        $tgl = $tahun . '-' . $bulan . '-' . $tanggal;
        $data_logs = $this->select_model->vd_logsWA($tgl);
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'folder'              => 'logs/whatapps',
            'page'                => 'index',
            'judul'               => 'Pinjaman Aktif || Bagian Manager',
            'data_id_login'       => $data_id_login,
            'data_login'          => $data_login,
            'notif'               => $notif,
            // Isi dalam konten
            'data_logs'           => $data_logs,
            'tgl_kirim'            => $tgl
        );
        $this->load->view('owner/index', $data);
    }
    public function filter()
    {
        if (isset($_POST['filterTransaksiKasTanggal'])) :
            $notif                 = $this->notifikasi->notif_owner();
            // Data Login
            $data_login            = $this->userlogin->user_login();
            $data_id_login         = $this->session->userdata('isloggedin');
            // Data Isian Konten
            // Data Logs Notifikasi
            $tanggal = htmlspecialchars($this->input->post('tanggal_transaksi'));
            if ($tanggal < 10) :
                $tgl_ok = '0' . $tanggal;
            else:
                $tgl_ok = $tanggal;
            endif;
            $bulan = htmlspecialchars($this->input->post('bulan_transaksi'));
            $tahun = htmlspecialchars($this->input->post('tahun_transaksi'));
            $tgl = $tahun . '-' . $bulan . '-' . $tgl_ok;
            $data_logs = $this->select_model->vd_logsWA($tgl);
            $data = array(
                // Untuk memanggil halaman sesuai menu
                'folder'              => 'logs/whatapps',
                'page'                => 'index',
                'judul'               => 'Logs Sending WhatApps || Bagian Manager',
                'data_id_login'       => $data_id_login,
                'data_login'          => $data_login,
                'notif'               => $notif,
                // Isi dalam konten
                'data_logs'           => $data_logs,
                'tgl_kirim'           => $tgl
            );
            $this->load->view('owner/index', $data);
        endif;
        if (isset($_POST['resetTransaksiKasTanggal'])) :
            redirect('owner/history_logs_activity/whatapps_notif');
        endif;
    }
    public function resned($id)
    {
        $logs_id   = $this->encrypt->decode($id);
        $data_logs = $this->db->get_where('logs_wa', ['id' => $logs_id])->row_array();
        $sett_wa   = $this->db->get_where('sistem_wa', ['id' => 2])->row_array();
        $cek_nomor = substr($data_logs['no_hp'], 0, 1);
        if ($cek_nomor == 0) :
            $no_hp = '62' . substr($data_logs['no_hp'], 1, 20);
        else :
            $no_hp = $data_logs['no_hp'];
        endif;
        $link_file = $data_logs['link_pdf'];
        $link_reviews = $data_logs['link_reviews'];
        $pesan = $data_logs['isi_pesan'];
        $footer = '*KSP Bina Artha Prima*';
        $data_inputan = array(
            'api_key'    => $sett_wa['api_key'],
            'sender'     => $sett_wa['no_hp'],
            'number'     => $no_hp,
            'media_type' => 'document',
            'caption'    => $pesan,
            'footer'     => $footer,
            'url'        => $link_file
        );
        // Jumlah kirim
        $data_kirim = array(
            'jml_kirim' => $data_logs['jml_kirim'] + 1
        );
        $this->db->where('id', $logs_id);
        $this->db->update('logs_wa', $data_kirim);
        // End Jumlah kirim
        $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);
        redirect('owner/history_logs_activity/whatapps_notif');
    }
}

/* End of file ControllerLogsWhatApps.php and path \application\controllers\owner\logsnotif\ControllerLogsWhatApps.php */