<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User extends CI_Controller {	
    public function __construct(){
        parent::__construct();		
        $this->load->model('M_user');
        $this->load->library('mailer');
        $this->load->model('M_template_email');
        $this->load->library('whatsapplib');
    }
	public function data_user(){
        $data["valid"] = $this->input->post('valid',TRUE);
        $result = $this->M_user->data_user($data)->result_array();
        /*for($i=0; $i<count($result);$i++){
            $data['id_penerima'] = $result[$i]['id_penerima'];
            $donasi = $this->m_penerima->data_donasi($data)->result_array();
            $a=0;
            for($o=0; $o<count($donasi);$o++){
                $a+=$donasi[$o]['donasi'];
            }
            $result[$i]['hasil_donasi']=$a;
        }*/
        echo json_encode($result);
    }
    private function send_mail($penerima,$pesan,$subjek){
        $email_penerima = $penerima;
        
        $logo = base_url('assets/img/logo.png');
        $template_email = $this->M_template_email->select_data_template_email()->row_array();
        // $attachment = $_FILES['attachment'];
        $content = $this->load->view('template_email/content_mail', array('pesan'=>$pesan,'logo'=>$logo,'template_email'=>$template_email), true); // Ambil isi file content.php dan masukan ke variabel $content
        $sendmail = array(
          'email_penerima'=>$email_penerima,
          'subjek'=>$subjek,
          'content'=>$content
          // 'attachment'=>$attachment
        );
        $send = $this->mailer->send($sendmail); // Panggil fungsi send yang ada di librari Mailer
        // echo "<b>".$send['status']."</b><br />";
        // echo $send['message'];
        // echo "<br /><a href='".base_url("index.php/email")."'>Kembali ke Form</a>";
        return $send;
      }
    public function tolak_user(){
        $data["id_user"] = $this->input->post('id_user',TRUE);
        $respon = $this->M_user->tolak_user($data);
        if ($respon) {
            $ambil_email= $this->M_user->ambil_email($data)->row_array();
            $ambil_tlp= $this->M_user->ambil_tlp($data)->row_array();
            $template_email = $this->M_template_email->select_data_template_email()->row_array();
            $subjek = $template_email['sjk_user_reg_rjk'];
            $pesan = $template_email['msg_user_reg_rjk'];
            $send_mail = $this->send_mail($ambil_email['email'],$pesan,$subjek);
            ///////////////
            $pesan_wa = "Maaf, registrasi ditolak oleh admin PKA";
                $this->whatsapplib->sendwa($ambil_tlp['tlp'],$pesan_wa);
            //////
            if($send_mail['status'] == "Sukses"){
                echo json_encode(true);
            }else{
                echo json_encode(false);
            }
        }else{
            echo json_encode(false);
        }
    }
    public function terima_user(){
        $data["id_user"] = $this->input->post('id_user',TRUE);
        $respon = $this->M_user->terima_user($data);
        if ($respon) {
            $ambil_email= $this->M_user->ambil_email($data)->row_array();
            $ambil_tlp= $this->M_user->ambil_tlp($data)->row_array();
            $template_email = $this->M_template_email->select_data_template_email()->row_array();
            $subjek = $template_email['sjk_user_reg_acc'];
            $pesan = str_replace("%login%",'<a href="'.base_url('login.aspx').'">Login</a>',$template_email['msg_user_reg_acc']);
            $send_mail = $this->send_mail($ambil_email['email'],$pesan,$subjek);
            /////////
            $pesan_wa = "Registrasi disetujui oleh admin PKA\nSilahkan Login : https://pka.gpibcinere.org/";
                $this->whatsapplib->sendwa($ambil_tlp['tlp'],$pesan_wa);
            //////
            if($send_mail['status'] == "Sukses"){
                echo json_encode(true);
            }else{
                echo json_encode(false);
            }
        }else{
            echo json_encode(false);
        }
    }
    public function hapus_user(){
        $data["id_user"] = $this->input->post('id_user',TRUE);
        $respon= $this->M_user->hapus_user($data);
        echo json_encode($respon);
    }
    public function ambil_valid_oleh(){
        $data["kode_user"] = $this->input->post('kode_user',TRUE);
        $respon= $this->M_user->ambil_valid_oleh($data)->row_array();
        echo json_encode($respon['username']);
    }
	
}
