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

class ControllerDataKpi extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        check_not_login();
        if ($this->session->userdata('level') != 6) {
            redirect();
        }
        $this->load->library('encrypt');
        $this->load->library('form_validation');
        $this->load->library('mycustom');
        $this->load->library('Notifikasi');
        $this->load->library('pdfgenerator');
        $this->load->model('kabag_marketing/insert_model');
        $this->load->model('kabag_marketing/select_model');
        $this->load->model('kabag_marketing/update_model');
        $this->set_timezone();
    }
    public function set_timezone()
    {
        date_default_timezone_set("Asia/Jakarta");
    }
    public function index()
    {
        // Data Login
        $data_login            = $this->userlogin->user_login();
        $data_id_login         = $this->session->userdata('isloggedin');
        $notif                 = $this->notifikasi->notif_kabag_marketing();
        // Data Komponen dan Faktor
        $komp_faktor           = $this->select_model->vd_KompFaktor();
        $komponen              = $this->db->get_where('k_komp', ['tgt' => 'MARKETING', 'sts_komp' => 'AKTIF'])->result();
        $persentase            = $this->select_model->vd_persentaseKompFaktor();
        // echo '<pre>';
        // var_dump($persentase);
        // die;
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'folder'              => 'data/kpi_marketing',
            'page'                => 'index',
            'judul'               => 'Kelola Data KPI Marketing || Bagian Kabag Marketing',
            'data_id_login'       => $data_id_login,
            'data_login'          => $data_login,
            'notif'               => $notif,
            // Data Nilai Komponen Faktor
            'komponen'            => $komponen,
            'komp_faktor'         => $komp_faktor,
            'persentase'          => $persentase
        );
        $this->load->view('kabag_marketing/index', $data);
    }
    public function target($id)
    {
        $faktor_id = $this->encrypt->decode($id);
        // Data Login
        $data_login            = $this->userlogin->user_login();
        $data_id_login         = $this->session->userdata('isloggedin');
        $notif                 = $this->notifikasi->notif_kabag_marketing();
        // Data Komponen dan Faktor
        $faktor   = $this->db->get_where('k_faktor', ['id' => $faktor_id])->row_array();
        $komponen = $this->db->get_where('k_komp', ['id' => $faktor['komp_id']])->row_array();
        // Data Marketing Aktif
        $marketing = $this->select_model->vd_MarketingAktif();
        $target_kpi = $this->select_model->vd_targetMarketingBulanan($faktor_id);
        // echo '<pre>';
        // var_dump($target_kpi, $marketing);
        // die;
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'folder'              => 'data/kpi_target',
            'page'                => 'index',
            'judul'               => 'Kelola Data KPI Marketing || Bagian Kabag Marketing',
            'data_id_login'       => $data_id_login,
            'data_login'          => $data_login,
            'notif'               => $notif,
            // Data Nilai Komponen Faktor
            'komponen'            => $komponen,
            'faktor'              => $faktor,
            'marketing'           => $marketing,
            'detail_kpi'          => $target_kpi
        );
        $this->load->view('kabag_marketing/index', $data);
    }
    public function kpi_crud()
    {
        if (isset($_POST['simpanTargetAnggota'])) {
            $faktor_id = htmlspecialchars($this->input->post('faktor_id'));
            $login_id  = htmlspecialchars($this->input->post('login_id'));
            $data_target = $this->db->get_where('k_target', ['faktor_id' => $faktor_id, 'login_id' => $login_id, 'sts_target' => 'AKTIF'])->row_array();
            if ($data_target == null) :
                $data_inputan = array(
                    'faktor_id'     => $faktor_id,
                    'login_id'      => $login_id,
                    'berlaku'       => date('Y-m-d'),
                    'tgt_kinerja'   => htmlspecialchars($this->input->post('nominal')),
                    'sts_target'    => 'AKTIF'
                );
                $this->db->insert('k_target', $data_inputan);
                $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Menambah Data Target Marketing!</div>');
            else:
                $data_inputan = array(
                    'berlaku'       => date('Y-m-d'),
                    'tgt_kinerja'   => htmlspecialchars($this->input->post('nominal'))
                );
                $this->db->where('faktor_id', $faktor_id);
                $this->db->where('login_id', $login_id);
                $this->db->where('sts_target', 'AKTIF');
                $this->db->update('k_target', $data_inputan);
                $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Update Data Target Marketing!</div>');
            endif;
            redirect('kabag_marketing/data/kpi_tgt/' . $this->encrypt->encode($faktor_id));
        }
        if (isset($_POST['updateDataTarget'])) {
            $target_id = htmlspecialchars($this->input->post('target_id'));
            $data_target = $this->db->get_where('k_target', ['id' => $target_id])->row_array();
            $data_inputan = array(
                'berlaku'       => date('Y-m-d'),
                'tgt_kinerja'   => htmlspecialchars($this->input->post('tgt_kinerja')),
                'update_at'     => date('Y-m-d H:i:s')
            );
            $this->db->where('id', $target_id);
            $this->db->update('k_target', $data_inputan);
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Update Data Target Marketing!</div>');
            redirect('kabag_marketing/data/kpi_tgt/' . $this->encrypt->encode($data_target['faktor_id']));
        }
    }
}

/* End of file ControllerDataKpi.php and path \application\controllers\kabag_marketing\kelola_data\ControllerDataKpi.php */