<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class pka_ev_tri extends CI_Controller {
    public function __construct(){
        parent::__construct();		
        $this->load->model('M_pka_ev_tri');
        $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 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";
        }
    }
    public function data_ev_pka_tri($no_pka){
        $result = $this->M_pka_ev_tri->data_ev_pka_tri($no_pka)->result_array();
        for($i=0; $i<count($result);$i++){
            if($result[$i]['ev_tri']==1){
                $kma = $result[$i]['kma'];
                $ev = $this->db->query("SELECT * FROM tb_ev_tri WHERE kma = '$kma'")->row_array();
                $status = $ev['status'];
            }else{
                $status = "9";
            }
            // $result[$i]['tgl_buat'] = $this->ubahDate($result[$i]['created_at']);
            // $result[$i]['waktu_buat'] = $this->ubahTime($result[$i]['created_at']);
            // $r = $this->M_pka->ambil_detail_pka_evaluasi( $result[$i]['kode_pka'])->row_array();
            $result[$i]['status'] = $status;
        }
        echo json_encode($result);
    }
    public function data_ev_pka_tri_adm($no_pka){
        $result = $this->M_pka_ev_tri->data_ev_pka_tri_adm($no_pka)->result_array();
        for($i=0; $i<count($result);$i++){
            if($result[$i]['ev_tri']==1){
                $kma = $result[$i]['kma'];
                $ev = $this->db->query("SELECT * FROM tb_ev_tri WHERE kma = '$kma'")->row_array();
                $status = $ev['status'];
            }else{
                $status = "9";
            }
            // $result[$i]['tgl_buat'] = $this->ubahDate($result[$i]['created_at']);
            // $result[$i]['waktu_buat'] = $this->ubahTime($result[$i]['created_at']);
            // $r = $this->M_pka->ambil_detail_pka_evaluasi( $result[$i]['kode_pka'])->row_array();
            $result[$i]['status'] = $status;
        }
        echo json_encode($result);
    }
    public function data_ev_pka_tri_pka($periode_thn){
        $result = $this->M_pka_ev_tri->data_ev_pka_tri_pka($periode_thn)->result_array();
        for($i=0; $i<count($result);$i++){
            $result[$i]['tgl_buat'] = $this->ubahDate($result[$i]['created_at']);
            $result[$i]['waktu_buat'] = $this->ubahTime($result[$i]['created_at']);
        }
        // for($i=0; $i<count($result);$i++){
        //     if($result[$i]['ev_tri']==1){
        //         $kma = $result[$i]['kma'];
        //         $ev = $this->db->query("SELECT * FROM tb_ev_tri WHERE kma = '$kma'")->row_array();
        //         $status = $ev['status'];
        //     }else{
        //         $status = "9";
        //     }
        //     // $result[$i]['tgl_buat'] = $this->ubahDate($result[$i]['created_at']);
        //     // $result[$i]['waktu_buat'] = $this->ubahTime($result[$i]['created_at']);
        //     // $r = $this->M_pka->ambil_detail_pka_evaluasi( $result[$i]['kode_pka'])->row_array();
        //     $result[$i]['status'] = $status;
        // }
        echo json_encode($result);
    }
    public function data_ev_pka_tri_pka_adm($periode_thn){
        $result = $this->M_pka_ev_tri->data_ev_pka_tri_pka_adm($periode_thn)->result_array();
        for($i=0; $i<count($result);$i++){
            $result[$i]['tgl_buat'] = $this->ubahDate($result[$i]['created_at']);
            $result[$i]['waktu_buat'] = $this->ubahTime($result[$i]['created_at']);
        }
        // for($i=0; $i<count($result);$i++){
        //     if($result[$i]['ev_tri']==1){
        //         $kma = $result[$i]['kma'];
        //         $ev = $this->db->query("SELECT * FROM tb_ev_tri WHERE kma = '$kma'")->row_array();
        //         $status = $ev['status'];
        //     }else{
        //         $status = "9";
        //     }
        //     // $result[$i]['tgl_buat'] = $this->ubahDate($result[$i]['created_at']);
        //     // $result[$i]['waktu_buat'] = $this->ubahTime($result[$i]['created_at']);
        //     // $r = $this->M_pka->ambil_detail_pka_evaluasi( $result[$i]['kode_pka'])->row_array();
        //     $result[$i]['status'] = $status;
        // }
        echo json_encode($result);
    }
    public function update_sesi_ev_pka_tri(){
        

        $dari = $this->input->post('dari',TRUE);

        $sampai = $this->input->post('sampai',TRUE);
        $data['periode_thn'] = $this->input->post('periode_thn',TRUE);
        $data['triwulan'] = $this->input->post('triwulan',TRUE);
        $data["dari"] = explode(" ", $dari)[2]."-".$this->switch_bulan(explode(" ", $dari)[1])."-".explode(" ", $dari)[0];
        $data["sampai"] = explode(" ", $sampai)[2]."-".$this->switch_bulan(explode(" ", $sampai)[1])."-".explode(" ", $sampai)[0];
        $data["keterangan"] = $this->input->post('keterangan',TRUE);
        $data["kode_sesi_ev_pka_tri"] = $this->input->post('kode_sesi_ev_pka_tri',TRUE);
        

        if($data["kode_sesi_ev_pka_tri"]==""){
            $token = "";
            $codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            $codeAlphabet.= "0123456789";
            $max = strlen($codeAlphabet) - 1;
            for ($i=0; $i < 8; $i++) {
                $token .= $codeAlphabet[mt_rand(0, $max)];
            } 
            $data['kode_sesi_ev_pka_tri'] = $token;

                $respon = $this->M_pka_ev_tri->add_sesi_ev_pka_tri($data);
                // echo $respon;
        }else{
                $respon = $this->M_pka_ev_tri->update_sesi_ev_pka_tri($data);
        }

        echo json_encode($respon);

    }
    public function data_sesi_ev_pka_tri(){
        $result = $this->M_pka_ev_tri->data_sesi_ev_pka_tri()->row_array();
        if ($result==null) {
            echo json_encode("kosong");
        }else{
            $result['dari_tgl'] = $result['dari'] ==""? "Not Data" : $this->date_to_date($result['dari']);
            $result['sampai_tgl'] = $result['sampai'] ==""? "Not Data" :  $this->date_to_date($result['sampai']);
            // $result['tgl_update'] = $this->ubahDate(explode(" ", $result['updated_at'])[0])." ".explode(" ", $result['updated_at'])[1];
            echo json_encode($result);
        }
    }
    public function publish_sesi_ev_pka_tri(){
        $respon = $this->M_pka_ev_tri->publish_sesi_ev_pka_tri($data);
        echo json_encode($respon);

    }
    public function ambil_bulan_kegiatan(){
        
        $rr = $this->M_pka_ev_tri->ambil_sesi_triwulan()->row_array();
        if($rr['triwulan']=="1"){
            $respon[0]['triwulan']="April";
            $respon[1]['triwulan']="Mei";
            $respon[2]['triwulan']="Juni";
        }elseif($rr['triwulan']=="2"){
            $respon[0]['triwulan']="Juli";
            $respon[1]['triwulan']="Agustus";
            $respon[2]['triwulan']="September";
        }elseif($rr['triwulan']=="3"){
            $respon[0]['triwulan']="Oktober";
            $respon[1]['triwulan']="November";
            $respon[2]['triwulan']="Desember";
        }else{
            $respon[0]['triwulan']="Januari";
            $respon[1]['triwulan']="Februari";
            $respon[2]['triwulan']="Maret"; 
        }
        echo json_encode($respon);
    }
    public function add_ev_tri(){
        $rr = $this->M_pka_ev_tri->ambil_sesi_triwulan()->row_array();
        
        $data['kma'] = $this->input->post('kma',true);
        $data['triwulan_evaluasi'] = $rr['triwulan'];
        $data['bulan_kegiatan'] = json_encode($this->input->post('bulan_kegiatan',true));
        $data['pencapaian'] = $this->input->post('pencapaian',true);
        $data['atp'] = $this->input->post('atp',true);
        $data['realisasi_kualitatif'] = $this->input->post('realisasi_kualitatif',true);
        $data['analisa_keuangan'] = $this->input->post('analisa_keuangan',true);
        $data['terlaksana'] = $this->input->post('terlaksana',true);
        

        $data['realisasi_penerimaan']=preg_replace("/[^0-9]/", '', $this->input->post('realisasi_penerimaan',true));
        $data['realisasi_pengeluaran']=preg_replace("/[^0-9]/", '', $this->input->post('realisasi_pengeluaran',true));

        $respon = $this->M_pka_ev_tri->add_ev_tri($data);
        if($respon == true){
            $this->M_pka_ev_tri->update_ev_tri_pka($data['kma']);
        }
        echo json_encode($respon);
    }
    public function row_ev_tri(){
        $data['kma'] = $this->input->post('kma',true);
        $respon = $this->M_pka_ev_tri->row_ev_tri($data['kma'])->row_array();
        echo json_encode($respon);

    }
    public function update_ev_tri(){
        $data['kma'] = $this->input->post('kma',true);
        $data['bulan_kegiatan'] = json_encode($this->input->post('bulan_kegiatan',true));
        $data['pencapaian'] = $this->input->post('pencapaian',true);
        $data['atp'] = $this->input->post('atp',true);
        $data['realisasi_kualitatif'] = $this->input->post('realisasi_kualitatif',true);
        $data['analisa_keuangan'] = $this->input->post('analisa_keuangan',true);
        $data['terlaksana'] = $this->input->post('terlaksana',true);

        $data['realisasi_penerimaan']=preg_replace("/[^0-9]/", '', $this->input->post('realisasi_penerimaan',true));
        $data['realisasi_pengeluaran']=preg_replace("/[^0-9]/", '', $this->input->post('realisasi_pengeluaran',true));
        $respon = $this->M_pka_ev_tri->update_ev_tri($data);
        if($respon == true){
            if($this->input->post('status',true)=='2'){
                $this->M_pka_ev_tri->update_ev_tri_pka($data['kma'],1);
            }
        }
        
        echo json_encode($respon);
    }
    // public function data_ev_pka_tri_adm($no_pka){
    //     $result = $this->M_pka_ev_tri->data_ev_pka_tri_adm($no_pka)->result_array();
    //     echo json_encode($result);
    // }
    public function approve_ev_tri(){
        $data['no_pka'] = $this->input->post('no_pka',true);
        $respon = $this->M_pka_ev_tri->approve_ev_tri($data['no_pka']);
        echo json_encode($respon);

    }
    public function reject_ev_tri(){
        $data['no_pka'] = $this->input->post('no_pka',true);
        $respon = $this->M_pka_ev_tri->reject_ev_tri($data['no_pka']);
        echo json_encode($respon);

    }
    public function ajukan_ev_tri(){
        $data['no_pka'] = $this->input->post('no_pka',true);
        $respon = $this->M_pka_ev_tri->ajukan_ev_tri($data['no_pka']);
        echo json_encode($data);

    }
}
