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

class ControllerReviewKonsumen 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()
    {
        redirect('https://kspbinaartha.com/');
    }
    public function konsumen_review($id)
    {
        $kontrak_id               = $this->encrypt->decode($id);
        $data_kontrak             = $this->db->get_where('p_koperasi_kontrak', ['id' => $kontrak_id])->row_array();
        $data_konsumen            = $this->db->get_where('anggota', ['id' => $data_kontrak['anggota_id']])->row_array();
        if ($data_kontrak == null) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Maaf, anda tidak dapat mengisi form review konsumen!</div>');
            redirect('https://kspbinaartha.com');
        endif;
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'judul'             => 'Review Konsumen || Konsumen Menilai Kinerja Marketing',
            'data_konsumen'     => $data_konsumen,
            'data_marketing'    => $data_kontrak
        );
        $this->load->view('document/review/index', $data);
    }
    public function crud()
    {
        if (isset($_POST['kirimReviewsKonsumen'])) :
            // Cek apakah orang ini sudah memberikan rating 6 kali atau belum dalam satu bulan
            $bulan = date('m');
            $tahun = date('Y');
            $cek_review = $this->db->get_where('k_rkonsumen', ['hp_konsumen' => $this->input->post('no_hp'), 'MONTH(tgl_review)' => $bulan, 'YEAR(tgl_review)' => $tahun])->result();
            $jml_review = count($cek_review);
            if ($jml_review > 6) :
                redirect('https://kspbinaartha.com/terima-kasih/');
            else:
                $data_inputan = array(
                    'login_id'     => htmlspecialchars($this->input->post('login_id')),
                    'tgl_review'   => date('Y-m-d'),
                    'nm_konsumen'  => htmlspecialchars($this->input->post('nm_anggota')),
                    'hp_konsumen'  => htmlspecialchars($this->input->post('no_hp')),
                    'rating'       => htmlspecialchars($this->input->post('rating')),
                    'kritik_saran' => htmlspecialchars($this->input->post('opinion'))
                );
                $this->db->insert('k_rkonsumen', $data_inputan);
                redirect('https://kspbinaartha.com/terima-kasih/');
            endif;
        else:
            redirect('https://kspbinaartha.com/terima-kasih/');
        endif;
    }
}

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