<?php



defined('BASEPATH') OR exit('No direct script access allowed');







class Request_revisi_pka extends CI_Controller {



	



    public function __construct(){



        parent::__construct();		



        $this->load->model('M_request_revisi_pka');



        $this->load->library('mailer');



        $this->load->model('M_template_email');



        $this->load->library('whatsapplib');



    }



    private function ubahDate($dateTime){



        $bulan = array("Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember");



        $date = explode(" ", $dateTime)[0];



        return intval(explode("-", $date)[2])." ".$bulan[intval(explode("-", $date)[1])-1]." ".explode("-", $date)[0];



    }



    private function ubahTime($dateTime){



        return explode(" ", $dateTime)[1];



    }



    private function ubahDateTime($dateTime){



        $bulan = array("Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember");



        $date = explode(" ", $dateTime)[0];



        return intval(explode("-", $date)[2])." ".$bulan[intval(explode("-", $date)[1])-1]." ".explode("-", $date)[0].", ".explode(" ", $dateTime)[1];







        return ;











    }



    private function date_to_date($date){



        $bulan = array("Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember");



        // $date = explode(" ", $dateTime)[0];



        return intval(explode("-", $date)[2])." ".$bulan[intval(explode("-", $date)[1])-1]." ".explode("-", $date)[0];



        // return $date;







    }



    private function switch_bulan($param){



        switch ($param) {



          case "Januari":



            return "01";



            break;



          case "Februari":



            return "02";



            break;



          case "Maret":



            return "03";



            break;



          case "April":



            return "04";



            break;



          case "Mei":



            return "05";



            break;



          case "Juni":



            return "06";



            break;



          case "Juli":



            return "07";



            break;



          case "Agustus":



            return "08";



            break;



            case "September":



            return "09";



            break;



          case "Oktober":



            return "10";



            break;



          case "November":



            return "11";



            break;



          default:



            return "12";



        }



    }



    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 data_request_revisi_pka(){



        $result = $this->M_request_revisi_pka->data_request_revisi_pka()->result_array();



        for($i=0; $i<count($result);$i++){



            $result[$i]['tgl_buat'] = $this->ubahDateTime($result[$i]['created_at']);



        }



        echo json_encode($result);



    }



    public function terima_revisi(){



        $data["id_req_rev"] = $this->input->post('id_req_rev',TRUE);



        $data["no_pka"] = $this->input->post('no_pka',TRUE);



        $respon = $this->M_request_revisi_pka->terima_revisi($data);



        if ($respon) {



            $status = $this->M_request_revisi_pka->update_status_pka($data);



            if ($status) {

                $ambil_tlp_um= $this->M_request_revisi_pka->ambil_tlp_um($data)->row_array();

                $msgwa_user = "Dear ".$ambil_tlp_um['nama_um'].",\nAdmin telah menyetujui request revisi anda.\nSilahkan revisi PKA";

                $this->whatsapplib->sendwa($ambil_tlp_um['tlp'],$msgwa_user);



                echo json_encode($status);

                



            }



        }



        



    }



    public function tolak_revisi(){



        $data["id_req_rev"] = $this->input->post('id_req_rev',TRUE);



        $respon = $this->M_request_revisi_pka->tolak_revisi($data);



        echo json_encode($respon);



        



        



    }



    ////////////////////////////////////////////////////////////////////////////////////



    ////////////////////////////End Admin/////////////////////////////////////////////////////////////////////////////



    //////////////////////////////////////////////////////////////////////////////////////////////////



	



}



